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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
import platform
import subprocess
import ipaddress
NETWORK = "192.168.1.0/24"
def ping(ip):
system = platform.system().lower()
if system == "windows":
cmd = ["ping", "-n", "1", "-w", "500", str(ip)]
else:
cmd = ["ping", "-c", "1", "-W", "1", str(ip)]
try:
output = subprocess.run(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
if output.returncode == 0:
# very simple latency extraction
text = output.stdout
for line in text.splitlines():
if "time=" in line:
return line.split("time=")[1].split(" ")[0]
return "ok"
except:
pass
return None
def scan_network():
devices = []
net = ipaddress.ip_network(NETWORK, strict=False)
for ip in net.hosts():
latency = ping(ip)
if latency:
devices.append({
"ip": str(ip),
"latency": latency
})
return devices
|