-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapper.py
86 lines (60 loc) · 1.93 KB
/
mapper.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
84
85
86
class Route:
def __init__(self, network, via, moi, metric):
self.network = network
self.via = via
self.moi = moi
self.metric = metric
def getRoute(self):
return self.network+"\n(Metric: "+self.metric+")"
def getVia(self):
return self.via
def getMoi(self):
return self.moi
def __str__(self):
return "route: [Network: "+self.network+", Via: " + self.via+"]"
def getRoutes(babelArray, myID):
routes = []
for line in babelArray:
elements = line.split(" ")
if elements[0] == "add" and elements[1] == "route" and elements[8] == "yes":
routes.append(Route(elements[4], elements[10], myID, elements[12]))
return routes
def babelCollect(ip, port):
import subprocess
babelData = subprocess.getoutput("bash -c \"echo dump | ncat "+ip+" "+port+" --no-shutdown -i 1\"")
"Only valid ncat one would have this (as the connection succeeded and we got a babel daemon response)"
if "my-id" in babelData:
babelArray = babelData.split("\n")
babelID = babelArray[3].split(" ")[1]
print("My ID: "+babelID)
routes = getRoutes(babelArray, babelID)
return routes
return None
def collectAllRoutes(peersFile):
file = open(peersFile, "r")
data = file.read()
allRoutes = []
for peer in data.split("\n"):
nodeIP = peer.split(" ")[0]
nodePort = peer.split(" ")[1]
routes = babelCollect(nodeIP, nodePort)
if routes != None:
for route in routes:
allRoutes.append(route)
return allRoutes
def makeGraph(peersFile):
routes = collectAllRoutes(peersFile)
print("Collected "+str(len(routes))+" routes")
graph="graph networkMap {"
"Add some spacing between nodes (vertically)"
graph+="graph [ranksep=\"2\"]"
for route in routes:
graph += "\""+route.getMoi()+"\" -- \""+route.getVia() +"\" [label=\""+route.getRoute()+"\"]"
graph+="}"
"Write out graph to disk"
file = open("thing.dot","w")
file.write(graph)
file.close()
import os
os.system("cat thing.dot | dot -Tpng -o graph.png")
makeGraph("peers.list")