blob: 6ae13c1cab6d24fa23e48c1d3f913a830e25e29b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
import subprocess
import platform
hostname = "something"
# ping parameters
# -c (count): number of packets to send
# -W (timeout): time to wait for a response, in seconds
ping_count = 5
ping_timeout = 1
# determine the system type (Windows or Unix-based)
system = platform.system().lower()
if system == "windows":
ping_args = ["ping", "-n", str(ping_count), "-w", str(ping_timeout * 1000), hostname]
else:
ping_args = ["ping", "-c", str(ping_count), "-W", str(ping_timeout), hostname]
# run the ping command
result = subprocess.run(ping_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# check if the ping was successful
if result.returncode == 0:
print(f"{hostname} is reachable.")
else:
print(f"{hostname} is unreachable.")
|