Scapy has a sniff() function that we can use for getting packets from the network. But Scapy's built-in sniff() function is a bit slow and may skip some packets. It is better to use tcpdump when the sniffing speed is important.
Sniffing packets
How to do it...
Here are the steps to write a sniffer with scapy module:
- Create a file called scapy-sniffer.py and open it with your editor.
- As usual, import the required modules for the script:
import sys from scapy.all import *
- Then, define the variables required. Here we need to define the interface to sniff:
interface = "en0"
You can get the interface to be used with the help of the ifconfig command in Linux and macOS:
- Now we can write a function to handle...