-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsshrc
More file actions
42 lines (33 loc) · 851 Bytes
/
sshrc
File metadata and controls
42 lines (33 loc) · 851 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/bin/bash
#author: testerzhang
#欢迎关注公众号:testerzhang
#获取登录者的用户名
user=$USER
#获取登录者的IP地址
ip=${SSH_CLIENT%% *}
#获取登录的时间
time=$(date +"%Y-%m-%d %H:%M:%S")
outlog="/opt/log/loginlog"
outdir=`dirname $outlog`
server=`hostname`
if [ -z "$ip" ]; then
#echo "ip is empty"
exit 0
fi
emailcontent="$ip在$time时间里$user登录了$server"
if [ ! -d "$outdir" ];then
exit 110
fi
echo $emailcontent >> $outlog
if [ $? -ne 0 ]; then
exit 111
fi
#忽略某些IP
ip_lists="10.10.10.10 10.10.10.11"
if [[ "$ip_lists" =~ "$ip" ]]
then
exit 0
fi
#发送通知,也可以改成发送邮件
timeout 5 curl -o /dev/null -s -XPOST -H "Content-Type: application/json" "http://具体通知的接口地址" \
-d '{ "subject":"'ssh登录"${server}"'","content":"'"${emailcontent}"'" }'