This Python script implements a simple ping utility that sends ICMP echo requests to a specified host, calculates round-trip times (RTTs), and provides statistics including packet loss, minimum, maximum, average RTT, and RTT standard deviation. It also includes a user-friendly command-line interface for customization.
- Checksum Calculation: Ensures data integrity of ICMP packets.
- Multi-Request Pinging: Sends multiple ping requests and tracks RTT for each.
- Comprehensive Statistics: Reports packet loss, minimum, maximum, average RTT, and RTT standard deviation.
- Command-Line Interface: Uses
argparsefor user customization.
- Python 3.x
- Administrative privileges (for raw socket access)
-
Save the Script
Save the provided Python script as
ping.py. -
Run the Script
Execute the script from the command line. You can specify the target host and the number of ping requests.
python ping.py <host> <count>
The Custom Ping Utility script is designed to send ICMP Echo Requests (pings) to a target and measure the Round-Trip Time (RTT) for each request. Here’s a step-by-step breakdown of how the script operates:
Before sending an ICMP packet, the script calculates a checksum to ensure data integrity.
- Function:
checksum(data) - Process:
- Adjusts the data length to ensure it is even.
- Unpacks the data into 16-bit words.
- Sums the words and adds any overflow bits.
- Inverts the result to obtain the checksum.
The script creates and sends an ICMP Echo Request packet to the target.
- Function:
send_ping_request(destination, sequence_number) - Process:
- Create Socket: A raw socket is created for ICMP communication.
- Build Header: Constructs the ICMP header with type (8 for Echo Request), code (0), checksum (initially 0), identifier, and sequence number.
- Calculate Checksum: Computes and updates the checksum for the header.
- Send Packet: Sends the ICMP packet to the target and records the time of sending.
The script waits for a reply that matches the sent request.
- Function:
receive_ping_reply(icmp_socket, expected_seq_number) - Process:
- Listen for Response: Continuously listens for incoming ICMP packets on the socket.
- Extract Header: Extracts and inspects the ICMP header from the response.
- Match Reply: Checks if the response is an Echo Reply (type 0) with the correct sequence number.
- Record Time: Captures the time when the reply is received.
For each received reply, the script calculates the Round-Trip Time (RTT).
- Function:
ping(target, num_requests) - Process:
- Send Requests: Sends the specified number of ping requests to the target.
- Receive Replies: Receives and processes replies, calculating the RTT for each.
- Store RTT: Collects RTT values in a list.
After completing all ping requests, the script calculates and displays summary statistics.
- Packet Loss: Computes the percentage of lost packets.
- RTT Statistics: Calculates minimum, maximum, average, and standard deviation of RTT values.
- Print Results: Outputs the RTT statistics and packet loss percentage.
- Send Request: The script sends an ICMP Echo Request to
example.comwith sequence number 0. - Receive Reply: The script waits for a reply and verifies it matches the sequence number 0.
- Measure RTT: The time difference between sending the request and receiving the reply is calculated.
- Repeat: This process is repeated for the specified number of requests.
- Calculate Stats: After all requests, the script calculates and prints statistics on RTT and packet loss.
By following these steps, the Custom Ping Utility provides a simple way to measure network latency and reliability to a target host.
