Skip to content

Commit 5607986

Browse files
committed
fqdn
1 parent 417f066 commit 5607986

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

check-hostname.py

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
import socket
44
import platform
55
import subprocess
6-
import sys
76

87

98
def get_hostname():
109
"""Get the system's hostname."""
1110
return socket.gethostname()
1211

1312

13+
def get_fqdn():
14+
"""Get the system's fully qualified domain name."""
15+
return socket.getfqdn()
16+
17+
1418
def get_ips_for_hostname(hostname):
1519
"""Get all IP addresses associated with the hostname."""
1620
try:
@@ -61,35 +65,51 @@ def ping_ip(ip):
6165
return ping_target(ip)
6266

6367

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}")
6871

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}")
7376

7477
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)
7780

7881
if not ips:
79-
print(f"No IP addresses found for {hostname}")
82+
print(f"No IP addresses found for {target_value}")
8083
return
8184

82-
print(f"IP addresses for {hostname}:")
85+
print(f"IP addresses for {target_value}:")
8386
for ip in ips:
8487
print(f" {ip}")
8588

8689
# Ping each IP
87-
print("\nPinging IP addresses:")
90+
print(f"\nPinging IP addresses for {target_value}:")
8891
for ip in ips:
8992
success = ping_ip(ip)
9093
status = "successful" if success else "failed"
9194
print(f" Ping to {ip}: {status}")
9295

9396

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+
94114
if __name__ == "__main__":
95115
main()

0 commit comments

Comments
 (0)