Exercise: Generate an ICMP Packet Using Python

You are a network administrator at a local organization and you have been
asked to develop a tool to generate an ICMP packet. You have decided to use
Python to create a tool that sends an ICMP packet to a specified IP address.

Tools needed: Python 3, scapy library (https://scapy.net/)

Step 1: Open a terminal and install the scapy library by typing “pip install
scapy” and pressing the “Enter” key.

Step 2: Open a text editor or Python IDE and create a new Python file.

Step 3: Import the necessary libraries at the top of the file:

from scapy.all import *

Step 4: Define a function called “send_icmp” that takes in two parameters,
“dst_ip” and “msg”, and sends an ICMP packet to the specified destination IP
address with the specified message as the payload.

Step 5: Inside the “send_icmp” function, add the following code to create and
send the ICMP packet:

# Create the ICMP packet
icmp_packet = IP(dst=dst_ip)/ICMP()/msg
# Send the ICMP packet
send(icmp_packet)

Step 6: Test the “send_icmp” function by calling it with a valid IP address and a
message of your choice as the arguments.

Open chat
Hello 👋.
Tell me, how can I help you?