当前位置:网站首页>SSH登录服务器发送提醒
SSH登录服务器发送提醒
2022-07-03 12:05:00 【程序员麻辣烫】
当有人登录服务器后,如何感知?本文讲述当有人通过SSH登录服务器后,将登录信息发送到微信企业号。
shell
先创建shell脚本,用于获取登录信息。
#!/bin/bash
#获取登录者的用户名
#user=$USER
user=$(getent passwd `who` | head -n 1 | cut -d : -f 1)
if [ "" = "$user" ]; then
user="default"
fi
#获取登录者的IP地址
ip=${SSH_CLIENT%% *}
echo $ip
if [ "$ip" = "" ]; then
ip="default"
fi
#获取登录的时间
time=$(date +%F-%k:%M)
#服务器的IP地址
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
创建python代码,用于将信息发送到企业号上。上面的shell脚本调用该python程序。企业号的信息填充到init中。如果不想用企业号,也可以使用邮件。以前最方便的是直接使用方糖,但被大家玩坏了。
## 微信推送脚本
import requests
import json
import sys
class Wechat_Info():
""" 微信推送 """
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}在{
time}登录{
ip}服务为{
server}"
print(msg)
wechat_info = Wechat_Info()
result = wechat_info.send_message(msg,"text")
print(result)
操作
系统层面
将上述sh文件命名为sshrc,将sh文件和python文件放到系统的/etc/ssh/ 目录下。
用户层面
如果只关注指定用户,可将文件放到如下目录:
Linux用户登陆都会执行/etc/profile文件
Ubuntu/Debian系统环境 编辑根目录下 ~/.bashrc文件
CentOS系统 编辑 ~/.bash_profile文件
添加代码:
sh shell文件名
资料
CentOS开启SSH登录后的微信消息推送
【CentOS】 Linux 7.4.1708 (Core)系统远程登陆服务器时发送邮件提醒
linux shell脚本将命令执行结果赋值给变量
最后
大家如果喜欢我的文章,可以关注我的公众号(程序员麻辣烫)
我的个人博客为:https://shidawuhen.github.io/
往期文章回顾:
边栏推荐
- Differences and connections between final and static
- Ali & ant self developed IDE
- Attack and defense world mobile--ph0en1x-100
- Flinksql can directly create tables and read MySQL or Kafka data on the client side, but how can it automatically flow and calculate?
- C graphical tutorial (Fourth Edition)_ Chapter 15 interface: interfacesamplep268
- How to stand out quickly when you are new to the workplace?
- C graphical tutorial (Fourth Edition)_ Chapter 15 interface: interfacesamplep271
- Do you feel like you've learned something and forgotten it?
- 并网-低电压穿越与孤岛并存分析
- 【習題七】【數據庫原理】
猜你喜欢

(最新版) Wifi分销多开版+安装框架

Deeply understand the mvcc mechanism of MySQL

With pictures and texts, summarize the basic review of C language in detail, so that all kinds of knowledge points are clear at a glance?

Node. Js: use of express + MySQL

Method overloading and rewriting

Sword finger offer09 Implementing queues with two stacks

How to get user location in wechat applet?
![[comprehensive question] [Database Principle]](/img/d7/8c51306bb390e0383a017d9097e1e5.png)
[comprehensive question] [Database Principle]
![[review questions of database principles]](/img/c3/81d192a40bcc4f5d72fcbe76c708bb.png)
[review questions of database principles]

01 three solutions to knapsack problem (greedy dynamic programming branch gauge)
随机推荐
C graphical tutorial (Fourth Edition)_ Chapter 13 entrustment: delegatesamplep245
最新版抽奖盲盒运营版
[exercise 5] [Database Principle]
ORM use of node -serialize
GCN thinking - word2vec directly calculates text classification
Loan calculator my pressure is high
Seven second order ladrc-pll structure design of active disturbance rejection controller
Glide question you cannot start a load for a destroyed activity
The latest version of lottery blind box operation version
双链笔记·思源笔记综合评测:优点、缺点、评价
【综合题】【数据库原理】
Exploration of sqoop1.4.4 native incremental import feature
Xctf mobile--app1 problem solving
Pytext training times error: typeerror:__ init__ () got an unexpected keyword argument 'serialized_ options'
Deeply understand the mvcc mechanism of MySQL
Project video based on Linu development
Cache penetration and bloom filter
【习题七】【数据库原理】
【数据库原理及应用教程(第4版|微课版)陈志泊】【SQLServer2012综合练习】
The upward and downward transformation of polymorphism