Python Ping Script

Th python ping script is a ping tool which is developed with Python and Scapy. The screenshot of the code and line by line explanation of the code is provided below. The script builds a ping packet with Python and scapy and is used for checking the connectivity between systems.

Learn to build network tools & scripts with our Python automation course

The name of the Python file is l3tshoot.py.

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 l3tshoot.py

The name of the Python file is l3tshoot.py.

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.

Learn how to build your own network tools with Python and scapy with our course. The course contains examples and projects which help you how to to integrate Python and scapy for network automation.

Click here to take the Course with Projects & Source Code on Udemy

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

sudo python l3tshoot.py