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的所有信息爲以後比對做準備
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章