Python scapy ping script

The Python ping script is developed using Python and scapy. The screenshot of the script along the code explanation is provided below.

Create a file ping.py on a linux system and ensure that you have installed scapy.

Join our Course to Build Network Automation tools and scripts with Python and Scapy on Udemy
Line wise code explanation is provided below.

1.Imports the logging module
2.Calls the appropriate function in the logging module.
3.Imports scapy
4.Takes the IP address for which the status has to be checked and stores it in the variable dstip
5.Creates an ICMP header
6.Associates the ICMP type value as 8
7.Associates the ICMP code value as 0
8.Creates an IP header.
9 Associates the destination IP address in the IP header with the value taken from the user.
10. Sends the packet on the network with a timeout set.
11. If statement for checking if the packet has been received.
12. Prints the layer 3 status is upon receipt of packet
13. Else statement
14 Prints the layer 3 status is down.

The code is executed from the Ubuntu command line using the following command

sudo python ping.py.

Join our Course to Build Network Automation tools and scripts with Python and Scapy on Udemy

Output (Device which is up on the network. )

Output (Device which is down on the network. )