-
Notifications
You must be signed in to change notification settings - Fork 0
/
pingtest.py
70 lines (62 loc) · 2.5 KB
/
pingtest.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
65
66
67
68
69
70
from collections import defaultdict
from multiprocessing.pool import Pool
from traceutils.progress.bar import Progress
from traceutils.scamper.hop import ICMPType
from traceutils.scamper.warts import WartsReader
from traceutils.utils.net import prefix_addrs
def read_responses(filename):
responses = defaultdict(bool)
with WartsReader(filename) as f:
for ping in f:
resp = any(r.type == ICMPType.echo_reply for r in ping.responses)
responses[ping.dst] |= resp
return responses
class PingTest:
def __init__(self, files):
self.files = files
self._responses = defaultdict(bool)
self.responses = {}
def read_responses_all(self, poolsize=25):
pb = Progress(len(self.files), 'Reading pings', callback=lambda: 'Responses {:,d}'.format(sum(self._responses.values())))
with Pool(poolsize) as pool:
for responses in pb.iterator(pool.imap_unordered(read_responses, self.files)):
for a, b in responses.items():
self._responses[a] |= b
self.responses = dict(self._responses)
# for file in pb.iterator(self.files):
# self.read_responses(file)
#
# def read_responses(self, filename):
# responses = defaultdict(bool)
# with WartsReader(filename) as f:
# for ping in f:
# resp = any(r.type == ICMPType.echo_reply for r in ping.responses)
# responses[ping.dst] |= resp
# return responses
def test_candidates(self, candidates):
valid = {}
for a in candidates:
# if a not in self.responses:
# valid[a] = 6
# continue
w, x, y, z = prefix_addrs(a, 2)
if w not in self.responses and x not in self.responses and y not in self.responses and z not in self.responses:
valid[a] = 6
continue
b = y if x == a else y
# if self.responses[w] or self.responses[z]:
if self.responses.get(w, False) or self.responses.get(z, False):
valid[a] = 1
continue
if self.responses.get(x, False) and self.responses.get(y, False):
valid[a] = 2
continue
if self.responses.get(a, False):
valid[a] = 3
continue
if self.responses.get(b, False):
valid[a] = 5
continue
else:
valid[a] = 4
return valid