如何使用iptables来统计来自外部的数据包?

如果你不给iptables一个目标,它只是计算有多less字节和数据包符合规则。 我想统计所有来自外部的数据包,即不匹配10.0.0.0/8,不匹配192.168.0.0/16。

我的第一个直觉是:

iptables -A INPUT ! -s 10.0.0.0/8,192.168.0.0/16 

然而,这增加了两个规则,其中每个规则分别计数,并且外部分组的计数是不可察觉的。 那么如何计算外部数据包?

我会试图通过创build一个链来做这样的事情。 然后添加规则到链中,以返回你不想要的东西。 持续到最终的链条的东西将是你想要的东西。

 # untested # create a new input counter chain /sbin/iptables -t filter -N inputcounter # redirect all traffic to chain /sbin/iptables -t filter -A INPUT -j inputcounter # return stuff we don't want to count /sbin/iptables -t filter -A inputcounter -s 10.0.0.0/8 -j RETURN /sbin/iptables -t filter -A inputcounter -s 192.168.0.0/16 -j RETURN # Final rule will return everything else. This should be a count of # everything that wasn't not previously excluded /sbin/iptables -t filter -A inputcounter -j RETURN