当前位置:网站首页>SSH login server sends a reminder

SSH login server sends a reminder

2022-07-03 13:02:00 Programmer spicy hot

When someone logs in to the server , How to perceive ? This article talks about when someone passes SSH After logging in to the server , Send login information to wechat enterprise number .

shell

First create shell Script , Used to obtain login information .

#!/bin/bash
# Get the login user name 
#user=$USER
user=$(getent passwd `who` | head -n 1 | cut -d : -f 1)
if [ "" = "$user" ]; then
   user="default"
fi
# Get the login's IP Address 
ip=${SSH_CLIENT%% *}
echo $ip
if [ "$ip" = "" ]; then
   ip="default"
fi
# Get the login time 
time=$(date +%F-%k:%M)
# Server's IP Address 
server=`ifconfig eth1|sed -n '2p'|awk -F ":" '{print $2}'|awk '{print $1}'`
if [ "$server" = "" ]; then
   server="default"
fi
python /etc/ssh/a.py $user $ip $time $server

python

establish python Code , Used to send information to the enterprise number . above shell The script calls the python Program . The information of the enterprise number is filled in init in . If you don't want to use an enterprise number , You can also use email . In the past, the most convenient way was to use sugar directly , But it was ruined by everyone .

##  Wechat push script 

import requests
import json
import sys

class Wechat_Info():
    """  WeChat push  """

    def __init__(self):

        self.partyID = '1'
        self.corpID = '**'
        self.secret = '**'
        self.agentID = '**'

    def __get_token(self, corpid, secret):

        Url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
        Data = {
    
            "corpid": corpid,
            "corpsecret": secret
        }
        r = requests.get(url=Url, params=Data)
        token = r.json()['access_token']

        return token

    def send_message(self, message, messagetype): # text textcard markdown

        token = self.__get_token(self.corpID, self.secret)

        url = f"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={
      token}"

        data = {
    
            "toparty": self.partyID,
            "msgtype": messagetype,
            "agentid": self.agentID,
            messagetype: {
    
                "content": message
            },
            "safe": "0"
        }

        result = requests.post(url=url, data=json.dumps(data))

        return result.text

    def send_file(self, path, filetype):   # image, vioce, video, file

        token = self.__get_token(self.corpID, self.secret)

        post_url = f"https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={
      token}&type={
      filetype}"
        data = {
    "media": open(path, 'rb')}

        r = requests.post(url=post_url, files=data)
        media_id = r.json()['media_id']

        url = f"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={
      token}"

        data = {
    
            "toparty": self.partyID,
            "msgtype": filetype,
            "agentid": self.agentID,
            filetype : {
    
                "media_id" : media_id
            },
            "safe": "0"
        }

        result = requests.post(url=url, data=json.dumps(data))

        return result.text

if __name__ == '__main__':
    user = sys.argv[1]
    ip = sys.argv[2]
    time = sys.argv[3]
    server = sys.argv[4]
    msg = f" user {
      user} stay {
      time} Sign in {
      ip} The service is {
      server}"
    print(msg)

    wechat_info = Wechat_Info()
    result = wechat_info.send_message(msg,"text")
    print(result)

operation

System level

Put the above sh The file is named sshrc, take sh Document and python Put the file on the system /etc/ssh/ Under the table of contents .

At the user level

If you only focus on the specified user , You can put the file in the following directory :

  • Linux User login will execute /etc/profile file

  • Ubuntu/Debian System environment Edit the root directory ~/.bashrc file

  • CentOS System edit ~/.bash_profile file

Add code :

sh shell file name 

Information

  1. CentOS Turn on SSH Wechat message push after login

  2. 【CentOS】 Linux 7.4.1708 (Core) The system sends an email reminder when logging in to the server remotely

  3. linux shell The script assigns the command execution result to the variable

Last

If you like my article , You can pay attention to my official account. ( Hot programmer )

My personal blog is :https://shidawuhen.github.io/

Review of previous articles :

  1. Design patterns

  2. Recruitment

  3. reflection

  4. Storage

  5. Algorithm series

  6. Reading notes

  7. Gadget

  8. framework

  9. The Internet

  10. Go Language

原网站

版权声明
本文为[Programmer spicy hot]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/184/202207031204514441.html