且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

shell 微信报警脚本

更新时间:2022-09-15 22:39:40

CorpID='xxxxxx'


#对应上面要记下的Secret

Secret='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'


#获取微信调用接口凭证access_token的链接地址

#这里需要用到上面的CorpID和Secret变量

GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CorpID&corpsecret=$Secret"


#获取access_token

Gtoken=$(/usr/bin/curl -s -G $GURL|awk -F\" '{print $10}')

#echo $Gtoken


#微信发送消息接口地址,这里需要用到上面获取的Gtoken变量

PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken"


#调用微信发送信息接口的一些参数说明

#touser:企业号中的用户账号,成员ID列表(消息接收者,多个接收者用‘|’分隔,最多支持1000个),特殊情况:指定为@all,则向关注该企业应用的全部成员发送

#toparty:企业号中的部门id,定义了范围,组内成员都可接收到消息,部门ID列表,多个接收者用‘|’分隔,最多支持100个。当touser为@all时忽略本参数

#agentid:企业应用id,整型。可在应用的设置页面查看

#content: 表示发送微信消息的内容消息内容,最长不超过2048个字节,主页型应用推送的文本消息在微信端最多只显示20个字(包含中英文)

#info:    返回执行结果信息{'result':None,'code':None};'code':0或者非零 ;0表示成功 非零表示失败


Ip=$(ip a |grep eno1 |grep inet|awk -F "/" '{ print $1 }' |awk -F " " '{ print $2 }')

Login_ip=$(who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g')


/usr/bin/curl --data-ascii '{

    "touser": "@all",

    "toparty": "@all",

    "msgtype": "text",

    "agentid": "1000004",

    "text": {"content": "'"[ User Login Notification ] \nUserName:[$(whoami)]\nHostName:[`uname -n`]\nDate:[$(date +%F-%T)] \nHost IP:[$Ip] \nLogin IP:[$Login_ip]"'"},

    "safe":"0"

  }' $PURL >/dev/null 2>&1















本文转自偏执与柔情51CTO博客,原文链接: http://blog.51cto.com/lesliecheung/1984131,如需转载请自行联系原作者