Sending and receiving packets with Scapy
We have already created some packets in the previous recipe. Now we can send and receive those packets with Scapy.
How to do it...
Following are the methods to send and receive packets with scapy
module:
- Make sure to import the required modules:
from scapy.all import *from pprint import pprint
- We can use the
send()
function to send packets at layer 3. In this case, Scapy will handle the routing and layer 2 within it:
network = IP(dst = '192.168.1.1')transport = ICMP()packet = network/transportsend(IP(packet)
This will send an ICMP packet
- To send a packet with custom layer 2, we have to use the
sendp()
method. Here we have to pass the interface to be used for sending the packet. We can provide it with theiface
parameter. If this is not provided, it will use the default value fromconf.iface
:
ethernet = Ether()network = IP(dst = '192.168.1.1')transport = ICMP()packet = ethernet/network/transportsendp(packet, iface="en0")
- To send a packet and receive a response...