linux ssh登录邮件报警

1,准备工作

创建目录:

mkdir /opt/login_warning/

#ssh登录ip白名单

touch /opt/login_warning/WhiteList.txt

#预警邮箱列表

touch /opt/login_warning/EmailList.txt

#登录日志

touch /opt/login_warning/login.log

#安装依赖

cd /opt/login_warning

wget http://stedolan.github.io/jq/download/linux64/jq
chmod 775 jq


2,设置邮箱服务器

vi /etc/mail.rc
末尾添加如下内容:


set from=******@163.com        #指定外部代为发送邮件的邮箱
set smtp=smtp.163.com               #代发送邮件的服务器域名格式:smtp.*
set smtp-auth-user=*******@163.com        #登入邮箱的账户(也是发件人名称)
set smtp-auth-password=******    #登入邮箱的密码(发件人邮箱的密码)
set smtp-auth=login                #登录方式


3,设置登录启动脚本


touch /opt/login_warning/user_login_notice.sh
vi /opt/login_warning/user_login_notice.sh


粘贴脚本内容如下:
-------------------
#!/bin/sh


echo $LANG
export LANG=en_US.UTF-8


ip=`/sbin/ifconfig eth0 | grep "inet addr" | awk '{ print $2}' | awk -F: '{print $2}'`


eval `curl -s "http://ip.taobao.com/service/getIpInfo.php?ip=${SSH_CLIENT%% *}" | /opt/login_warning/jq . | awk -F':|[ ]+|"' '$3~/^(country|area|region|city|isp)$/{print $3"="$7}'`


if [ `grep -c "${SSH_CLIENT%% *}" /opt/login_warning/WhiteList.txt` -eq '0' ]; then
  
while read line


do
    
echo -e "
登入者IP地址:${SSH_CLIENT%% *}\n\
所用用户:$USER\n\
IP归属地:${country}_${area}_${region}_${city}_${isp}\n\
被登录服务器IP:${ip}\n\
时间:`date +'%H:%M:%S %m/%d'`" | mail -s "**服务器异常登录" ${line}
done < /opt/login_warning/EmailList.txt


else
    echo "welcome!"
fi


echo "----------------" >> opt/login_warning/login.log
echo -e "
登入者IP地址:${SSH_CLIENT%% *}\n\
所用用户:$USER\n\
IP归属地:${country}_${area}_${region}_${city}_${isp}\n\
被登录服务器IP:${ip}\n\
时间:`date +'%H:%M:%S %m/%d'`" >> /opt/login_warning/login.log
echo "----------------" >> opt/login_warning/login.log


-------------------


4,设置触警
touch /etc/ssh/sshrc
echo "/opt/login_warning/user_login_notice.sh" >> /etc/ssh/sshrc


5,已知BUG
scp功能似乎受影响





猜你喜欢

转载自blog.csdn.net/a448984863/article/details/80973481