shell脚本调用企业微信api发送告警

alert.sh

#!/bin/sh

corpId=xxxxxxxxxxx
corpSecret=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

###### get token (expires: 2 hour)
### version 1
#   curl "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=${corpId}&corpsecret=${corpSecret}"  |awk -F\" '{prin
t $10}'  > ./token_tmp.txt
#   tokenTmp=$(cat ./token_tmp.txt)

### version 2
tokenTmp=$(curl -s "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=${corpId}&corpsecret=${corpSecret}"  |awk -
F\" '{print $10}')

#  echo "The current token is ${tokenTmp}"

###### send json message by wechat api
curl -H "Content-Type: application/json" -XPOST -d @./post.json https://qyapi.weixin.qq.com/cgi-bin/message/send?acce
ss_token=${tokenTmp} &>/dev/null

echo "Alert send success !!!"

#END

post.json


{
   "touser" : "xxxx",
   "msgtype" : "text",
   "agentid" : 1000002,
   "text" : {
       "content" : "alert script test"
   },
   "safe":0
}

posted @ 2019-08-07 14:56  M1927  阅读(145)  评论(0)    收藏  举报