运维木子小李架构师之路
当前位置: shell编程常用案例脚本 > Linux之使用shell脚本实现ssh登录邮件报警

如果你服务器被入侵了,该怎么做才能让自己第一时间知晓?本文将教您实现,服务器被人登录之后第一时间通知你。其实我自己没想到/etc/profile文件的特殊之处 ,进行配置脚本生效source /etc/profile

脚本如下:

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
#!/bin/bash
# auto email
# 2021年5月20日13:56:44
# Email 2911811868@qq.com
# by author limaolin
###################################

if ! rpm -q mailx >/dev/unll;then
    yum install mailx -yum
    cat >>/etc/mail.rc<<-EOF
    set from=2911811686@qq.com                      #设置发送邮件地址
    set smtp=smtp.qq.com                            #设置邮件地址服务器
    set smtp-auth-user=2911811686@qq.com            #设置认证邮件用户
    set smtp-auth-password=sde22dxqqlwxc            #设置认证邮件密码
    EOF
else
    echo Successfuly
fi

if [ ! -f /usr/local/bin/jq ];then
    wget -c http://stedolan.github.io/jq/download/linux64/jq -O /usr/local/bin/jq
    chmod o+x /usr/local/bin/jq
else
    echo Successfuly
fi

curl -s https://www.ip.cn/ip/`ifconfig eth0 |grep broadcast |awk '{print $2}'`.html |grep -E "tab0_address"|awk -F">" '{print "远程服务器地址: " $2}'|sed 's@</div@@g' >/opt/ssh.logs
IP1=`cat /opt/ssh.logs`
IP2=`ifconfig eth0 |grep broadcast |awk '{print "登记远程IP地址: " $2}'`
IP3=`curl -s https://ip.taobao.com/outGetIpInfo?ip=${SSH_CLIENT%% *} |jq .|grep -Eia "country|isp_id|queryIp|city|ip|isp|region_id|region|country_id|city_id"|sed 's/,//g;s/:/=/g;s/"//g'|sed "s#country_id#国家代码#g;s#city_id#市编号#g;s#region_id#省编号#g;s#isp_id#ISP服务商编号#g;s#isp#ISP服务商名称#g;s#city#市名称#g;s#region#省名称#g;s#country#国家#g;s#ip#查询的IP地址#g;s#queryIp#登入IP地址#g"`

echo "$IP1
${IP2}
 
${IP3}"
|mail -s "您服务器有人登录SSH" 2911811686@qq.com

echo /root/auto-mail.v.01.sh >>/etc/profile
source /etc/profile #让脚本生效
如果有黑客破密码解成功第一时间收到报警信息

「梦想一旦被付诸行动,就会变得神圣,如果觉得我的文章对您有用,请帮助本站成长」

分享到:
赞(3) 打赏
多谢大佬的支持

支付宝扫一扫打赏

微信扫一扫打赏

标签:

上一篇:没有了,已经是最后文章

下一篇:

相关推荐

0 条评论关于"Linux之使用shell脚本实现ssh登录邮件报警"

最新评论

    暂无留言哦~~

博客简介

精彩评论

站点统计

  • 文章总数: 29 篇
  • 草稿数目: 0 篇
  • 分类数目: 14 个
  • 独立页面: 0 个
  • 评论总数: 8 条
  • 链接总数: 6 个
  • 标签总数: 12 个
  • 注册用户: 48 人
  • 访问总量: 8,689,294 次
  • 最近更新: 2023年5月21日
服务热线:
 17608504224

 QQ在线交流

 旺旺在线