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

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='有用户上线请注意:
主机名:'`hostname`'
主机ip:'`curl ifconfig.me`'
登录用户:'`whoami`'
登录时间:'`date`
        printf '{
'
        printf '	"touser":"'"$userId""",
"
        printf '	"toparty":"'"$partyId""",
"
        printf '	"msgtype": "text",'"
"
        printf '	"agentid":"'"$appId""",
"
        printf '	"text":{
'
        printf '		"content":"'"$msg""
        printf '
	},
'
        printf '	"safe":"0"
'
        printf '}
'
}
#/usr/bin/curl --data-ascii "$(body $1)" $PURL
/usr/bin/curl --data-ascii "$(body)" $PURL
原文地址:https://www.cnblogs.com/linyouyi/p/9845843.html