最近看了一篇通過nagios實現MD5實時監控iptables狀態的文章,就想是否可以用shell也做到監控iptables規則改變,經過實驗,就有了下面這個腳本.
系統:centos 5.x
腳本內容:
cat check_iptables.sh
復制代碼 代碼如下:
#!/bin/bash
if [ ! -f .count ];then
iptables -L -n|md5sum|awk '{print $1}' > ~/.count
exit 1
else
iptables -L -n|md5sum|awk '{print $1}' >~/1.txt
difffile=`diff ~/.count ~/1.txt|wc -l`
if [[ $difffile = 0 ]];then
echo "file is ok!"
sleep 1
rm -f ~/1.txt
else
echo "file is no ok!"
cat ~/1.txt >~/.count
sleep 1
rm -f ~/1.txt
fi
fi
然后丟到crontab里.以每隔3分鐘檢測一次.
復制代碼 代碼如下:
chmod +x /root/check_iptables.sh
*/3 * * * * /bin/sh /root/check_iptables.sh
當然你也可以加上郵件報警來通知iptables規則有改變.
您可能感興趣的文章:- Shell監控iptables運行狀態
- Shell實現的iptables管理腳本分享
- Shell腳本實現監控iptables運行狀態
- Shell腳本配合iptables屏蔽來自某個國家的IP訪問
- CentOS 7 下使用 iptables