-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfind_src_i2.py
executable file
·83 lines (71 loc) · 2.66 KB
/
find_src_i2.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
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/python3
import sys
import requests
import re
import time
import datetime
BASE_URL = 'https://routerproxy.grnoc.iu.edu/internet2/'
FILENAME = '/home/wzhang/senior-research/ips.txt'
FORMAT = '/home/wzhang/senior-research/logs/mcast-i2-{:%Y-%m-%d-%H:%M:%S}.log'
VERBOSE = True
class Source():
def __init__(self, source, group, stats, router):
self.source = source
self.group = group
st = stats.split(',')
self.speed = int(re.sub(r'[^0-9]', '', st[0]))
self.pps = int(re.sub(r'[^0-9]', '', st[1]))
self.packets = int(re.sub(r'[^0-9]', '', st[2]))
self.router = router
def __repr__(self):
return 'Group: {0:25s}\tSource: {1:25s}\tRouter: {2:18s}\tPackets/Second: {3:6d}'.format(self.group, self.source, self.router, self.pps)
def __str__(self):
return 'Group: {0:25s}\tSource: {1:25s}\tRouter: {2:18s}\tPackets/Second: {3:6d}'.format(self.group, self.source, self.router, self.pps)
def __eq__(self, other):
return self.source == other.source and self.group == other.group and self.router == self.router
def __lt__(self, other):
return self.group < other.group
def __hash__(self):
return hash(self.__repr__())
def main():
global FILENAME
global BASE_URL
global VERBOSE
global FORMAT
devices = set()
with open(FILENAME, 'r') as f:
ip = f.readline()
while ip != '':
devices.add(ip.strip())
ip = f.readline()
devices = list(devices)
output = set()
d = datetime.datetime.now()
for device in devices:
r = requests.get(BASE_URL + '?method=submit&device=' + device + '&command=show multicast&menu=0&arguments=route detail')
new_text = re.sub(r'&[^\s]{2,4};|[\r]', '', r.text)
s_new_text = new_text.split('\n')
fields = dict()
for i in range(1, len(s_new_text) - 1):
s_line = s_new_text[i].split(':', 1)
if s_line[0] == '':
if 'Group' in fields:
s = Source(fields['Source'], fields['Group'], fields['Statistics'], device)
if s.pps > 5:
if VERBOSE:
print(s)
print()
output.add(s)
fields = dict()
else:
fields[s_line[0]] = ''.join(s_line[1:])
time.sleep(2)
out_list = sorted(list(output))
if VERBOSE:
print('-' * 10)
for o in out_list:
print(o)
with open(FORMAT.format(d), 'a+') as f:
f.write('\n'.join([str(out) for out in out_list]))
if __name__ == '__main__':
main()