1.再钉钉群中找到智能群助手
2.选择添加机器人
3.选择自定义机器人
4.按照提示创建机器人。
5.复制登陆的webhook,将会用来发送钉钉消息。
6.在服务器编写shell脚本,因为要实现用户登录就触发脚本,所有脚本可以放在全局变量中,/etc/profile.d/中。
因为curl中传json格式不能有空格,分号等特殊字符。如需美观可以使用XML格式来编写。
vim /etc/profile.d/logininfo.sh
(1)适用于centos6
#!/bin/bash
ip=`ifconfig |grep "inet addr" |awk -F " " '{print $2}'|awk -F ":" '{print $2}'| head -n 1`
shijian=`date +"%Y-%m-%d-%H:%M:%S"`
user=`whoami`
web="刚刚获取到的机器人webhook"
lgIP=`last |head -n 1 |awk -F " " '{print $3}'`
curl "$web" &> /dev/null \
-H 'Content-Type: application/json' \
-d '{"msgtype": "text",
"text": {
"content": "'登陆服务器ip:[${ip}]登陆时间:[${shijian}]登陆用户:[${user}]登陆用户IP:[${lgIP}]'"
}
}'
(2)适用于centos7
#!/bin/bash
ip=`ifconfig |grep "inet" |awk -F " " '{print $2}'|head -n 1`
shijian=`date +"%Y-%m-%d-%H:%M:%S"`
user=`whoami`
lgIP=`last |head -n 1 |awk -F " " '{print $3}'`
web="刚刚获取到的机器人webhook"
curl "$web" &> /dev/null \
-H 'Content-Type: application/json' \
-d '{"msgtype": "text",
"text": {
"content": "'登陆服务器ip:[${ip}]登陆时间:[${shijian}]登陆用户:[${user}]登陆用户IP:[${lgIP}]'"
}
}'
7.保存后刷新全局变量,source /etc/profile就可以实现钉钉发送服务器登录告警了。