本文主要是介绍iptables增加和删除防火墙规则,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
shell脚本:
#!/bin/bashusage() {echo "usage(): "echo "xxx_network --add|-a Append rule "echo "xxx_network --delete|-d Delete rule"echo "xxx_network --search|-s Print the rules"
}iptables_add() {local retret=`iptables -S | grep "N xxx_service"`if [ ! -n "${ret}" ];theniptables -N xxx_servicefiret=`iptables -S | grep "A INPUT -j xxx_service"`if [ ! -n "${ret}" ];theniptables -I INPUT 1 -j xxx_servicefiret=`iptables -S | grep "A IN_public -j xxx_service"`if [ ! -n "${ret}" ];theniptables -I IN_public 1 -j xxx_servicefiret=`iptables -S | grep "A xxx_service -p udp -m state --state NEW -m udp --dport 5000 -j ACCEPT"`if [ ! -n "${ret}" ];theniptables -A xxx_service -p udp -m state --state NEW -m udp --dport 5000 -j ACCEPTfiret=`iptables -S | grep "A xxx_service -p tcp -m state --state NEW -m tcp --dport 60000 -j ACCEPT"`if [ ! -n "${ret}" ];theniptables -A xxx_service -p tcp -m state --state NEW -m tcp --dport 60000 -j ACCEPTfi
}iptables_delete() {#INPUT (1,1)INPUT_RET=`iptables -L INPUT -n --line-number | grep xxx_service | sed -n "1, 1p" | awk '{print $1}'`if [ -n "${INPUT_RET}" ];theniptables -D INPUT ${INPUT_RET}fi#IN_public (1,1) IN_PUBLIC_RET=`iptables -L IN_public -n --line-number | grep xxx_service | sed -n "1, 1p" | awk '{print $1}'`if [ -n "${IN_PUBLIC_RET}" ];theniptables -D IN_public ${IN_PUBLIC_RET}fiXXX_SERVICE_RET=`iptables -S | grep "N xxx_service"`if [ "${XXX_SERVICE_RET}" = "-N xxx_service" ];then#xxx_serviceXXX_RET=`iptables -L xxx_service -n --line-number | awk 'END{print NR}'`if [ "${XXX_RET}" -gt 2 ];thenfor i in $(seq 3 ${XXX_RET})doiptables -D xxx_service 1donefi#-X xxx_serviceXXX_SERVICE=`iptables -S | grep "xxx_service"`if [ "${XXX_SERVICE}" = "-N xxx_service" ];theniptables -X xxx_servicefifi
}iptables_search() {iptables -S
}while :; docase "$1" in -a|--add) shiftADD=1break;;-d|--delete)shiftDELETE=1break;;-s|--search)shiftSEARCH=1break;;esac
done
参考网址:
(1) Iptables Essentials: Common Firewall Rules and Commands.
GitHub - trimstray/iptables-essentials: Iptables Essentials: Common Firewall Rules and Commands.
(2)iptables基础知识详解
iptables基础知识详解_Larry的博客-CSDN博客_iptables
这篇关于iptables增加和删除防火墙规则的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!