-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping_threads.py
64 lines (49 loc) · 1.35 KB
/
ping_threads.py
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
57
58
59
60
61
62
63
64
#!/usr/bin/env python
"""
ping using standard linux ping to avoid being root etc etc etc...
Tue Dec 22 10:17:16 AM 2018 CST
"""
from threading import Thread
import subprocess
try:
import queue
except ImportError:
import Queue as queue
import re
num_threads = 15
ips_q = queue.Queue()
out_q = queue.Queue()
ips = []
for i in range(1, 200):
ips.append("192.168.0." + str(i))
def thread_pinger(i, q):
"""Pings hosts in queue"""
_ = i
while True:
ip = q.get()
args = ["/bin/ping", "-c", "1", "-W", "1", str(ip)]
p_ping = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE)
p_ping_out = str(p_ping.communicate()[0])
if p_ping.wait() == 0:
# rtt min/avg/max/mdev = 22.293/22.293/22.293/0.000 ms
search = re.search(
r"rtt min/avg/max/mdev = (\S+)/(\S+)/(\S+)/(\S+) ms",
p_ping_out,
re.M | re.I,
)
ping_rtt = search.group(2)
out_q.put("OK " + str(ip) + " rtt= " + ping_rtt)
q.task_done()
for i in range(num_threads):
worker = Thread(target=thread_pinger, args=(i, ips_q))
worker.setDaemon(True)
worker.start()
for ip in ips:
ips_q.put(ip)
ips_q.join()
while True:
try:
msg = out_q.get_nowait()
except queue.Empty:
break
print(msg)