|
6 | 6 | import sys |
7 | 7 |
|
8 | 8 |
|
9 | | -def get_hostname(): |
10 | | - """Get the system's hostname.""" |
11 | | - return socket.gethostname() |
12 | | - |
13 | | - |
14 | | -def get_fqdn(): |
15 | | - """Get the system's fully qualified domain name.""" |
16 | | - return socket.getfqdn() |
17 | | - |
18 | | - |
19 | 9 | def get_ips_for_hostname(hostname): |
20 | 10 | """Get all IP addresses associated with the hostname.""" |
21 | 11 | try: |
@@ -55,20 +45,9 @@ def ping_target(target): |
55 | 45 | return False |
56 | 46 |
|
57 | 47 |
|
58 | | -# Maintain backwards compatibility with original function names |
59 | | -def ping_hostname(hostname): |
60 | | - """Ping a hostname directly and return whether it was successful.""" |
61 | | - return ping_target(hostname) |
62 | | - |
63 | | - |
64 | | -def ping_ip(ip): |
65 | | - """Ping an IP address and return whether it was successful.""" |
66 | | - return ping_target(ip) |
67 | | - |
68 | | - |
69 | 48 | def run_checks_for_target(target_name, target_value): |
70 | 49 | """Run all checks for a given target (hostname or FQDN). |
71 | | - |
| 50 | +
|
72 | 51 | Returns: |
73 | 52 | bool: True if all checks passed, False if any failed |
74 | 53 | """ |
@@ -97,37 +76,37 @@ def run_checks_for_target(target_name, target_value): |
97 | 76 | # Ping each IP |
98 | 77 | print(f"\nPinging IP addresses for {target_value}:") |
99 | 78 | for ip in ips: |
100 | | - success = ping_ip(ip) |
| 79 | + success = ping_target(ip) |
101 | 80 | status = "successful" if success else "failed" |
102 | 81 | print(f" Ping to {ip}: {status}") |
103 | 82 | if not success: |
104 | 83 | has_failures = True |
105 | | - |
| 84 | + |
106 | 85 | return not has_failures |
107 | 86 |
|
108 | 87 |
|
109 | 88 | def main(): |
110 | 89 | # Get hostname and FQDN |
111 | | - hostname = get_hostname() |
112 | | - fqdn = get_fqdn() |
113 | | - |
| 90 | + hostname = socket.gethostname() |
| 91 | + fqdn = socket.getfqdn() |
| 92 | + |
114 | 93 | all_passed = True |
115 | 94 |
|
116 | 95 | # Run checks for hostname |
117 | 96 | hostname_passed = run_checks_for_target("Hostname", hostname) |
118 | 97 | if not hostname_passed: |
119 | 98 | all_passed = False |
120 | | - |
121 | | - print("\n" + "="*50 + "\n") |
122 | | - |
| 99 | + |
| 100 | + print("\n" + "=" * 50 + "\n") |
| 101 | + |
123 | 102 | # Run checks for FQDN (only if different from hostname) |
124 | 103 | if fqdn != hostname: |
125 | 104 | fqdn_passed = run_checks_for_target("FQDN", fqdn) |
126 | 105 | if not fqdn_passed: |
127 | 106 | all_passed = False |
128 | 107 | else: |
129 | 108 | print(f"FQDN is the same as hostname: {fqdn}") |
130 | | - |
| 109 | + |
131 | 110 | # Exit with appropriate status code |
132 | 111 | if not all_passed: |
133 | 112 | print("\nSome checks failed. Exiting with status code 1.") |
|
0 commit comments