Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import platform
- # Dictionary to store named IPs
- ip_names = {}
- last_ip = None
- last_ping_stats = ""
- def ping(ip):
- """Ping the given IP address and return the detailed result."""
- param = "-n 1" if platform.system().lower() == "windows" else "-c 1"
- command = f"ping {param} {ip}"
- response = os.popen(command).read()
- return response
- def check_ping(ip):
- """Check if the IP is reachable and store the result."""
- global last_ping_stats
- param = "-n 1" if platform.system().lower() == "windows" else "-c 1"
- command = f"ping {param} {ip}"
- response = os.popen(command).read()
- # Check if any packets were received (look for "0 packets received")
- if "0 received" in response or "100% packet loss" in response:
- last_ping_stats = response
- return False
- else:
- last_ping_stats = response
- return True
- def handle_command(command):
- global last_ip
- cmd_parts = command.split(" ")
- if command.startswith("/ping"):
- if len(cmd_parts) > 1 and "=" in cmd_parts[1]:
- ip_or_name = cmd_parts[1].split("=")[1]
- elif len(cmd_parts) == 2:
- ip_or_name = cmd_parts[1]
- else:
- print("Error: Invalid format. Use '/ping ip=x' or '/ping GivenName'.")
- return
- ip_to_ping = ip_names.get(ip_or_name, ip_or_name) # Check if it's a name, fallback to IP
- last_ip = ip_to_ping
- if check_ping(ip_to_ping):
- print(f"{ip_to_ping} is reachable.")
- else:
- print(f"{ip_to_ping} is not reachable.")
- elif command.startswith("/name"):
- if len(cmd_parts) == 4 and cmd_parts[1].startswith("ip=") and cmd_parts[2] == "name":
- ip = cmd_parts[1].split("=")[1]
- name = cmd_parts[3]
- ip_names[name] = ip
- last_ip = ip
- print(f"Named IP {ip} as {name}.")
- elif len(cmd_parts) == 3 and cmd_parts[1] == "ip=prev" and cmd_parts[2].startswith("name"):
- if last_ip:
- name = cmd_parts[2].split("=")[1]
- ip_names[name] = last_ip
- print(f"Named previous IP {last_ip} as {name}.")
- else:
- print("No previous IP to name.")
- else:
- print("Error: Invalid command format.")
- elif command == "/statsip prev":
- if last_ip:
- print(f"Statistics for {last_ip}:\n{last_ping_stats}")
- else:
- print("No previous IP to show statistics for.")
- else:
- print("Unknown command.")
- # Main loop
- def main():
- while True:
- command = input("> ")
- if command.lower() in ["/quit", "/exit"]:
- break
- handle_command(command)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement