shell指令碼 使用者登入伺服器傳送釘釘提醒
阿新 • • 發佈:2021-12-12
一.企業微信配置
1.獲取AgentId(AppID)、Secret 、CropID、部門ID
建立一個企業微信應用獲取到AgentId(AppID)、Secret
2.獲取CropID,點選 “我的企業“ --“企業資訊”
3.獲取部門ID,點選“企業應用”- 建立應用” 對應範圍選擇自己的部門。
二.server 配置
1.微信介面文件
https://work.weixin.qq.com/api/doc/90000/90135/90664
2.微信指令碼,指令碼需要放到/etc/profile.d/ 路徑
#!/bin/bash #企業微信配置 CropID='wwxxxxxxxxxxxxxx' Secret='RauJ_-t-xxxxxxxxxxxxxxxxxxxxxxxxxxxxx' 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=xxxxxxxxxxxuser 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 ]; then Msg=有使用者上線請注意:\n主機名:hostname\n主機ip:ifconfig eht0 | grep inet | awk 'NR==1{ print $2}'\n登入使用者:whoami\n地址來源:$Status /usr/bin/curl -s --data-ascii $(body xxxxx $2) $POSTURL 2>&1 > /dev/null fi
3.釘釘介面文件
https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq/e9d991e2
釘釘指令碼,這裡就不放釘釘申請機器人的步驟了
本文版權歸作者所有,歡迎轉載,請務必新增原文連結。#!/bin/bash ############################### # 2020-3-26 # # # # 通過釘釘介面傳送使用者登入資訊 # ############################### 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 Msg=有使用者上線請注意:\n主機名:hostname\n主機ip:ifconfig eth0 | grep inet | awk 'NR==1{ print $2}'\n登入使用者:whoami\n地址來源:$Status curl ${Dingding_Url} -H 'Content-Type: application/json' -d { 'msgtype': 'text', 'text': {'content': '${Msg}\n'}, 'at': {'atMobiles': ['${user}' ], 'isAtAll': false} } > /dev/null 2>&1 fi