以前の tcpdump を含むファイルがあるため、ファイルには次の形式の行があります。
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
私の関数は、各行の特定の文字列 (送信元アドレスと宛先アドレス) を抽出して出力するだけです。奇妙なことは、それが機能することです(印刷する必要があるものはすべて機能します)が、最終的にエラーが発生します。
これが私のコードです:
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()
出力は次のとおりです。
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