forked from USC-NSL/ripe-atlas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyse_traceroute.py
255 lines (198 loc) · 6.68 KB
/
analyse_traceroute.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import sys
import hmvp
import json
import requests
import asrel
import ip2as as IP2AS
URL = 'https://atlas.ripe.net/api/v1/probe/?limit=10000&format=txt'
TRfile = 'outputTraceroute.txt'
probeData = 'atlas-probes.json'
asMapFile = 'asMap.txt'
asRelationFile = 'asrelOut.txt'
ixpInfoFile = 'ixpInfo.txt'
peers = {}
providercust_dict = {}
custprovider_dict = {}
def getProbe(id2info,probe_id):
tmp = id2info[probe_id]
if str(tmp[0])=='None':
return tmp[1],tmp[3]
return tmp[0],tmp[2]
def initProbeData():
response = requests.get(URL) #make request
json_response = json.loads(response.text)
if 'error' in json_response:
err_msg = 'Error: %s' % json_response['error']
raise Exception(err_msg)
probes = json_response['objects']
id2info={}
for probe in probes:
id2info[int(probe['id'])] = (probe['asn_v4'],probe['asn_v6'],probe['prefix_v4'],probe['prefix_v6'])
print "probe data init completed"
return id2info
def load_relationship_data(providercustomer_file, peering_file):
f = open(providercustomer_file)
for line in f:
line = line.strip()
chunks = line.split()
provider = chunks[0]
#customers = chunks[1:]
#include provider in customer cone
providercust_dict[provider] = set(chunks) #customers
customers = chunks[1:]
for customer in customers:
try:
custprovider_dict[customer].add(provider)
except KeyError:
custprovider_dict[customer] = set(provider)
f.close()
f = open(peering_file)
for line in f:
line = line.strip()
if line.startswith('#'):
continue
chunks = line.split('|')
p1 = chunks[0]
p2 = chunks[1]
peering = chunks[2]
if peering == '0':
try:
peers[p1].add(p2)
except KeyError:
peers[p1] = set(p2)
try:
peers[p2].add(p1)
except KeyError:
peers[p2] = set(p1)
f.close()
def cleanAS(asn):
if '{' in asn:
asn = asn.split('{')[1].split('}')[0]
if '_' in asn:
tmp = asn.split('_')
asn = tmp[0]
if float(tmp[1])>float(tmp[0]):
asn = tmp[1]
return asn
def relationship(as1, as2):
"""
as1 is a X of as2
"""
ispeer = False if as1 not in peers else as2 in peers[as1]
#is as2 a customer of as1
iscustomer = False if as2 not in providercust_dict else as1 in providercust_dict[as2]
#is as2 a provider of as2
isprovider = False if as2 not in custprovider_dict else as1 in custprovider_dict[as2]
if ispeer:
return 'peer'
elif iscustomer:
return 'customer'
elif isprovider:
return 'provider'
else:
return 'missing'
def getASN(id2info):
mapper = hmvp.ip2as.Mapper()
fout=open(asMapFile,'w')
with open(TRfile, 'r') as f:
for line in f:
tmp = line.split('\n')[0].split(',')
hop_ips = tmp[2:]
as_trace = mapper.mapTrace(hop_ips)
as_out = ','.join(as_trace)
probe_id=int(tmp[0])
probeASN,probeIP = getProbe(id2info,probe_id)
line = ''
if str(probeASN) != as_trace[0]:
line = str(probeASN)+','+as_out
print line,probe_id
line += '\n'
else:
line = as_out
print line, probe_id
line += '\n'
fout.write(line)
fout.close()
def analyseASRelation():
load_relationship_data('20131101.ppdc-ases.txt', '20131101.as-rel.txt')
fout=open(asRelationFile,'w')
with open(asMapFile, 'r') as f:
for line in f:
nline=''
aspath = line.split('\n')[0].split(',')
as1=aspath[0]
print "line: ",line
for asn in aspath[1:]:
as2 = asn
as1,as2 = cleanAS(as1),cleanAS(as2)
r = relationship(as1, as2)
if nline == '':
nline += as1+'--('+r+')->'+as2
else:
nline += '--('+r+')->'+as2
print as1,as2
print nline
as1 = as2
nline += '\n'
fout.write(nline)
def getASPaths(mapper, hops,probe_id):
as_trace = mapper.mapTrace(hops)
probeASN,probeIP = getProbe(id2info,probe_id)
print hops, as_trace
if str(probeASN) != as_trace[0]:
as_trace = [str(probeASN)]+as_trace
as_out = ' '.join(as_trace)
return as_out, as_trace
def getASrel(aspaths):
as_pairs = asrel.pairs(aspaths)
print as_pairs
rel_list = []
for (as1, as2) in as_pairs:
r = relationship(as1, as2)
rel_list.append(r)
rel_str = ' '.join(rel_list)
return rel_str
def getRelationData_tabFormat():
#mapper = hmvp.ip2as.Mapper()
mapper = ip2as.Mapper()
load_relationship_data('20131101.ppdc-ases.txt', '20131101.as-rel.txt')
fout=open(asRelationFile,'w')
with open(TRfile, 'r') as f:
for line in f:
nline = ''
tmp = line.split('\n')[0].split(',')
hop_ips = tmp[2:]
probe_id=int(tmp[0])
aspaths,as_trace = getASPaths(mapper, hop_ips, probe_id)
#print as_trace
asrelations = getASrel(as_trace)
#print asrelations
nline += tmp[0]+'|'+tmp[1]+'|'+aspaths+'|'+asrelations+'\n'
#print nline
fout.write(nline)
fout.close()
def getIXPInfo():
mapper = IP2AS.Mapper()
load_relationship_data('20131101.ppdc-ases.txt', '20131101.as-rel.txt')
fout=open(ixpInfoFile,'w')
with open(TRfile, 'r') as f:
for line in f:
nline = ''
tmp = line.split('\n')[0].split(',')
hop_ips = tmp[2:]
probe_id=int(tmp[0])
as_trace_ixp = mapper.mapTraceIXP(hop_ips)
asPathIXP = ' '.join(as_trace_ixp)
aspaths,as_trace = getASPaths(mapper, hop_ips, probe_id)
print as_trace
asrelations = getASrel(as_trace)
#print asrelations
nline += tmp[0]+'|'+tmp[1]+'|'+aspaths+'|'+asrelations+'|'+asPathIXP+'\n'
fout.write(nline)
fout.close()
if __name__=='__main__':
id2info = initProbeData()
#getRelationData_tabFormat()
getIXPInfo()
#getASN(id2info)
#analyseASRelation()