如何获得准确的iptables规则?

我在iptables几个链中有很多规则。 什么是可靠的方法来获得每桌的所有规则的准确计数?

iptables --list --line-numbers打印这样的东西:

 Chain INPUT (policy ACCEPT) num target prot opt source destination 1 cali-INPUT all -- anywhere anywhere /* cali:Cz_u1IQiXIMmKD4c */ 2 KUBE-FIREWALL all -- anywhere anywhere Chain FORWARD (policy ACCEPT) num target prot opt source destination 1 cali-FORWARD all -- anywhere anywhere /* cali:wUHhoiAYhphO9Mso */ 2 DOCKER-ISOLATION all -- anywhere anywhere 3 DOCKER all -- anywhere anywhere Chain KUBE-SERVICES (1 references) num target prot opt source destination 1 REJECT tcp -- anywhere 10.17.6.000 /* ephemeral-1498158084 reject-with icmp-port-unreachable …. 

获取输出的行数并不能提供正确的规则数量。 有空行,标题和标题。 寻找一种方法来获得规则计数。

在你的shell中运行这个命令:

 iptables --list --line-numbers | sed '/^num\|^$\|^Chain/d' | wc -l 

描述:

得到名单

iptables –list – 行号

忽略以numchain和whitelines开头的行

sed'/ ^ num \ | ^ $ \ | ^ Chain / d'

计算结果行

wc -l

你可以使用这个脚本 。 看起来它应该解决你的问题。