Python Scapy tutorial – Build ping packet

This Python Scapy tutorial , explains how to construct and send a ping packet using Python and Scapy. The tutorial shows how to construct and IP packet, create an ICMP header, the protocol used by ping and how it is appended and sent using Python program. The script is command like script. The explanation of code is provided below.

Check out our Network automation course with Python and Scapy Python Scapy Course on Udemy

Steps

1. Install Python and Scapy on any linux system. The above screenshot is an ubuntu system.
2. Create Python file called scapyping.py on the ubuntu system
3. The first line imports the scapy library into the Python file
4. The second line creates an ICMP header. This is created since ping uses icmp.
5. The third and fourth line appends associates the type value of icmp header as 8 and code value as zero. This is to craft an ICMP request packet.
6. The 5th line creates an IP header.
7. The 6th line associates the source IP address in the IP header as 192.168.1.25. This is the IP address of the ubuntu system where the code is executed.
8. The 7th line associates the destination IP address in the IP header as 192.168.1.100
9. The 8th line associates sends the ICMP packet with the created IP header.

The code is run from the command line.

Check out our Network automation course with Python and Scapy Python Scapy Course on Udemy