加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 我可以使用iptables –delete命令删除多个匹配规则

发布时间:2020-12-13 19:36:38 所属栏目:Linux 来源:网络整理
导读:iptables append(-A)命令允许您添加多个相同的规则,并且您似乎必须运行相同数量的delete(-D)命令才能再次删除它们. iptables联机帮助页说delete命令可以从选定的链中删除一个或多个规则.如何在单个操作中使用delete命令删除所有匹配的规则? 在一个脚本中,我

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

我在Amazon Linux上使用iptables v1.4.18(他们的EC2基本映像)

最佳答案
我担心你不能只使用iptables命令行选项.你可以做的是使用shell功能和xargs:

$iptables [-t table] -S [chain] | grep [your pattern] | cut -d " " -f 2- | xargs -rL1 iptables [-t table] -D

例如,使用上面的数字:

$iptables -S OUTPUT | 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没有多大帮助,但也许其他人会偶然发现这个问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读