-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacket_parser.py
More file actions
35 lines (28 loc) · 1.21 KB
/
packet_parser.py
File metadata and controls
35 lines (28 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Reg Chuhi, Vincent Digiovanni, Julian Moylan
# NSSA 220 Project 2
'''
This file parses the filtered txt file and extracts the time, source IP, destination IP,
packet length, packet type, sequence and time-to-live fields
'''
def parse(filtered_file):
parsed_packets = []
with open(filtered_file, 'r') as f:
packet = f.readline()
while packet:
packet_data = packet.split()
# if len = 10, there is a destination unreachable error
if len(packet_data) != 10:
parsed_packet_data = []
# Extract the time, src_IP, dest_IP, length, packet type, sequence, ttl and add to parsed data
for i in range(1, 12):
if i in (4,6,7,9): continue
if i in (10,11):
parsed_packet_data.append(packet_data[i][4:])
else:
parsed_packet_data.append(packet_data[i])
# Add the formatted data list to the parsed_packets list
parsed_packets.append(parsed_packet_data)
# Read in the next line
packet = f.readline()
# return the parsed_packets
return parsed_packets