shell check system last

linux系统用户登录信息监控
 
请各位读者提出宝贵的意见,谢谢先
 
#!/bin/bash
ifconfig=`cat /etc/sysconfig/network-scripts/ifcfg-eth0 | awk 'NR==4{print $0}' | awk -F '=' '{print $2}'`
##取Host IP Address
lastold=/tmp/last.old
date=`date +%d`
last | awk '{if ($6~'$date') print $0}'| grep -v '^reboot' | grep -v '^wtmp begins' > /tmp/last.new
##过滤出与当前日期匹配的last信息
if [ ! -f $lastold ]; then
        last | grep -v '^reboot' | grep -v '^wtmp begins' > $lastold
##用diff比对的文件,是执行last显示的信息,如果是第一次使用此脚本是不会有结果,大家仔细分析一下,脚本的执行建议crontab -e */1 * * * *这样效果会好点
else
        exit
fi
diff /tmp/last.old /tmp/last.new | grep '>' | sed 's/>//g' > /tmp/last.diff
##last.old与last.new做差异比对并将差异内容重定向到last.diff文件

function send_mail(){
/usr/sbin/sendmail -t <<Endmail
from:last@your domain.com.cn
to:your [email protected]
subject:$ifconfig user login
$lastmail
Endmail
}
##以上定义一个send_mail函数发邮件用
lastmail=`cat /tmp/last.diff | awk '{if ($3!~/172\.16\.[26]\../) print $0}'`
##lastmail的内容正则过滤掉我本身的IP ADDERSS
if [ -z "$lastmail" ]; then
        exit
else
        send_mail
fi
##判断lastmail有没有内容,如果有就证明有非法用户登录系统了,马上会send mail 给你
last | grep -v '^reboot' | grep -v '^wtmp begins' > /tmp/last.old
##取出当前last的所有信息为以后比对做准备
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章