python3 钉钉 加签名 钉钉群机器人巡检告警 脚本
Posted 闭关苦炼内功
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python3 钉钉 加签名 钉钉群机器人巡检告警 脚本相关的知识,希望对你有一定的参考价值。
本文接上文
https://blog.csdn.net/frdevolcqzyxynjds/article/details/128455191
cat dd-os-t1.py
#!/usr/bin/env python3
# encoding=utf-8
import json
import sys
from hdfs import *
import json
import hashlib
import base64
import hmac
import time
import requests
from urllib.parse import quote_plus
from datetime import datetime
from subprocess import call
import subprocess
timestamp = str(round(time.time() * 1000))
url = 'https://oapi.dingtalk.com/robot/send?access_token=你的webhook'
secret = '你的签名'
secret_enc = secret.encode('utf-8')
string_to_sign = '\\n'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = quote_plus(base64.b64encode(hmac_code))
class Send_Message:
def __init__(self, text):
self.text = text
def send_message(self):
headers = 'Content-Type': 'application/json'
webhook = url + '×tamp=' + timestamp + '&sign=' + sign
data =
"msgtype": "text",
"text":
"content": "%s" % (self.text)
value = json.dumps(data)
r = requests.post(webhook, value, headers=headers)
return r.text
if __name__ == "__main__":
py_path = sys.executable
cmd = """echo '----';
date +%F-%H:%M:%S;
echo '';
echo 'node info: ';hostname -f;
echo '';
echo 'kernel info: ';uname -a;
echo '';
echo 'os info: ';cat /etc/system-release;
echo '';
echo 'arch: ';hostnamectl;
echo '';
echo 'cpu info: ';lscpu;
echo '';
echo 'mem info: ';free -h;
echo '';
echo 'disk info: ';lsblk;echo;df -hT;
echo '';
echo 'ip info: ';hostname -I;
echo '';
date +%F-%H:%M:%S;
echo '----';
""".format(py_path, py_path)
# statusoutput = subprocess.getstatusoutput(cmd)
result = subprocess.getoutput(cmd)
s = Send_Message(result + "\\n\\n巡检成功!" + "\\n\\n通知时间:".format(datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
s.send_message()
sys.exit(0)
打完收工!
我们下期见!
以上是关于python3 钉钉 加签名 钉钉群机器人巡检告警 脚本的主要内容,如果未能解决你的问题,请参考以下文章