Sniffing

Scapy can also be used to easily capture packets on the wire:

>>> a = sniff(filter="icmp and host 172.217.3.164", count=5)>>> a.show()0000 Ether / IP / TCP 192.168.225.146:ssh > 192.168.225.1:50862 PA / Raw0001 Ether / IP / ICMP 192.168.225.146 > 172.217.3.164 echo-request 0 / Raw0002 Ether / IP / ICMP 172.217.3.164 > 192.168.225.146 echo-reply 0 / Raw0003 Ether / IP / ICMP 192.168.225.146 > 172.217.3.164 echo-request 0 / Raw0004 Ether / IP / ICMP 172.217.3.164 > 192.168.225.146 echo-reply 0 / Raw>>>

We can look at the packets in some more detail, including the raw format:

>>> for i in a:...     print i.show()...<skip>###[ Ethernet ]### dst= <> src= <> type= 0x800###[ IP ]### version= 4L ihl= 5L tos= 0x0 len= 84 id= 15714 flags= DF

Get Mastering Python Networking - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.