通過企業微信機器人實現ssh登陸傳送ip警告
阿新 • • 發佈:2020-08-28
因實際網路環境複雜,直接新增ssh白名單風險較大,使用sshrc檔案呼叫企業微信api實現警告白名單。
$ chmod a+x /etc/ssh/sshrc
$ cat /etc/ssh/sshrc
#!/bin/bash #登入使用者 user=$USER #發起登陸ip ip=${SSH_CLIENT%% *} #時間 time=$(date +%F%t%k:%M) #伺服器名 server=$HOSTNAME #判斷如果登入者的IP地址不是指定的IP,則發郵件報警 if [ "$ip" != "12.123.12.12" ] && [ "$ip" != "12.34.12.24" ];then echo -e "被登陸節點:$server \n登陸時間:$time \n使用者:$user \n發起登陸 ip:$ip \n" > /tmp/msg #也可以發郵件 # echo -e "time:$time \nuser:$user \nlogin ip:$ip \n"|mail -s "server:$server" [email protected] #呼叫給機器人推訊息的指令碼 python2.7 /tmp/sent_ssh.py > /dev/null 2>&1 fi
下面是推訊息的指令碼程式碼:
$ cat /tmp/sent_ssh.py
## coding: utf-8 # import sys import os import smtplib import pandas as pd import pymysql import requests p=os.popen('cat /tmp/msg') x=p.read() print(x) if not x.strip(): print 'x is null' else: url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxxxxxxxxxxx" headers = {"Content-Type": "text/plain"} data = { "msgtype": "text", "text": { "content": x, } } r = requests.post(url, headers=headers, json=data) print(r.text)
登陸實現效果: