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 mail@139.com
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的全部信息爲之後比對作準備
相關文章
相關標籤/搜索