Skip to content

Commit 256230d

Browse files
committed
ping!
1 parent 0d89e74 commit 256230d

File tree

6 files changed

+121
-57
lines changed

6 files changed

+121
-57
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 17 deletions
This file was deleted.

.github/workflows/hostname.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Check Hostname
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
workflow_dispatch:
9+
10+
jobs:
11+
check-hostname:
12+
runs-on: ${{ matrix.os }}
13+
strategy:
14+
matrix:
15+
os: [ubuntu-latest, macos-latest, windows-latest]
16+
17+
steps:
18+
- uses: actions/checkout@v3
19+
20+
- name: Set up Python
21+
uses: actions/setup-python@v4
22+
with:
23+
python-version: '3.x'
24+
25+
- name: Run hostname check script
26+
run: python check-hostname.py

.github/workflows/timeout.yml

Lines changed: 0 additions & 21 deletions
This file was deleted.

Makefile

Lines changed: 0 additions & 12 deletions
This file was deleted.

check-hostname.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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()

hello.c

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)