-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathping_module.py
More file actions
38 lines (32 loc) · 1.01 KB
/
Copy pathping_module.py
File metadata and controls
38 lines (32 loc) · 1.01 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
import logging
import re
import subprocess
import sys
def check_ping(target, timeout_seconds):
"""Prueft die Erreichbarkeit und liefert die Latenz in Millisekunden."""
if sys.platform.startswith("win"):
command = [
"ping", "-n", "1", "-w", str(timeout_seconds * 1000), target
]
else:
command = [
"ping", "-c", "1", "-W", str(timeout_seconds), target
]
try:
result = subprocess.run(
command,
capture_output=True,
text=True,
timeout=timeout_seconds + 1,
check=False,
)
except (FileNotFoundError, subprocess.TimeoutExpired) as error:
logging.warning("Ping konnte nicht ausgefuehrt werden: %s", error)
return None
if result.returncode != 0:
return None
output = result.stdout + result.stderr
match = re.search(r"time[=<]([0-9]+(?:[.,][0-9]+)?)", output)
if not match:
return 0.0
return float(match.group(1).replace(",", "."))