|
3 | 3 | import socket |
4 | 4 | import platform |
5 | 5 | import subprocess |
6 | | -import sys |
7 | 6 |
|
8 | 7 |
|
9 | 8 | def get_hostname(): |
10 | 9 | """Get the system's hostname.""" |
11 | 10 | return socket.gethostname() |
12 | 11 |
|
13 | 12 |
|
| 13 | +def get_fqdn(): |
| 14 | + """Get the system's fully qualified domain name.""" |
| 15 | + return socket.getfqdn() |
| 16 | + |
| 17 | + |
14 | 18 | def get_ips_for_hostname(hostname): |
15 | 19 | """Get all IP addresses associated with the hostname.""" |
16 | 20 | try: |
@@ -61,35 +65,51 @@ def ping_ip(ip): |
61 | 65 | return ping_target(ip) |
62 | 66 |
|
63 | 67 |
|
64 | | -def main(): |
65 | | - # Get hostname |
66 | | - hostname = get_hostname() |
67 | | - print(f"Hostname: {hostname}") |
| 68 | +def run_checks_for_target(target_name, target_value): |
| 69 | + """Run all checks for a given target (hostname or FQDN).""" |
| 70 | + print(f"{target_name}: {target_value}") |
68 | 71 |
|
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}") |
| 72 | + # Ping the target directly |
| 73 | + ping_success = ping_target(target_value) |
| 74 | + ping_status = "successful" if ping_success else "failed" |
| 75 | + print(f"Ping to {target_name.lower()} {target_value}: {ping_status}") |
73 | 76 |
|
74 | 77 | print() |
75 | | - # Get IPs for hostname |
76 | | - ips = get_ips_for_hostname(hostname) |
| 78 | + # Get IPs for the target |
| 79 | + ips = get_ips_for_hostname(target_value) |
77 | 80 |
|
78 | 81 | if not ips: |
79 | | - print(f"No IP addresses found for {hostname}") |
| 82 | + print(f"No IP addresses found for {target_value}") |
80 | 83 | return |
81 | 84 |
|
82 | | - print(f"IP addresses for {hostname}:") |
| 85 | + print(f"IP addresses for {target_value}:") |
83 | 86 | for ip in ips: |
84 | 87 | print(f" {ip}") |
85 | 88 |
|
86 | 89 | # Ping each IP |
87 | | - print("\nPinging IP addresses:") |
| 90 | + print(f"\nPinging IP addresses for {target_value}:") |
88 | 91 | for ip in ips: |
89 | 92 | success = ping_ip(ip) |
90 | 93 | status = "successful" if success else "failed" |
91 | 94 | print(f" Ping to {ip}: {status}") |
92 | 95 |
|
93 | 96 |
|
| 97 | +def main(): |
| 98 | + # Get hostname and FQDN |
| 99 | + hostname = get_hostname() |
| 100 | + fqdn = get_fqdn() |
| 101 | + |
| 102 | + # Run checks for hostname |
| 103 | + run_checks_for_target("Hostname", hostname) |
| 104 | + |
| 105 | + print("\n" + "="*50 + "\n") |
| 106 | + |
| 107 | + # Run checks for FQDN (only if different from hostname) |
| 108 | + if fqdn != hostname: |
| 109 | + run_checks_for_target("FQDN", fqdn) |
| 110 | + else: |
| 111 | + print(f"FQDN is the same as hostname: {fqdn}") |
| 112 | + |
| 113 | + |
94 | 114 | if __name__ == "__main__": |
95 | 115 | main() |
0 commit comments