ssh 登录服务器触发钉钉机器人
#!/bin/bash
setopt no_nomatch
#获取相关登陆信息
name=`hostname`
ip=$(ip addr | grep inet | grep -v inet6 | sed -n 2p | awk '{print $2}' |awk -F "/" '{print $1}')
user=$USER
_ip=${SSH_CLIENT%% *}
_time=`date '+%Y-%m-%d-%H:%M:%S'`
# #查询登陆地址并返回相应信息
ipaddress=`curl -q -s https://www.36ip.cn/?ip=$_ip`
addr=`echo $ipaddress | sed '/ \+/s//|/g'`
echo $ip $user $name $addr
#自定义接入钉钉函数
function SendMessageToDingding(){
#钉钉机器人接口地址
url="https://oapi.dingtalk.com/robot/send?access_token=xxxx"
#推送到钉钉
res=`curl -X POST -s -L -H "Content-Type:application/json" -H "charset:utf-8" $url -d "
{
\"msgtype\": \"text\",
\"text\": {
\"content\": \" $1\n 主机名称:$2\n 主机 IP:$3\n 登录用户:$4\n 登录 IP:$5\n IP 归属地:$6\n 登录时间:$7 \"
}
}"`
#显示推送结果
# echo $res
}
body="服务器 SSH 登录提示"
#SendMessageToDingding $subject $body
#调用函数
SendMessageToDingding $body $name $ip $user $_ip $addr $_time