一.环境信息
OS: Centos 6.5
Python 3.5
二.由于CentOS 6.5系统自带python2.6 ,故需要先安装python 3.5而后在进行设置
1.安装python 3.5.6 #wget https://www.python.org/ftp/python/3.5.6/Python-3.5.6.tgz #tar -zxf Python-3.5.6.tgz #cd Python-3.5.6 #./configure --prefix=/usr/local/python3 #make #make install 2.修改链接文件 #ln -s /usr/local/python3/bin/python3 /usr/bin/python3 3.添加环境变量 echo $PATH /usr/local/java/bin:/usr/local/java/bin:/usr/local/java/bin:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/python3/bin 4.执行py脚本时,如遇到报错 Python ImportError: No module named 'requests' 需要安装requests模块,前提是通过pip install requests安装,如没有pip #wget https://files.pythonhosted.org/packages/69/81/52b68d0a4de760a2f1979b0931ba7889202f302072cc7a0d614211bc7579/pip-18.0.tar.gz # tar -xzvf pip-18.0.tar.gz # cd pip-18.0 # python3 setup.py install 5.安装requests模块 #pip3 install requests 6.执行py脚本 #python3 alert.py "hello guys too" b'{"errmsg":"ok","errcode":0}'
三. zabbix py脚本
#!/usr/bin/python3 #-*- coding: utf-8 -*- #Author:DingLin import requests import json import sys import os headers = {'Content-Type':'application/json;charset=utf-8'} api_url = "https://oapi.dingtalk.com/robot/send?access_token=ad3172fe72faf277548d4db6ada1ed02c3d3c3e0a0xxxxxxxxxx" def msg(text): json_text= { "msgtype": "text", "at": { "atMobiles": [ "1xxxxxxxx0" ], "isAtAll": "false" }, "text": { "content": text } } print (requests.post(api_url,json.dumps(json_text),headers=headers).content) if __name__ == '__main__': text = sys.argv[1] msg(text)
钉钉API Reference: https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.karFPe&treeId=257&articleId=105735&docType=1