Cross-Platform, Python

Packet sniffing in Python

With pcapy module you can capture any network packet! Let’s see a simple example of packet sniffing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/python
 
import datetime, pcapy, sys
 
def main(argv):
  cap = pcapy.open_live("eth0", 100, 1, 0)
 
  (header, payload) = cap.next()
  while header:
    print ('%s: captured %d bytes, truncated to %d bytes'
           %(datetime.datetime.now(), header.getlen(), header.getcaplen()))
    (header, payload) = cap.next()
 
if __name__ == "__main__":
  main(sys.argv)

To run the code above you need administrative rights!