当前位置:网站首页>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/
往期文章回顾:
边栏推荐
- Sword finger offer06 Print linked list from end to end
- 剑指Offer03. 数组中重复的数字【简单】
- Xctf mobile--app3 problem solving
- [data mining review questions]
- Application of ncnn neural network computing framework in orange school orangepi 3 lts development board
- [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
- 【习题五】【数据库原理】
- 【R】【密度聚类、层次聚类、期望最大化聚类】
- GCN thinking - word2vec directly calculates text classification
- Tensorflow binary installation & Failure
猜你喜欢

The upward and downward transformation of polymorphism

Xctf mobile--app3 problem solving

Sword finger offer09 Implementing queues with two stacks

【综合题】【数据库原理】
![Sword finger offer03 Repeated numbers in the array [simple]](/img/cf/c1ad2f2a45560b674b5b8c11fed244.png)
Sword finger offer03 Repeated numbers in the array [simple]

Harmonic current detection based on synchronous coordinate transformation

Drop down refresh conflicts with recyclerview sliding (swiperefreshlayout conflicts with recyclerview sliding)

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

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

GaN图腾柱无桥 Boost PFC(单相)七-PFC占空比前馈
随机推荐
最新版盲盒商城thinkphp+uniapp
记录自己vulnhub闯关记录
01 three solutions to knapsack problem (greedy dynamic programming branch gauge)
Differences between initial, inherit, unset, revert and all
Solve the problem of VI opening files with ^m at the end
公纵号发送提示信息(用户微服务--消息微服务)
Using swift language features, write a pseudo-random number generator casually
C graphical tutorial (Fourth Edition)_ Chapter 18 enumerator and iterator: enumerator samplep340
Pytext training times error: typeerror:__ init__ () got an unexpected keyword argument 'serialized_ options'
[exercise 6] [Database Principle]
[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)
Nodejs+express+mysql realizes login function (including verification code)
Grid connection - Analysis of low voltage ride through and island coexistence
Flinksql can directly create tables and read MySQL or Kafka data on the client side, but how can it automatically flow and calculate?
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
Redhat5 installing socket5 proxy server
十条职场规则
context. Getexternalfilesdir() is compared with the returned path
阿里大于发送短信(用户微服务--消息微服务)
Openstack node address change