0

我有一个包含一大堆数据信息的数据文件,我试图从文件中提取 IP 时间和长度。我设法提取时间,但我不确定你如何提取长度和 IP。有没有办法搜索,当它看到长度时,它会打印任何长度?

import sys
import string


text_file = file('MyTraceOutput.txt', "r")

for line in text_file:
    columns = line.split(' ')

    if columns:
        print "Time", columns[0]

我到目前为止所拥有的

这是继续的数据文件的一部分...

reading from file enel573-project1-1-0.pcap, link-type EN10MB (Ethernet)

2.000000 arp who-has 192.168.0.1 (ff:ff:ff:ff:ff:ff) tell 192.168.0.2
2.000023 arp who-has 192.168.0.1 (ff:ff:ff:ff:ff:ff) tell 192.168.0.3
2.000044 arp reply 192.168.0.1 is-at 00:00:00:00:00:01
2.000044 IP (tos 0x0, ttl  64, id 0, offset 0, flags [none], proto: TCP (6), length: 40, bad cksum 0 (->f97c)!) 192.168.0.2.49153 > 192.168.0.1.80: S, cksum 0x0000 (incorrect (-> 0x6e3d), 0:0(0) win 65535
2.000116 arp reply 192.168.0.1 is-at 00:00:00:00:00:01
2.000128 arp who-has 192.168.0.2 (ff:ff:ff:ff:ff:ff) tell 192.168.0.1
2.000128 arp reply 192.168.0.2 is-at 00:00:00:00:00:02
2.000141 IP (tos 0x0, ttl  64, id 0, offset 0, flags [none], proto: TCP (6), length: 40, bad cksum 0 (->f97b)!) 192.168.0.3.49153 > 192.168.0.1.80: S, cksum 0x0000 (incorrect (-> 0x6e3c), 0:0(0) win 65535
2.000152 arp who-has 192.168.0.3 (ff:ff:ff:ff:ff:ff) tell 192.168.0.1
2.000165 arp reply 192.168.0.3 is-at 00:00:00:00:00:03
2.000178 IP (tos 0x0, ttl  64, id 1, offset 0, flags [none], proto: TCP (6), length: 40, bad cksum 0 (->f97a)!) 192.168.0.1.80 > 192.168.0.3.49153: S, cksum 0x0000 (incorrect (-> 0x6e2b), 0:0(0) ack 1 win 65535
2.000189 IP (tos 0x0, ttl  64, id 1, offset 0, flags [none], proto: TCP (6), length: 40, bad cksum 0 (->f97a)!) 192.168.0.3.49153 > 192.168.0.1.80: ., cksum 0x0000 (incorrect (-> 0x6e2c), ack 1 win 65535
2.000202 IP (tos 0x0, ttl  64, id 2, offset 0, flags [none], proto: TCP (6), length: 44, bad cksum 0 (->f975)!) 192.168.0.3.49153 > 192.168.0.1.80: ., cksum 0x0000 (incorrect (-> 0x18d3), 1:5(4) ack 1 win 65535
2.000214 IP (tos 0x0, ttl  64, id 2, offset 0, flags [none], proto: TCP (6), length: 40, bad cksum 0 (->f979)!) 192.168.0.1.80 > 192.168.0.3.49153: ., cksum 0x0000 (incorrect (-> 0x6e28), ack 5 win 65535
2.000253 IP (tos 0x0, ttl  64, id 3, offset 0, flags [none], proto: TCP (6), length: 44, bad cksum 0 (->f974)!) 192.168.0.3.49153 > 192.168.0.1.80: ., cksum 0x0000 (incorrect (-> 0x18cf), 5:9(4
4

1 回答 1

2

一种可能的方法是使用 str.translate

>>> from string import printable, digits
>>> select_set = ''.join(set(printable) - set(digits + '. '))
>>> st = "2.000000 arp who-has 192.168.0.1 (ff:ff:ff:ff:ff:ff) tell 192.168.0.2"
>>> st.translate(None, select_set).split()
['2.000000', '192.168.0.1', '192.168.0.2']

另一种选择是使用正则表达式

>>> import re
>>> re.findall('[\d\.]+',st)
['2.000000', '192.168.0.1', '192.168.0.2']

所以你的程序更改为

import sys
import string


text_file = file('MyTraceOutput.txt', "r")

for line in text_file:
    columns = re.findall('[\d\.]+',line)

    if columns:
        print "Time {}, IP_Range {}-{}".format(*columns)
于 2013-02-22T06:32:11.413 回答