当前位置:网站首页>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/
往期文章回顾:
边栏推荐
- The foreground uses RSA asymmetric security to encrypt user information
- node的ORM使用-Sequelize
- 电压环对 PFC 系统性能影响分析
- 【判断题】【简答题】【数据库原理】
- [combinatorics] permutation and combination (multiple set permutation | multiple set full permutation | multiple set incomplete permutation all elements have a repetition greater than the permutation
- Everything comes to him who waits
- idea将web项目打包成war包并部署到服务器上运行
- C graphical tutorial (Fourth Edition)_ Chapter 17 generic: genericsamplep315
- Using swift language features, write a pseudo-random number generator casually
- 4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment
猜你喜欢

最新版盲盒商城thinkphp+uniapp

【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素

The latest version of lottery blind box operation version

Ali & ant self developed IDE

The latest version of blind box mall thinkphp+uniapp

Sword finger offer09 Implementing queues with two stacks

T430 toss and install OS majave 10.14

Sword finger offer06 Print linked list from end to end

【R】【密度聚类、层次聚类、期望最大化聚类】

Sword finger offer07 Rebuild binary tree
随机推荐
Cache penetration and bloom filter
【数据库原理复习题】
Redhat5 installing socket5 proxy server
initial、inherit、unset、revert和all的区别
电压环对 PFC 系统性能影响分析
[network counting] Chapter 3 data link layer (2) flow control and reliable transmission, stop waiting protocol, backward n frame protocol (GBN), selective retransmission protocol (SR)
01 three solutions to knapsack problem (greedy dynamic programming branch gauge)
Define a list, store n integers, and calculate the length, maximum value, minimum value and average value of the list
【習題五】【數據庫原理】
[exercise 7] [Database Principle]
ncnn神經網絡計算框架在香柳丁派OrangePi 3 LTS開發板中的使用介紹
[problem exploration and solution of one or more filters or listeners failing to start]
The best shortcut is no shortcut
Everything comes to him who waits
剑指Offer03. 数组中重复的数字【简单】
Togaf certification self-study classic v2.0
【ManageEngine】IP地址扫描的作用
Do you feel like you've learned something and forgotten it?
Record your vulnhub breakthrough record
十条职场规则