Skip to content

Instantly share code, notes, and snippets.

@oxnz
Created September 21, 2013 16:17
Show Gist options
  • Save oxnz/6651947 to your computer and use it in GitHub Desktop.
Save oxnz/6651947 to your computer and use it in GitHub Desktop.
python pcap dispatcher
#!/usr/bin/env python
#coding=utf-8
import struct
import sys
fp = open(sys.argv[1], 'rb')
data = fp.read()
pcap_header = {}
pcap_header['magic_number'] = data[0:4]
pcap_header['version_major'] = data[4:6]
pcap_header['version_minor'] = data[6:8]
pcap_header['thiszone'] = data[8:12]
pcap_header['sigfigs'] = data[12:16]
pcap_header['snaplen'] = data[16:20]
pcap_header['linktype'] = data[20:24]
print '==============='
for key in ['magic_number', 'version_major', 'version_minor', 'thiszone',
'sigfigs', 'snaplen', 'linktype']:
print key + ':' + repr(pcap_header[key])
print '---------------'
step = 0
packet_num = 0
packet_data = []
pcap_packet_header = {}
i = 24
while (i < len(data)):
pcap_packet_header['GMTtime'] = data[i:i+4]
pcap_packet_header['MicroTime'] = data[i+4:i+8]
pcap_packet_header['caplen'] = data[i+8:i+12]
pcap_packet_header['len'] = data[i+12:i+16]
packet_len = struct.unpack('I', pcap_packet_header['len'])[0]
packet_data.append(data[i+16:i+16+packet_len])
i = i + packet_len + 16
packet_num += 1
for i in range(packet_num):
print "packet " + str(i) + ':'
for key in ['GMTtime', 'MicroTime', 'caplen', 'len']:
print key + ':' + repr(pcap_packet_header[key])
print 'data:' + repr(packet_data[i])
print 'Total packet: ' + str(packet_num)
fp.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment