
正文
shell脚本 微信/钉钉验证登录服务器
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
一.简介
登录用户需要二次验证码进行验证
可以配合 监控用户登录,发送通知给企业微信/钉钉 来使用
脚本放到/etc/profile.d/ 目录,登录的时候自动触发
二.微信脚本
1.需要修改CropID、Secret、 local int AppID 、local UserID 、local PartyID 五项内容
#!/bin/bash
#微信配置
CropID='ww022xxxxxxxx'
Secret='RauJ_-t-LxBhfEN7g1shxxxxxxxxxxxx'
APIURL=https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret
TOKEN=$(/usr/bin/curl -s -G $APIURL | awk -F\ '{print $10}')
POSTURL=https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$TOKEN
function body() {
local int AppID=1000004
local UserID=xxxx
local PartyID=2
printf '{\n'
printf '\ttouser: '$UserID\,\n
printf '\ttoparty: '$PartyID\,\n
printf '\tmsgtype: text,\n'
printf '\tagentid: '$AppID\,\n
printf '\ttext: {\n'
printf '\t\tcontent: '$Msg\\n
printf '\t},\n'
printf '\tsafe:0\n'
printf '}\n'
}Status=who am i | awk '{print $NF}' | sed 's/(//g' | sed 's/)//g'if [ -n $Status ]; thenMsg=有用户上线请注意:\n主机名:hostname\n主机ip:ifconfig ens33 | grep inet | awk 'NR==1{ print $2}'\n登录用户:whoami\n地址来源:$Status
/usr/bin/curl -s --data-ascii $(body xxxxxx $2) $POSTURL 2>&1 > /dev/null
fi

2.钉钉脚本修改内容 Dingding_Url
#!/bin/bash
###############################
# 2020-3-26 #
# #
# 通过钉钉接口发送验证码二次验证 #
###############################
trap 1
read -p 请输入你的钉钉手机号: user
if [ ${#user} -ne 11 ]; then
echo 请出入有效手机号码
sleep 1
logout
fi
#钉钉配置
Dingding_Url=https://oapi.dingtalk.com/robot/send?access_token=
Status=who am i | awk '{print $NF}' | sed 's/(//g' | sed 's/)//g'
if [ -n $Status ]; then
RANDOM=$(date +%s)
echo $RANDOM >/tmp/pass.txt
PASS=tail -n 1 /tmp/pass.txt
Msg=你的验证码是:$PASS
curl ${Dingding_Url} -H 'Content-Type: application/json' -d
{
'msgtype': 'text',
'text': {'content': '${Msg}\n'},
'at': {'atMobiles': [ '${user}' ], 'isAtAll': false}
} > /dev/null 2>&1
trap 2
read -p 请输入验证码: code
if [ $code != xuewenlong ] && [ $code != $PASS ]; then
echo 验证码验证失败!!!
sleep 1
logout
else
echo Welcome to shvm01 System
fi
fi







