前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >shell脚本 微信/钉钉验证登录服务器

shell脚本 微信/钉钉验证登录服务器

原创
作者头像
陈不成i
修改2021-05-31 10:42:59
1.1K0
修改2021-05-31 10:42:59
举报
文章被收录于专栏:ops技术分享ops技术分享

一.简介

登录用户需要二次验证码进行验证

可以配合 监控用户登录,发送通知给企业微信/钉钉 来使用

脚本放到/etc/profile.d/ 目录,登录的时候自动触发

二.微信脚本

1.需要修改CropID、Secret、 local int AppID 、local UserID 、local PartyID 五项内容

  1. #!/bin/bash
  2. #微信配置
  3. CropID='ww022xxxxxxxx'
  4. Secret='RauJ_-t-LxBhfEN7g1shxxxxxxxxxxxx'
  5. APIURL=https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret
  6. TOKEN=$(/usr/bin/curl -s -G $APIURL | awk -F\ '{print $10}')
  7. POSTURL=https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$TOKEN
  8. function body() {
  9. local int AppID=1000004
  10. local UserID=xxxx
  11. local PartyID=2
  12. printf '{\n'
  13. printf '\ttouser: '$UserID\,\n
  14. printf '\ttoparty: '$PartyID\,\n
  15. printf '\tmsgtype: text,\n'
  16. printf '\tagentid: '$AppID\,\n
  17. printf '\ttext: {\n'
  18. printf '\t\tcontent: '$Msg\\n
  19. printf '\t},\n'
  20. printf '\tsafe:0\n'
  21. printf '}\n'
  22. }
  23. Status=who am i | awk '{print $NF}' | sed 's/(//g' | sed 's/)//g'
  24. if [ -n $Status ]; then
  25. Msg=有用户上线请注意:\n主机名:hostname\n主机ip:ifconfig ens33 | grep inet | awk 'NR==1{ print $2}'\n登录用户:whoami\n地址来源:$Status
  26. /usr/bin/curl -s --data-ascii $(body xxxxxx $2) $POSTURL 2>&1 > /dev/null
  27. fi

2.钉钉脚本修改内容 Dingding_Url

  1. #!/bin/bash
  2. ###############################
  3. # 2020-3-26 #
  4. # #
  5. # 通过钉钉接口发送验证码二次验证 #
  6. ###############################
  7. trap 1
  8. read -p 请输入你的钉钉手机号: user
  9. if [ ${#user} -ne 11 ]; then
  10. echo 请出入有效手机号码
  11. sleep 1
  12. logout
  13. fi
  14. #钉钉配置
  15. Dingding_Url=https://oapi.dingtalk.com/robot/send?access_token=
  16. Status=who am i | awk '{print $NF}' | sed 's/(//g' | sed 's/)//g'
  17. if [ -n $Status ]; then
  18. RANDOM=$(date +%s)
  19. echo $RANDOM >/tmp/pass.txt
  20. PASS=tail -n 1 /tmp/pass.txt
  21. Msg=你的验证码是:$PASS
  22. curl ${Dingding_Url} -H 'Content-Type: application/json' -d
  23. {
  24. 'msgtype': 'text',
  25. 'text': {'content': '${Msg}\n'},
  26. 'at': {'atMobiles': [ '${user}' ], 'isAtAll': false}
  27. } > /dev/null 2>&1
  28. trap 2
  29. read -p 请输入验证码: code
  30. if [ $code != xuewenlong ] && [ $code != $PASS ]; then
  31. echo 验证码验证失败!!!
  32. sleep 1
  33. logout
  34. else
  35. echo Welcome to shvm01 System
  36. fi
  37. fi

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一.简介
  • 二.微信脚本
相关产品与服务
命令行工具
腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com