美文网首页
linux用户登陆时获取登录信息到钉钉

linux用户登陆时获取登录信息到钉钉

作者: zorelu | 来源:发表于2018-06-03 17:45 被阅读0次

脚本内容

###使用钉钉机器人接口进行post数据
###使用bash
####添加登陆脚本
###vim .bash_profile
###sh /opt/login.sh
###debian 在.profile  添加要执行的脚本

#!/bin/bash
###接口地址由钉钉机器人获取(直接复制粘贴)
url=https://oapi.dingtalk.com/robot/s*****
##获取登陆用户名
u=$USER
###获取登录时间(无法使用空格)
b=$(date '+%Y-%m-%d,%H:%M:%S')
###获取登录主机命
h=`hostname`
##获取最后登录的ip
userip=`lastlog -u root | awk '{print $3}'|sed -n '2p'`
##echo输出
a=$(echo "当前主机为:$h,当前登录主机ip为:$hip,当前登录用户:$u,当前时间登陆时间:$b,当前登录的用ip:$userip")
#echo $a
##对机器人post数据
c=`curl  -s -H "Content-type: application/json" -X POST -d '{"msgtype":"text","text":{"content":"'$a'"},"at":{"atMobiles":["189xxxx8325","189xxxx8325"]}}' $url`
echo $c,$b >> /var/log/login.log  >/dev/null

钉钉机器人使用

image

传送门

更多post数据类型请查看钉钉机器人帮助文档

极速linux-专注开源

相关文章

网友评论

      本文标题:linux用户登陆时获取登录信息到钉钉

      本文链接:https://www.haomeiwen.com/subject/eoojsftx.html