当前位置:网站首页>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/
往期文章回顾:
边栏推荐
- Alibaba is bigger than sending SMS (user microservice - message microservice)
- Analysis of a music player Login Protocol
- How to stand out quickly when you are new to the workplace?
- Sword finger offer10- I. Fibonacci sequence
- A large select drop-down box, village in Chaoyang District
- 基于Linu开发的项目视频
- OpenStack节点地址改变
- C graphical tutorial (Fourth Edition)_ Chapter 15 interface: interfacesamplep268
- Ali & ant self developed IDE
- Detailed explanation of the most complete constraintlayout in history
猜你喜欢

Day 1 of kotlin learning: simple built-in types of kotlin

Xctf mobile--rememberother problem solving

Gan totem column bridgeless boost PFC (single phase) seven PFC duty cycle feedforward

低代码平台国际化多语言(i18n)技术方案

Grid connection - Analysis of low voltage ride through and island coexistence

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

4. 无线体内纳米网:电磁传播模型和传感器部署要点

Application of ncnn Neural Network Computing Framework in Orange Pi 3 Lts Development Board

Solve the problem of VI opening files with ^m at the end

Node.js: express + MySQL的使用
随机推荐
Eureka self protection
Application of ncnn Neural Network Computing Framework in Orange Pi 3 Lts Development Board
C graphical tutorial (Fourth Edition)_ Chapter 15 interface: interfacesamplep271
基于Linu开发的项目视频
如何在微信小程序中获取用户位置?
ORM use of node -serialize
Solve the problem of VI opening files with ^m at the end
C graphical tutorial (Fourth Edition)_ Chapter 17 generic: genericsamplep315
context. Getexternalfilesdir() is compared with the returned path
studio All flavors must now belong to a named flavor dimension. Learn more
阿里 & 蚂蚁自研 IDE
A large select drop-down box, village in Chaoyang District
Huffman coding experiment report
[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
C graphical tutorial (Fourth Edition)_ Chapter 13 entrustment: what is entrustment? P238
Node. Js: use of express + MySQL
并网-低电压穿越与孤岛并存分析
Kotlin notes - popular knowledge points asterisk (*)
Swift5.7 扩展 some 到泛型参数
最新版抽奖盲盒运营版