|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import socket |
| 4 | +import platform |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | + |
| 8 | + |
| 9 | +def get_hostname(): |
| 10 | + """Get the system's hostname.""" |
| 11 | + return socket.gethostname() |
| 12 | + |
| 13 | + |
| 14 | +def get_ips_for_hostname(hostname): |
| 15 | + """Get all IP addresses associated with the hostname.""" |
| 16 | + try: |
| 17 | + # Get both IPv4 and IPv6 addresses |
| 18 | + addrinfo = socket.getaddrinfo(hostname, None) |
| 19 | + # Extract unique IP addresses |
| 20 | + ip_addresses = set() |
| 21 | + for addr in addrinfo: |
| 22 | + ip = addr[4][0] |
| 23 | + ip_addresses.add(ip) |
| 24 | + return list(ip_addresses) |
| 25 | + except socket.gaierror: |
| 26 | + return [] |
| 27 | + |
| 28 | + |
| 29 | +def ping_target(target): |
| 30 | + """ |
| 31 | + Ping a target (hostname or IP) and return whether it was successful. |
| 32 | +
|
| 33 | + Args: |
| 34 | + target (str): The hostname or IP address to ping |
| 35 | +
|
| 36 | + Returns: |
| 37 | + bool: True if ping was successful, False otherwise |
| 38 | + """ |
| 39 | + # Adjust ping command based on operating system |
| 40 | + param = "-n" if platform.system().lower() == "windows" else "-c" |
| 41 | + command = ["ping", param, "1", target] |
| 42 | + |
| 43 | + try: |
| 44 | + result = subprocess.run( |
| 45 | + command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True |
| 46 | + ) |
| 47 | + return result.returncode == 0 |
| 48 | + except Exception as e: |
| 49 | + print(f"Error pinging {target}: {e}") |
| 50 | + return False |
| 51 | + |
| 52 | + |
| 53 | +# Maintain backwards compatibility with original function names |
| 54 | +def ping_hostname(hostname): |
| 55 | + """Ping a hostname directly and return whether it was successful.""" |
| 56 | + return ping_target(hostname) |
| 57 | + |
| 58 | + |
| 59 | +def ping_ip(ip): |
| 60 | + """Ping an IP address and return whether it was successful.""" |
| 61 | + return ping_target(ip) |
| 62 | + |
| 63 | + |
| 64 | +def main(): |
| 65 | + # Get hostname |
| 66 | + hostname = get_hostname() |
| 67 | + print(f"Hostname: {hostname}") |
| 68 | + |
| 69 | + # Ping hostname |
| 70 | + hostname_ping_success = ping_hostname(hostname) |
| 71 | + hostname_ping_status = "successful" if hostname_ping_success else "failed" |
| 72 | + print(f"Ping to hostname {hostname}: {hostname_ping_status}") |
| 73 | + |
| 74 | + print() |
| 75 | + # Get IPs for hostname |
| 76 | + ips = get_ips_for_hostname(hostname) |
| 77 | + |
| 78 | + if not ips: |
| 79 | + print(f"No IP addresses found for {hostname}") |
| 80 | + return |
| 81 | + |
| 82 | + print(f"IP addresses for {hostname}:") |
| 83 | + for ip in ips: |
| 84 | + print(f" {ip}") |
| 85 | + |
| 86 | + # Ping each IP |
| 87 | + print("\nPinging IP addresses:") |
| 88 | + for ip in ips: |
| 89 | + success = ping_ip(ip) |
| 90 | + status = "successful" if success else "failed" |
| 91 | + print(f" Ping to {ip}: {status}") |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + main() |
0 commit comments