创建sshrc文件

/etc/ssh路径下创建sshrc文件

#!/bin/bash
user=$USER
ip=${SSH_CLIENT%% *}
hostname=$(hostname)

# 调用告警脚本
/root/anaconda3/bin/python3 /etc/ssh/SendMsg.py   "$user" "$ip" "$hostname"

Python脚本

import time
import requests
import json
import sys

# 钉钉token
token = "21c469xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"


def send_msg(duser, dip, dhostname):
    urls = "https://oapi.dingtalk.com/robot/send?access_token=" + token
    headers = {
        "Content-Type": "application/json",
        "Charset": "UTF-8"
    }
  
    md = "## **<font color=\"#FFD433\">SSH登录提醒</font>**\n" \
    "#### 主机: **[ " + dhostname + " ]**\n" \
    "#### IP地址: " + dip + "\n" \
    "#### 登录用户: **<font color=\"#FF0000\">[ "+duser +"]</font>**\n" \
    "#### 登录时间: " + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "\n" \
  
  
    data = {
        "msgtype": "markdown",
        "markdown": {
            "title": " ["+ dhostname +" & "+duser+"] - SSH登录!",
            "text":md
        },
        "at": {
            "isAtAll": "true"
        }
    }
    requests.post(url=urls, data=json.dumps(data), headers=headers)
  
    if __name__ == "__main__":
        send_msg(sys.argv[1], sys.argv[2], sys.argv[3])
最后修改:2023 年 10 月 16 日
如果觉得我的文章对你有用,请随意赞赏