Shell實(shí)現(xiàn)的iptables管理腳本分享
以前的腳本是用save模式,現(xiàn)在把命令附加到一個(gè)文件里面,這樣的話,可以方便的二次修改什么的
腳本基本是這樣的,大家可以跟自己的情況再次修改~ 增加功能什么的。
#!/bin/bash
while true
do
clear
echo "----------------------menu----------------------"
echo "(1) service iptables restart"
echo "(2) iptables add"
echo "(3) iptables delete"
echo "(4) iptables stop"
echo "(5) iptables save(不推薦使用這種模式)"
echo "(6) iptables status"
echo "(7) iptables ACL list"
echo "(0) exit"
echo "會(huì)在當(dāng)前的目錄下生成一個(gè)fw.sh文件"
echo "-------------------------------------------------"
echo -n "enter you chose[0-7]:"
read num
if [ ${num} -lt 0 -o ${num} -gt 7 ]
then
echo "this is not between 0-7"
else
if [ "${num}" == "1" ]
then
service iptables restart&
else
if [ "${num}" == "2" ]
#######################################################
then
while [ "1" == "1" ]
do
clear
echo "----------------------add ACL----------------------"
echo "(1) 針對(duì)源IP放行添加"
echo "(2) 針對(duì)服務(wù)器端口放行添加"
echo "(3) 針對(duì)有端口和服務(wù)的ACL添加(這里要參數(shù)IP和端口 例如 0/0 80)"
echo "(4) 自定義添加"
echo "(5) 退回上一級(jí)"
echo "-------------------------------------------------"
echo -n "enter you chose[0-4]:"
read aclnum
if [ "${aclnum}" == "1" ]
then
read ip
iptables -A INPUT -s ${ip} -p tcp --dport 22 -j ACCEPT
echo "iptables -A INPUT -s ${ip} -p tcp --dport 22 -j ACCEPT" >>fw.sh
# service iptables save
elif [ "${aclnum}" == "2" ]
then
rad ip
iptables -A INPUT -p tcp --dport ${IP} -j ACCEPT
echo "iptables -A INPUT -p tcp --dport ${IP} -j ACCEPT" >>fw.sh
# service iptables save
elif [ "${aclnum}" == "3" ]
then
read ip port
iptables -A INPUT -p tcp -s ${ip} --dport ${port} -j ACCEPT
echo "iptables -A INPUT -p tcp -s ${ip} --dport ${port} -j ACCEPT" >>fw.sh
# service iptables save
elif [ "${aclnum}" == "4" ]
then
read addacl
`${addacl}`
service iptables save
else
break
fi
echo -n "是否想繼續(xù)添加: [y/n]:"
read contine
if [ "${contine}" == "n" -o "${contine}" == "N" ]
then
break
fi
done
#######################################################
else
if [ "${num}" == "3" ]
then
while [ "1" == "1" ]
do
clear
echo "---------------------delete ACL----------------------"
echo "(1) 針對(duì)源ip刪除"
echo "(2) 針對(duì)端口刪除"
echo "(3) 針對(duì)有端口和服務(wù)的ACL刪除"
echo "(4) 自定義刪除"
echo "(5) 退回上一級(jí)"
echo "-------------------------------------------------"
echo -n "enter you chose[0-5]:"
read aclnum
if [ "${aclnum}" == "1" ]
then
read ip
iptables -D INPUT -s ${ip} -p tcp --dport 22 -j ACCEPT
echo "iptables -D INPUT -s ${ip} -p tcp --dport 22 -j ACCEPT" >>fw.sh
# service iptables save
elif [ "${aclnum}" == "2" ]
then
read port
iptables -D INPUT -p tcp --dport ${port} -j ACCEPT
echo "iptables -D INPUT -p tcp --dport ${port} -j ACCEPT" >>fw.sh
# service iptables save
elif [ "${aclnum}" == "3" ]
then
read ip port
iptables -D INPUT -p tcp -s ${ip} --dport ${port} -j ACCEPT
echo "iptables -D INPUT -p tcp -s ${ip} --dport ${port} -j ACCEPT" >>fw.sh
# service iptables save
elif [ "${aclnum}" == "4" ]
then
read deleteacl
`${deleteacl}`
service iptables save
else
break
fi
echo -n "是否想繼續(xù)添加: [y/n]:"
read contine
if [ "${contine}" == "n" -o "${contine}" == "N" ]
then
break
fi
done
###################################################################
else
if [ "${num}" == "4" ]
then
echo -e "`service iptables stop&` "
else
if [ "${num}" == "5" ]
then
echo -e "`service iptables save&`"
else
if [ "${num}" == "6" ]
then
echo -e "`service iptables status&`"
else
if [ "${num}" == "7" ]
then
while [ "1" == "1" ]
do
clear
echo "---------------------list ACL----------------------"
echo "(1) 查看當(dāng)前正在使用的規(guī)則集"
echo "(2) 查看每個(gè)策略或每條規(guī)則、每條鏈的簡(jiǎn)單流量統(tǒng)計(jì)"
echo "(3) 查看NAT表"
echo "(4) 自定義查看"
echo "(5) 退回上一級(jí)"
echo "-------------------------------------------------"
echo -n "enter you chose[0-5]:"
read aclnum
if [ "${aclnum}" == "1" ]
then
iptables -L
elif [ "${aclnum}" == "2" ]
then
iptables -L -n -v
elif [ "${aclnum}" == "3" ]
then
iptables -L -t nat
elif [ "${aclnum}" == "4" ]
then
read listacl
`${listacl}`
else
break
fi
echo -n "是否想繼續(xù)添加: [y/n]:"
read contine
if [ "${contine}" == "n" -o "${contine}" == "N" ]
then
break
fi
done
################################################
else
exit
fi
fi
fi
fi
fi
fi
fi
fi
echo -n "Do you contine [y/n]:"
read contine
if [ "${contine}" == "n" -o "${contine}" == "N" ]
then
exit
fi
done
相關(guān)文章
Linux命令定位與查找之which、whereis和find的用法示例詳解
Linux命令的定位與查找是我們?nèi)粘9ぷ髦斜貍涞募寄?掌握which、whereis和find這三個(gè)命令,可以幫助我們更加高效地進(jìn)行文件搜索和定位工作,這篇文章主要介紹了Linux命令定位與查找:which、whereis和find的用法詳解,需要的朋友可以參考下2023-10-10阿里云主機(jī)一鍵安裝lamp、lnmp環(huán)境的shell腳本分享
這篇文章主要介紹了阿里云主機(jī)一鍵安裝lamp、lnmp環(huán)境的shell腳本分享,需要的朋友可以參考下2014-07-07crond構(gòu)建linux定時(shí)任務(wù)及日志查看腳本詳解
這篇文章主要為大家介紹了crond構(gòu)建linux定時(shí)任務(wù)及日志查看腳本詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10linux每天定時(shí)備份數(shù)據(jù)庫(kù)并刪除十天前數(shù)據(jù)詳細(xì)步驟
每天定時(shí)備份數(shù)據(jù)庫(kù)需要用到Linux的定時(shí)任務(wù),利用Linux的crondtab 命令。下面通過本文給大家?guī)?lái)了linux每天定時(shí)備份數(shù)據(jù)庫(kù)并刪除十天前數(shù)據(jù)詳細(xì)步驟,感興趣的朋友一起看看吧2018-06-06Shell正則表達(dá)式之grep、sed、awk實(shí)操筆記
這篇文章主要介紹了Shell正則表達(dá)式之grep、sed、awk實(shí)操筆記,本文使用grep、sed、awk配合正則達(dá)到了一些需求和目的,需要的朋友可以參考下2014-09-09Shell四種運(yùn)行方式(啟動(dòng)方式)的實(shí)現(xiàn)
這篇文章主要介紹了Shell四種運(yùn)行方式(啟動(dòng)方式)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03