Skip to content

Instantly share code, notes, and snippets.

@kbandla
Last active February 8, 2016 17:00
Show Gist options
  • Save kbandla/baf7ca47429fd6c53696 to your computer and use it in GitHub Desktop.
Save kbandla/baf7ca47429fd6c53696 to your computer and use it in GitHub Desktop.
( 20bf086a7f742685709a286914bd86e8 )
import dpkt
from dpkt.ip import IP
from dpkt.ethernet import Ethernet
import struct
import socket
import csv
def ip_to_str(address):
return socket.inet_ntoa(address)
f = open('sample.pcap', 'rb')
pcap = dpkt.pcap.Reader(f)
c = csv.writer(open("a.csv", "wb"))
for ts, buf in pcap:
eth = dpkt.ethernet.Ethernet(buf)
if eth.type != dpkt.ethernet.ETH_TYPE_IP:
continue
ip = eth.data
do_not_fragment = bool(dpkt.ip.IP_DF)
more_fragments = bool(dpkt.ip.IP_MF)
fragment_offset = bool(dpkt.ip.IP_OFFMASK)
Source = "%s" % ip_to_str(ip.src)
Destination = "%s" % ip_to_str(ip.dst)
Length = "%d" % (ip.len)
TTL = "%d" % (ip.ttl)
OFF = ip.off
TOS = ip.tos
Protocol = ip.p
data = (Source, Destination, Length, TTL, TOS, OFF, Protocol)
c.writerow(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment