Skip to content

feat: Integrate PythonPing as CLI tool #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions ping.py
Original file line number Diff line number Diff line change
@@ -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()