-
Notifications
You must be signed in to change notification settings - Fork 0
/
runRIPETraceroute.py
249 lines (218 loc) · 7.57 KB
/
runRIPETraceroute.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
from __future__ import print_function
from ripe.atlas.cousteau import ProbeRequest
from ripe.atlas.cousteau import Probe
import ipaddress
from datetime import datetime
import traceback
import configparser
import MySQLdb as pymysql
from operator import itemgetter
from contextlib import closing
import random
import time
import sys
from math import radians, cos, sin, asin, sqrt
from ripe.atlas.cousteau import (
Ping,
Traceroute,
AtlasSource,
AtlasCreateRequest
)
def haversine(lon1, lat1, lon2, lat2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
km = 6367 * c
return km
def getCountry(ip):
countrySet = set()
try:
ctry=maxmind.city(ip).country.iso_code
if ctry != "None":
countrySet.add(ret)
return countrySet
except:
return set()
def selectProbes(prList):
retIDList=[]
distances=[]
if len(prList) ==1:
id,lat,lon=prList[0]
retIDList.append(id)
else:
for iter in range(0,len(prList)-1):
id,lat,lon=prList[iter]
for iter2 in range(iter+1,len(prList)):
id2,lat2,lon2=prList[iter2]
dist=haversine(lon,lat,lon2,lat2)
distances.append([id,id2,dist])
#retIDList.append(id)
sortedDistances=sorted(distances, key=itemgetter(2),reverse=True)
ID1,ID2,dist=sortedDistances[0]#Top one
retIDList=[ID1,ID2]
return retIDList
def runTraceroute(target,country,asn):
global noProbesASes
msms=[]
probeList=[]
probesids=""
prolen=0
filters = {"country_code": country,"asn_v4":asn}
print(filters)
probes = ProbeRequest(**filters)
for pb in probes:
probe = Probe(id=pb["id"])
lon=float(probe.geometry['coordinates'][0])
lat=float(probe.geometry['coordinates'][1])
print(pb["country_code"],country,pb["id"],pb["asn_v4"],lat,lon)
if pb["country_code"] != country:
print('Country filter didnt match: ',pb['id'],pb['country_code'],country)
continue
try:
ip=str(ipaddress.IPv4Address(probe.address_v4))
except:
continue
probeList.append([pb["id"],lat,lon])
if len(probeList)==0:
print('No probes found.')
return msms
selectedProbes=selectProbes(probeList)
for prbid in selectedProbes:
prolen+=1
probesids=probesids+str(prbid)+","
#print(probesids)
if prolen==0:
print('No probes found: '+str(prolen))
return msms
try:
probesids=probesids[:-1]
measurement=None
#print(probesids)
if typeRun=="traceroute":
traceroute = Traceroute(
af=4,
target=target,
description="Traceroute "+str(target),
protocol="ICMP",
)
measurement=traceroute
elif typeRun=="ping":
ping = Ping(af=4, target=target, description="Ping "+str(target))
measurement=ping
else:
print('Need correct type of measurement specified.')
exit(1)
source = AtlasSource(type="probes", value=probesids,requested=prolen)
atlas_request = AtlasCreateRequest(
start_time=datetime.utcnow(),
key=API_KEY_CREATE_UDM,
measurements=[measurement],
sources=[source],
is_oneoff=True
)
try:
(is_success, response) = atlas_request.create()
except:
traceback.print_exc()
if is_success:
msms=response['measurements']
else:
print(response)
except:
traceback.print_exc()
print("ERROR in traceroute.")
return msms
if __name__ == "__main__":
if sys.version_info < (2, 7):
print("ERROR: RIPE only support python2.7. Please use python2.7")
exit(0)
configFile = 'conf/ripe.conf'
config = configparser.ConfigParser()
config.read(configFile)
config.sections()
counter = 0
noProbesASes = []
try:
dbname = config['MySQL']['dbname']
serverIP = config['MySQL']['serverIP']
serverPort = int(config['MySQL']['serverPort'])
user = config['MySQL']['user']
password = config['MySQL']['password']
API_KEY_CREATE_UDM = config['DEFAULT']['API_KEY_CREATE_UDM']
API_KEY_DOWNLOAD_UDM = config['DEFAULT']['API_KEY_DOWNLOAD_UDM']
typeRun = config['DEFAULT']['typeRun']
outFile = config['DEFAULT']['outFile']
except:
traceback.print_exc()
allmsms=[]
#Get all detoured prefixes
db = pymysql.connect(host=serverIP,
port=serverPort,
user=user,
passwd=password,
db=dbname)
prefixToTraceroute={}
with closing( db.cursor() ) as cur:
try:
cur.execute('select are.prefix,are.as_path,u.detour_return_countries from AbnormalRibEntries are join v_all_def_paths u on are.as_path=u.as_path group by are.prefix,are.as_path,u.detour_return_countries')
row=cur.fetchone()
while row is not None:
prefix=row[0]
peerAS=row[1].split(' ')[0]
country=row[2]
prefixToTraceroute[prefix]=peerAS+'|'+country
row=cur.fetchone()
except Exception:
raise Exception('Select Query Failed')
db.close()
#print(prefixToTraceroute)
with closing(open(outFile,'w+')) as fp:
print('prefix peerAS country ip msm',file=fp)
try:
prfs = prefixToTraceroute.keys()
random.shuffle(prfs)
#for prefix,vals in prefixToTraceroute.items():
for prefix in prfs:
vals=prefixToTraceroute[prefix]
network = ipaddress.IPv4Network(unicode(prefix))
(peerAS,countryset)=vals.split('|')
if peerAS in noProbesASes:
continue
country=None
for ct in eval(countryset.replace('\'{','{').replace('}\'','}')):
country=ct
allNets = [network] if network.prefixlen >= 24 else network.subnets(new_prefix=24)
for net in allNets:
if peerAS in noProbesASes:
continue
normalizeabnormalRibEntriesIn=[]
randomHost=None
allHosts = list(net.hosts())
if(net.prefixlen==32):
allHosts.append(net.network_address)
randomHost=str(random.choice(allHosts))
if not randomHost:
continue
msm=runTraceroute(randomHost,country,peerAS)
if len(msm)==0:
noProbesASes.append(peerAS)
else:
for ms in msm:
with closing(open(outFile,'a+')) as fp:
print(prefix,peerAS,randomHost,country,ms)
print(prefix,peerAS,randomHost,country,ms,file=fp)
counter+=1
if counter == 90:
print('Sleeping...')
time.sleep(9*60)
counter=0
except:
traceback.print_exc()