From a402abb93f21ec53e0f8016815ae21f1f7523205 Mon Sep 17 00:00:00 2001 From: Abhishek Jha Date: Mon, 4 Dec 2023 13:20:52 +0530 Subject: [PATCH] feat: Integrate PythonPing as CLI tool This commit introduces PythonPing as a command-line tool (CLI) for network troubleshooting. The 'ping' functionality allows users to perform ICMP echo requests to a specified target IP address or hostname, displaying detailed response information and round-trip time (RTT) statistics. Changes Made: - Added 'ping.py' implementing the CLI tool with argparse for argument parsing and pythonping for ICMP ping operations. - Defined perform_ping() to execute pings and display responses. - Implemented ping_command_line() for parsing arguments and executing ping operations based on user inputs. Enhancements: - Displayed ping results, including successful replies and timed-out requests. - Calculated and exhibited RTT statistics (min/avg/max) for successful ping responses. Usage: - Users can execute the tool using 'python3 -m ping [target] [--count]'. - 'Target' represents the IP address or hostname to ping; '--count' specifies the number of pings (default: 4). Testing: - Thoroughly tested the tool to ensure consistent functionality across various network scenarios. Associated Issue: - Resolves #: A discussion was held in the linked issue before implementing this feature. Note: This commit adheres to the project's guidelines, including the code of conduct, and is ready for review and integration into the 'dev' branch. Contributor: Abhishek Jha --- ping.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 ping.py diff --git a/ping.py b/ping.py new file mode 100644 index 0000000..303084c --- /dev/null +++ b/ping.py @@ -0,0 +1,42 @@ +import argparse +from pythonping import ping +def perform_ping(target, count=4): + """Performs ICMP pings and displays the responses""" + responses = ping(target, count=count) + + # Display ping results + print(f"Ping results for {target}:") + min_rtt = float('inf') + max_rtt = 0 + total_rtt = 0 + for response in responses: + if response.success: + rtt = response.time_elapsed_ms + total_rtt += rtt + if rtt < min_rtt: + min_rtt = rtt + if rtt > max_rtt: + max_rtt = rtt + print(f"Reply from {response.message.source}, {len(response.message.packet.raw)} bytes in {response.time_elapsed_ms}ms") + # Extracted attributes: source, packet, time_elapsed_ms + else: + print(f"Request timed out") + + # Calculate and display RTT statistics + if len(responses) > 0: + avg_rtt = total_rtt / len(responses) + print(f"\nRound Trip Times min/avg/max is {min_rtt:.2f}/{avg_rtt:.2f}/{max_rtt:.2f} ms") + +def ping_command_line(): + parser = argparse.ArgumentParser(description='PythonPing command-line tool') + parser.add_argument('target', nargs='?', help='Target IP address to ping') + parser.add_argument('--count', type=int, default=4, help='Number of pings to send') + args = parser.parse_args() + + if args.target: + perform_ping(args.target, count=args.count) + else: + print("Please provide a target IP address or hostname.") + +if __name__ == "__main__": + ping_command_line()