一直在使用python解析iptables输出以获得每个源IP的统计信息。不想包括iptc模块。 所以只是做字符串操作。你知道吗
bytes target prot opt in out source destination
0 0 udp -- * eth1 10.10.10.10 0.0.0.0/0 udp spt:10
0 0 tcp -- * eth1 10.10.10.10 0.0.0.0/0 tcp spt:10
0 0 all -- * eth1 1.1.1.1 0.0.0.0/0
0 0 all -- * eth1 0.0.0.0/0 0.0.0.0/0 source IP range 5.5.5.5-5.5.5.10
0 0 all -- * eth1 0.0.0.0/0 0.0.0.0/0 source IP range 4.4.4.4-4.4.4.5
0 0 all -- * eth1 0.0.0.0/0 0.0.0.0/0
>>> s=' 0 0 udp -- * eth1 10.10.10.10 0.0.0.0/0 udp spt:10 '
>>> s.split()
['0', '0', 'udp', '--', '*', 'eth1', '10.10.10.10', '0.0.0.0/0', 'udp', 'spt:10']
要按如下方式解析,如何将单词之间除了单个空格以外的所有空格分隔开?你知道吗
expected_output=['0', '0', 'udp', '--', '*', 'eth1', '10.10.10.10', '0.0.0.0/0', 'udp spt:10']
或者有更好的方法从iptables获取数据,但是没有iptc?你知道吗
对于iptables输出,不能保证所有列都被多个空格隔开。它只保证至少有一个空间。下面是该命令输出的一个片段(在我的一台计算机上运行时),您可以看到:
幸运的是,前9列中的值将没有任何空格。因此,您可以对每行执行以下操作,以获得所需的数据:
相关问题 更多 >
编程相关推荐