当前位置:网站首页>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
CentOS Turn on SSH Wechat message push after login
【CentOS】 Linux 7.4.1708 (Core) The system sends an email reminder when logging in to the server remotely
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 :
边栏推荐
- 【习题七】【数据库原理】
- Dix règles de travail
- The latest version of blind box mall thinkphp+uniapp
- Enter the length of three sides of the triangle through the user, and calculate the area of the triangle, where the length is a real number
- 2022-01-27 research on the minimum number of redis partitions
- Idea full text search shortcut ctr+shift+f failure problem
- Simple use and precautions of kotlin's array array and set list
- context. Getexternalfilesdir() is compared with the returned path
- [exercise 7] [Database Principle]
- (最新版) Wifi分销多开版+安装框架
猜你喜欢

(latest version) WiFi distribution multi format + installation framework

自抗扰控制器七-二阶 LADRC-PLL 结构设计

Powerful avatar making artifact wechat applet

ncnn神经网络计算框架在香橙派OrangePi 3 LTS开发板中的使用介绍

T430 toss and install OS majave 10.14

强大的头像制作神器微信小程序

Xctf mobile--app2 problem solving

Analysis of the influence of voltage loop on PFC system performance

对业务的一些思考

解决 System has not been booted with systemd as init system (PID 1). Can‘t operate.
随机推荐
【综合题】【数据库原理】
C graphical tutorial (Fourth Edition)_ Chapter 13 entrustment: what is entrustment? P238
强大的头像制作神器微信小程序
[data mining review questions]
自抗扰控制器七-二阶 LADRC-PLL 结构设计
[exercice 7] [principe de la base de données]
Loan calculator my pressure is high
Export the entire Oracle Database
How to stand out quickly when you are new to the workplace?
[exercise 5] [Database Principle]
Method overloading and rewriting
剑指 Offer 14- II. 剪绳子 II
(最新版) Wifi分销多开版+安装框架
sitesCMS v3.0.2发布,升级JFinal等依赖
【Colab】【使用外部数据的7种方法】
[combinatorics] permutation and combination (the combination number of multiple sets | the repetition of all elements is greater than the combination number | the derivation of the combination number
[exercise 7] [Database Principle]
Image component in ETS development mode of openharmony application development
Swift5.7 extend some to generic parameters
C graphical tutorial (Fourth Edition)_ Chapter 20 asynchronous programming: examples - cases without asynchronous