-
Notifications
You must be signed in to change notification settings - Fork 19
/
get-ips.py
executable file
·216 lines (200 loc) · 8.72 KB
/
get-ips.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
#!/usr/bin/env python
import os
import sys
from lib.Atlas import MeasurementFetch,MeasurementPrint,IPInfoCache
sys.path.append( os.path.dirname(os.path.realpath(__file__) ) )
from libixpcountryjedi import ip2asn,ip2hostname,ICJRun
import requests
import json
import urllib2
import ripe.atlas.sagan
import ipaddress
import re
import concurrent.futures
import threading
from multiprocessing import cpu_count
#import random
from math import radians, cos, sin, asin, sqrt
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))
r = 6371 # Radius of earth in kilometers. Use 3956 for miles
return c * r
ipinfo = {}
counter = 1
run = ICJRun('.')
### read openipmap info, and key by IP
#openipmap = {}
#with open("ips-openipmap.json-fragments") as inf:
# for line in inf:
# d = json.loads( line )
# openipmap[ d['ip'] ] = d
def get_asnmeta( asn ):
meta = {'asn': asn, 'as_name': '<unknown>', 'as_description': '<unknown>', 'as_country': 'XX', 'ips_v4': None, '48s_v6': None}
### find long name and asn-size
## call https://stat.ripe.net/data/as-overview/data.json?resource=%s
## and https://stat.ripe.net/data/routing-status/data.json?resource=%s
name_url = "https://stat.ripe.net/data/as-overview/data.json?resource=AS%s" % ( asn )
size_url = "https://stat.ripe.net/data/routing-status/data.json?resource=AS%s" % ( asn )
MAX_RETRIES=5
nconn = None
nretries = 0
if nconn == None and nretries <= MAX_RETRIES:
try:
nconn = urllib2.urlopen( name_url , timeout=60 )
except:
nretries += 1
if not nconn:
print "URL fetch error on: %s" % (name_url)
else:
try:
ndata = json.load( nconn )
holder = ndata['data']['holder']
name_desc,cc = holder.rsplit(",",1)
nd_list = name_desc.split(" ",1)
name = nd_list[0]
desc = None
if len(nd_list) == 1:
desc = nd_list[0]
else:
desc = nd_list[1]
meta['as_name'] = name
meta['as_description'] = desc
meta['as_country'] = cc
except:
print "asn name extraction failed for %s (%s)" % ( asn, ndata )
sconn = None
sretries = 0
if sconn == None and sretries <= MAX_RETRIES:
try:
sconn = urllib2.urlopen( size_url , timeout=60 )
except:
sretries += 1
if not sconn:
print "URL fetch error on: %s" % (size_url)
else:
try:
sdata = json.load( sconn )
meta['ips_v4'] = sdata['data']['announced_space']['v4']['ips']
meta['48s_v6'] = sdata['data']['announced_space']['v6']['48s']
except:
print "asn size extraction failed for %s (%s)" % ( asn, sdata )
return meta
def main():
iprtt={}
with open('measurementset.json','r') as infile:
msms = json.load( infile )
msm_list = msms['v4'] + msms['v6']
# random.shuffle( msm_list )
count=0
for m in msm_list:
print >>sys.stderr, "(%d/%d) msm gathering, now fetching %s" % ( count, len(msm_list), m )
for data in MeasurementFetch.fetch( m['msm_id'] ):
tr = ripe.atlas.sagan.TracerouteResult( data )
for hop in tr.hops:
for pkt in hop.packets:
ip = pkt.origin
rtt = pkt.rtt
if pkt.arrived_late_by: ## these are 'weird' packets ignore ehm (better would be to filter out pkts with 'edst')
continue
if ip != None:
if not ip in iprtt or iprtt[ ip ]['rtt'] > rtt:
iprtt[ ip ] = {
'rtt': rtt,
'prb_id': tr.probe_id,
'msm_id': tr.measurement_id,
}
count+=1
ips = list( iprtt.keys() )
ips.sort()
ip_count = len( ips )
print >>sys.stderr, "ip gathering finished, now analysing. ip count: %s" % ( ip_count )
outf = open("ips.json-fragments","w")
outf_geoerr = open('geo-err.json-fragments',"w")
no_result_cnt = 0
for ip in ips:
print >>sys.stderr, "attempting %s" % ip
j = None
#if True
have_result = False
try:
out = {'ip': ip, 'lon': None, 'location': '', 'lat': None, 'hostname': "", 'asn': "", 'geo_error': None}
req = requests.get("https://ipmap.ripe.net/api/v1/locate/%s/partials?engines=crowdsourced,ixp" % ip , timeout=25, verify=False )
j = req.json()
j['ip'] = ip
#del( j['meta'] )
#print "%s" % json.dumps( j )
have_result = False
try:
out['hostname'] = ip2hostname( ip )
except:
print >>sys.stderr, "hostname lookup failed for %s" % ip
pass
try:
out['asn'] = ip2asn( ip )
except:
print >>sys.stderr, "asn lookup failed for %s" % ip
pass
if 'partials' in j and len( j['partials'] ) > 0:
for p in j['partials']:
if not have_result and p['engine'] in ('probelocations','crowdsourced','ixp') and len( p['locations'] ) > 0:
#print '# %s' % ( p['locations'][0], )
for loc in p['locations']:
if 'cityNameAscii' in loc:
out['lat'] = loc['latitude']
out['lon'] = loc['longitude']
out['location'] = u"%s,%s" % ( loc['cityNameAscii'], loc['countryCodeAlpha2'] )
# {"ip": "213.19.197.126", "hostname": "infopact-ne.ear3.amsterdam1.level3.net", "lon": null, "location": "", "lat": null, "asn": 3356}
#print u'%s %s "%s"' % ( ip, p['engine'], loc['cityName'], loc['countryCodeAlpha2'] )
have_result = True
# now get the probe lat/lon and compare to ipmap + RTT
if out['lon'] and out['lat']:
prb = run.probes_by_id[ iprtt[ ip ]['prb_id'] ]
geodist = haversine( out['lon'], out['lat'], prb['lon'], prb['lat'] )
rttdist = iprtt[ ip ]['rtt'] * 100
# don't make geo_err judgements when prb-ip distance is under 100km
if geodist < 100 or geodist < rttdist:
out['geo_error'] = False
else:
out['geo_error'] = True
g = out
g['prb_id'] = iprtt[ip]['prb_id']
g['msm_id'] = iprtt[ip]['msm_id']
g['rtt'] = iprtt[ip]['rtt']
g['geo_km'] = geodist
g['rtt_km'] = rttdist
print >>outf_geoerr, json.dumps( g )
## now reset the location, as we don't want it to be used in the real jedi
out['lat'] = None
out['lon'] = None
out['location'] = ''
print >>sys.stderr, "#RTT %s geo_km:%.2f rtt_km:%.2f prb_id:%s rtt:%.2f (%s)" % ( ip, geodist, rttdist, iprtt[ip]['prb_id'], iprtt[ip]['rtt'], out['geo_error'] )
else:
print >>sys.stderr, "#error: no lat/lon in new ipmap?! %s" % ( out )
break
except:
print >>sys.stderr, "#error on json for %s: %s" % (ip, j )
print >>outf, u'%s' % json.dumps( out )
if not have_result:
no_result_cnt += 1
#if 'locations' in j and len( j['locations'] ) > 0:
# do something smart with ipmap
# and write out to ipmap.json-fragments
# RIPEstat API slow/times out on very large ASNs
#asn_info = []
#for asn in asns:
# asnmeta = get_asnmeta( asn )
# print asnmeta
# asn_info.append( asnmeta )
#with open('asns.json','w') as asnf:
# json.dump(asn_info, asnf, indent=4)
main()