Denizhalil

Performing ICMP Ping Checks with Python

In computer networks, ICMP (Internet Control Message Protocol) ping requests are a widely used tool for verifying the accessibility of devices and servers. ICMP ping requests send packets to a target device or server and wait for responses. Receiving these responses helps us confirm the accessibility of a specific IP address. In this article, we will learn how to create a simple tool using Python to send ICMP ping requests.

Required Libraries

To create this tool, we will need the main library, scapy, and argparse to handle command-line arguments. Let’s start by importing these libraries:

import argparse
from scapy.all import IP, ICMP, sr

Sending ICMP Ping Requests

We will create a function called ping to perform the ping operation. This function will send a specified number of ICMP ping requests to the target IP address.
Don’t forget to check out our dos & ddos tool 😉

def ping(target_ip, count=4):
    successful_pings = 0

    for i in range(count):
        # Create an ICMP ping packet
        ping_packet = IP(dst=target_ip) / ICMP()

        # Send the packet and get the response
        response, _ = sr(ping_packet, timeout=2, verbose=False)

        # If a response is received
        if response:
            successful_pings += 1
            print(f"Successful ping: {response[0][1].src}")

    return successful_pings

Handling Command-Line Arguments

The tool needs to receive the target IP address and an optional count of ping requests from the user. We will use the argparse library to process these arguments.

def main():
    parser = argparse.ArgumentParser(description="PingChecker")
    parser.add_argument("target_ip", help="Destination IP address to send ping requests to")
    parser.add_argument("-c", "--count", type=int, default=4, help="Number of ping requests")

    args = parser.parse_args()
    target_ip = args.target_ip
    count = args.count

    successful_pings = ping(target_ip, count)

    print(f"{target_ip} =>> {count} ping =>> {successful_pings}")

if __name__ == "__main__":
    main()

For the advanced icmpwatch I shared on my github

Conclusion

This simple Python tool can be used to verify accessibility to a target IP address by sending ICMP ping requests. Users can specify the target IP address and the number of ping requests from the command line. Such tools can be valuable for network administrators and system administrators as they allow for quick and easy verification of device accessibility within a network.

By using the code we learned in this article, you can create your own ICMP ping check tool. Such tools play a crucial role in detecting and addressing network issues.

2 thoughts on “Performing ICMP Ping Checks with Python”

Leave a Comment

Join our Mailing list!

Get all latest news, exclusive deals and academy updates.