iptables进行stream量监控

我在IPTABLES中有这样的规则:

-A INPUT -s 166.100.102.50/32 -j LOG – 日志级别7

我写了一个脚本来抓取这些规则的输出,并将IP的字节输出到我的服务器。

我希望得到有关如何创build一个跟踪分散子网IPstream量的规则的build议。 IP地址不固定,甚至子网不固定。 例如:

120.2.33.45可能是一天的设备的IP地址,204.65.3.88可能是第二天同一设备的IP地址。

我认为,如果有一种方法来编写规则,以便它给我的IP地址的一切,除了一个固定的IP地址范围,如166.100.102.50,那么我会没事的。

就像是:

-INPUT -s 不等于 166.100.102.50/32 -j LOG – 日志级别7

提前致谢

你想(看! ):

 iptables -A INPUT ! -s 166.100.102.50/32 -j LOG --log-level 7 

这将匹配源地址不是166.100.102.50的所有内容。

man iptables

  [!] -s, --source address[/mask][,...] Source specification. Address can be either a network name, a hostname, a network IP address (with /mask), or a plain IP address. Hostnames will be resolved once only, before the rule is submitted to the kernel. Please note that specifying any name to be resolved with a remote query such as DNS is a really bad idea. The mask can be either a network mask or a plain num‐ ber, specifying the number of 1's at the left side of the net‐ work mask. Thus, a mask of 24 is equivalent to 255.255.255.0. 

这里开始相关部分:

  A "!" argument before the address specification inverts the sense of the address. The flag --src is an alias for this option. Multiple addresses can be specified, but this will expand to multiple rules (when adding with -A), or will cause multiple rules to be deleted (with -D). 

你可能会发现,在这里build立一个链条将使事情变得更容易。

链基本上就像一个子表。 你发送东西给它,然后你可以返回或处理链中的东西。

 -t INPUT -N LOGME # return stuff, that we don't want to handle -A LOGME -s 166.100.102.50/32 -j RETURN -A LOGME -s 192.168.27.0/24 -j RETURN # log everything that hasn't been returned -A LOGME -j LOG --log-level 7 

另一个select,可能是创build和使用一个ipset ,它基本上允许您build立一组地址,然后您可以使用--match-set选项在规则中引用它。