2013-02-22 2 views
0

데이터 정보가 많은 데이터 파일이 있고 파일에서 IP 시간과 길이를 추출하려고합니다. 나는 시간을 추출 할 수 있지만 길이와 IP를 추출하는 방법을 잘 모르겠습니다. 검색 할 길이 있습니까? 길이가 무엇이든간에 길이를 볼 수 있습니까? 내가 가지고있는 지금까지파이썬에서 특정 라인을 인쇄하여 추출하기

이 계속 데이터 파일의 일부입니다 ...

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 

답변

2

이 작업을 수행하는 한 가지 가능한 방법은 str.translate에게

을 사용하는 것입니다

import sys 
import string 


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

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

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

>>> 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'] 

또 다른 옵션은 Regex를 사용하는 것입니다.

,451,515,
>>> 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) 
로 변경