Shell腳本實現(xiàn)監(jiān)控iptables規(guī)則是否被修改
更新時間:2014年12月31日 11:33:38 投稿:junjie
這篇文章主要介紹了Shell腳本實現(xiàn)監(jiān)控iptables規(guī)則是否被修改,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下
最近看了一篇通過nagios實現(xiàn)MD5實時監(jiān)控iptables狀態(tài)的文章,就想是否可以用shell也做到監(jiān)控iptables規(guī)則改變,經(jīng)過實驗,就有了下面這個腳本.
系統(tǒng):centos 5.x
腳本內(nèi)容:
cat check_iptables.sh
復(fù)制代碼 代碼如下:
#!/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分鐘檢測一次.
復(fù)制代碼 代碼如下:
chmod +x /root/check_iptables.sh
*/3 * * * * /bin/sh /root/check_iptables.sh
當(dāng)然你也可以加上郵件報警來通知iptables規(guī)則有改變.
相關(guān)文章
Linux shell條件判斷if中的-a到-z的意思【推薦】
這篇文章主要介紹了Linux shell條件判斷if中的-a到-z的意思,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08Shell獲取路徑操作(dirname $0 pwd)的實現(xiàn)
在shell腳本中經(jīng)常會看到$(cd $(dirname $0); pwd)、basename等操作,本文主要介紹了Shell獲取路徑操作(dirname $0 pwd)的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02