iptables --append(-A)命令允许您添加多个相同的规则,并且您似乎必须运行相同数量的--delete(-D)命令才能再次删除它们.
iptables联机帮助页说--delete命令可以从选定的链中删除一个或多个规则.如何在单个操作中使用--delete命令删除所有匹配的规则?
在一个脚本中,我可以循环调用--delete直到我获得非零退出状态,但这看起来很苛刻.
$ # Add two identical rules.
$ /sbin/iptables --append OUTPUT --protocol tcp --destination example.com --jump DROP
$ /sbin/iptables --append OUTPUT --protocol tcp --destination example.com --jump DROP
$ /sbin/iptables -L OUTPUT -v
Chain OUTPUT (policy ACCEPT 6 packets, 780 bytes)
pkts bytes target prot opt in out source destination
0 0 DROP tcp -- any any anywhere 93.184.216.119
0 0 DROP tcp -- any any anywhere 93.184.216.119
$ # Delete a single rule - can this remove all rules?
$ /sbin/iptables --delete OUTPUT --protocol tcp --destination example.com --jump DROP
$ /sbin/iptables -L OUTPUT -v
Chain OUTPUT (policy ACCEPT 6 packets, 716 bytes)
pkts bytes target prot opt in out source destination
0 0 DROP tcp -- any any anywhere 93.184.216.119
Run Code Online (Sandbox Code Playgroud)
我在Amazon Linux上使用iptables v1.4.18(他们的EC2基本映像)
小智 5
恐怕您不能仅使用 iptables 命令行选项来做到这一点。你可以做的是使用 shell 功能和 xargs:
$ iptables [-t 表] -S [链] | grep [你的模式] | cut -d " " -f 2- | xargs -rL1 iptables [-t table] -D
例如,使用上面的数字:
$ iptables -S 输出 | grep 93.184.216.119 | cut -d " " -f 2- | xargs -rL1 iptables -D
您也可以将它用于不同的表:
$ iptables -t nat -S | grep 93.184.216.119 | cut -d " " -f 2- | xargs -rL1 iptables -t nat -D
我知道这是一个老问题,可能不会对 OP 很有帮助,但也许其他人会偶然发现这个问题。
| 归档时间: |
|
| 查看次数: |
1869 次 |
| 最近记录: |