使用shell脚本监控用户登陆服务器并发送提示信息给微信

时间:2018-10-24 22:22:50   收藏:0   阅读:373

1.需要在/etc/ssh/目录下面创建一个名为sshrc的文件,执行权限可给可不给,那么在有人通过ssh远程登录这台服务器的时候,这段脚本就会被执行

#!/bin/bash
###V1-2018-10-24###

CropID=xxxxxxxxxx
Secret=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret"
#Gtoken=$(/usr/bin/curl  $GURL -H "DNT: 1" -H "Accept-Encoding: gzip, deflate" -H "Accept-Language: zh-CN,zh;q=0.8" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 BIDUBrowser/8.1 Safari/537.36" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" -H "X-DevTools-Emulate-Network-Conditions-Client-Id: B546FC80-414C-403F-95F0-EB0F70E58EF7" -H "Connection: keep-alive" -H "Cache-Control: max-age=0" --compressed | awk -F \" ‘{print $4}‘)
Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F\" ‘{print $10}‘)
PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken"
function body() {
        local int appId=1000002
        #local userId=$1
        local userId="LinYouYi"
        local partyId=2
        local msg=有用户上线请注意:\n主机名:`hostname`\n主机ip:`curl ifconfig.me`\n登录用户:`whoami`\n登录时间:`date`
        printf {\n
        printf \t"touser":"‘"$userId"\"",\n"
        printf \t"toparty":"‘"$partyId"\"",\n"
        printf \t"msgtype": "text",‘"\n"
        printf \t"agentid":"‘"$appId"\"",\n"
        printf \t"text":{\n
        printf \t\t"content":"‘"$msg"\"
        printf \n\t},\n
        printf \t"safe":"0"\n
        printf }\n
}
#/usr/bin/curl --data-ascii "$(body $1)" $PURL
/usr/bin/curl --data-ascii "$(body)" $PURL

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!