您好,登錄后才能下訂單哦!
這篇文章主要講解了“Shell實現的iptables管理腳本的方法教程”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Shell實現的iptables管理腳本的方法教程”吧!
代碼如下:
#!/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 "會在當前的目錄下生成一個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) 針對源IP放行添加"
echo "(2) 針對服務器端口放行添加"
echo "(3) 針對有端口和服務的ACL添加(這里要參數IP和端口 例如 0/0 80)"
echo "(4) 自定義添加"
echo "(5) 退回上一級"
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 "是否想繼續添加: [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) 針對源ip刪除"
echo "(2) 針對端口刪除"
echo "(3) 針對有端口和服務的ACL刪除"
echo "(4) 自定義刪除"
echo "(5) 退回上一級"
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 "是否想繼續添加: [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) 查看當前正在使用的規則集"
echo "(2) 查看每個策略或每條規則、每條鏈的簡單流量統計"
echo "(3) 查看NAT表"
echo "(4) 自定義查看"
echo "(5) 退回上一級"
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 "是否想繼續添加: [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
感謝各位的閱讀,以上就是“Shell實現的iptables管理腳本的方法教程”的內容了,經過本文的學習后,相信大家對Shell實現的iptables管理腳本的方法教程這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。