I have a file containing a previous tcpdump, so the file has lines in this format:
17:20:03.867998 IP 23.222.75.204.443 > 192.168.0.15.51773: Flags [.], ack 518, win 916, options [nop,nop,TS val 303057114 ecr 43022775], length 0
17:20:03.870231 IP 209.148.192.43.80 > 192.168.0.15.60174: Flags [.], seq 1:1449, ack 511, win 486, options [nop,nop,TS val 1840008838 ecr 43022779], length 1448
My function simply extracts specific strings in each line (the source and destination addresses) and prints them. The strange thing is that it works (everything that should print does) but in the end I get an error.
Here's my code:
def parse_file() :
try :
file_object = open("tcp_dump","r")
for x in file_object.readlines() :
source_ip=x.split("IP ")[1].split(" >")[0]
dest_ip=x.split("> ")[1].split(": Flags")[0]
print(source_ip)
print(dest_ip)
file_object.close()
except IOError :
print("The specified file could not be found/accessed")
parse_file()
Here's the output:
23.222.75.204.443
192.168.0.15.51773
209.148.192.43.80
192.168.0.15.60174
Traceback (most recent call last):
File "./test", line 26, in <module>
parse_file()
File "./test", line 15, in parse_file
source_ip=x.split("IP ")[1].split(" >")[0]
IndexError: list index out of range