Advertisement
EdmundC

pingsubsoft

Sep 30th, 2024 (edited)
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 KB | None | 0 0
  1. import os
  2. import platform
  3.  
  4. # Dictionary to store named IPs
  5. ip_names = {}
  6. last_ip = None
  7. last_ping_stats = ""
  8.  
  9. def ping(ip):
  10.     """Ping the given IP address and return the detailed result."""
  11.     param = "-n 1" if platform.system().lower() == "windows" else "-c 1"
  12.     command = f"ping {param} {ip}"
  13.     response = os.popen(command).read()
  14.     return response
  15.  
  16. def check_ping(ip):
  17.     """Check if the IP is reachable and store the result."""
  18.     global last_ping_stats
  19.     param = "-n 1" if platform.system().lower() == "windows" else "-c 1"
  20.     command = f"ping {param} {ip}"
  21.     response = os.popen(command).read()
  22.    
  23.     # Check if any packets were received (look for "0 packets received")
  24.     if "0 received" in response or "100% packet loss" in response:
  25.         last_ping_stats = response
  26.         return False
  27.     else:
  28.         last_ping_stats = response
  29.         return True
  30.  
  31. def handle_command(command):
  32.     global last_ip
  33.     cmd_parts = command.split(" ")
  34.  
  35.     if command.startswith("/ping"):
  36.         if len(cmd_parts) > 1 and "=" in cmd_parts[1]:
  37.             ip_or_name = cmd_parts[1].split("=")[1]
  38.         elif len(cmd_parts) == 2:
  39.             ip_or_name = cmd_parts[1]
  40.         else:
  41.             print("Error: Invalid format. Use '/ping ip=x' or '/ping GivenName'.")
  42.             return
  43.  
  44.         ip_to_ping = ip_names.get(ip_or_name, ip_or_name)  # Check if it's a name, fallback to IP
  45.         last_ip = ip_to_ping
  46.  
  47.         if check_ping(ip_to_ping):
  48.             print(f"{ip_to_ping} is reachable.")
  49.         else:
  50.             print(f"{ip_to_ping} is not reachable.")
  51.  
  52.     elif command.startswith("/name"):
  53.         if len(cmd_parts) == 4 and cmd_parts[1].startswith("ip=") and cmd_parts[2] == "name":
  54.             ip = cmd_parts[1].split("=")[1]
  55.             name = cmd_parts[3]
  56.             ip_names[name] = ip
  57.             last_ip = ip
  58.             print(f"Named IP {ip} as {name}.")
  59.         elif len(cmd_parts) == 3 and cmd_parts[1] == "ip=prev" and cmd_parts[2].startswith("name"):
  60.             if last_ip:
  61.                 name = cmd_parts[2].split("=")[1]
  62.                 ip_names[name] = last_ip
  63.                 print(f"Named previous IP {last_ip} as {name}.")
  64.             else:
  65.                 print("No previous IP to name.")
  66.         else:
  67.             print("Error: Invalid command format.")
  68.  
  69.     elif command == "/statsip prev":
  70.         if last_ip:
  71.             print(f"Statistics for {last_ip}:\n{last_ping_stats}")
  72.         else:
  73.             print("No previous IP to show statistics for.")
  74.  
  75.     else:
  76.         print("Unknown command.")
  77.  
  78. # Main loop
  79. def main():
  80.     while True:
  81.         command = input("> ")
  82.         if command.lower() in ["/quit", "/exit"]:
  83.             break
  84.         handle_command(command)
  85.  
  86. if __name__ == "__main__":
  87.     main()
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement