-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-kml-from-measurement.py
805 lines (649 loc) · 24.3 KB
/
generate-kml-from-measurement.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2014 Pier Carlo Chiodi - http://www.pierky.com
# Licensed under The MIT License (MIT) - http://opensource.org/licenses/MIT
#
# The MIT License (MIT)
# =====================
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Part of this work is based on Raphael JavaScript Library by Dmitry Baranovskiy
# (http://raphaeljs.com/) and Dracula Graph Library by Johann Philipp Strathausen
# (http://www.graphdracula.net/).
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import os
import getopt
import json
import urllib
import ipdetailscache
import simplekml
import geoip2.database
import config
import time
from datetime import date
from html import HTML
import pprint
import math
__version__ = '0.1'
DATA_DIR = config.DATA_DIR
KML_FILE = config.KML_FILE
GEOIP_FILE = config.GEOIP_FILE
# DATA_DIR = ''.join([os.environ['HOME'], "/traceroute_cache"])
# KML_FILE = ''.join([os.environ['HOME'], "/output.kml"])
# GEOIP_FILE = ''.join([os.path.dirname(os.path.abspath(__file__)),"/GeoLite2-City.mmdb"])
# GEOIP_FILE = "/opt/geoip2/GeoLite2-City.mmdb"
try:
os.stat(DATA_DIR)
except:
os.mkdir(DATA_DIR)
APIKey = ''
MeasurementID = 0
ASPathRadiusFromTarget = 20
AlsoIncludeIncompletePaths = False
ForceDownload = False
def PrintLine(s):
print s
def PrintNoNewLine(s):
sys.stdout.write(s)
def PrintLine(s):
print s
def Usage():
PrintLine('')
PrintLine('ripe-kml-generator %s, Copyright (c) 2014 Mathias Handsche'
% __version__)
PrintLine('')
PrintLine('Usage: %s [-k api_key ] [-f] measurement_id'
% sys.argv[0])
PrintLine('')
PrintLine('Options:')
PrintLine(" -k RIPEAtlas API key to access the measurement")
PrintLine(" -f Skip local measurement cache and force its download")
PrintLine('')
# ------------------------------------------------------------------
def print_descr(tbl_el):
table = '<table>'
table += '<tr><td><b>Probe ID</b></td><td>' + str(tbl_el['Probe ID'
]) + '</td>'
table += '<tr><td><b>IPv4 ASN</b></td><td>' + tbl_el['IPv4 ASN'] \
+ '</td>'
table += '<tr><td><b>IPv4 Prefix</b></td><td>' \
+ tbl_el['IPv4 Prefix'] + '</td>'
if tbl_el['IPv6 ASN'] != 'NA':
table += '<tr><td><b>IPv6 ASN</b></td><td>' + tbl_el['IPv6 ASN'
] + '</td>'
if tbl_el['IPv6 Prefix'] != 'NA':
table += '<tr><td><b>IPv6 Prefix</b></td><td>' \
+ tbl_el['IPv6 Prefix'] + '</td>'
if tbl_el['Country Code'] != 'NA':
table += '<tr><td><b>Country Code</b></td><td>' \
+ tbl_el['Country Code'] + '</td>'
if tbl_el['Up since'] != 'NA':
table += '<tr><td><b>Up since</b></td><td>' + tbl_el['Up since'
] + '</td>'
if tbl_el['Down since'] != 'NA':
table += '<tr><td><b>Down since</b></td><td>' \
+ tbl_el['Down since'] + '</td>'
table += '</table>'
return str(table)
def getProbeInfo():
url = 'https://atlas.ripe.net/contrib/active_probes.json'
js = '%s/probes.json' % DATA_DIR
if os.path.isfile(js) and os.path.getctime(js) > time.time() \
- 86400:
PrintLine('Active Probes Json up to date')
else:
try:
urllib.urlretrieve(url, js)
except:
PrintLine("Can't download Active Probes")
quit()
json_data = open(js)
j = json.load(json_data)
json_data.close()
hashr = {}
for e in j:
d = {}
up = e[0]
d['lat'] = e[1]
d['lon'] = e[2]
a = e[3]
for i in range(0, len(a), 2):
d[a[i]] = a[i + 1]
if 'IPv6 Prefix' not in d.keys():
d['IPv6 Prefix'] = 'NA'
else:
up = 'UP6'
if 'IPv6 ASN' not in d.keys():
d['IPv6 ASN'] = 'NA'
else:
up = 'UP6'
if 'IPv4 Prefix' not in d.keys():
d['IPv4 Prefix'] = 'NA'
if 'IPv4 ASN' not in d.keys():
d['IPv4 ASN'] = 'NA'
if 'Country Code' not in d.keys():
d['Country Code'] = 'NA'
if 'Up since' not in d.keys():
d['Up since'] = 'NA'
if 'Down since' not in d.keys():
d['Down since'] = 'NA'
descr = print_descr(d)
d['desc'] = descr
hashr[d['Probe ID']] = d
return hashr
def distance_on_unit_sphere(
lat1,
long1,
lat2,
long2,
):
if lat1 == lat2 and long1 == long2:
return 0
PrintLine('Latitude: %.4f Longitude: %.4f \nLatitude: %.4f Longitude: %.4f'
% (lat1, long1, lat2, long2))
# Convert latitude and longitude to
# spherical coordinates in radians.
degrees_to_radians = math.pi / 180.0
# phi = 90 - latitude
phi1 = (90.0 - lat1) * degrees_to_radians
phi2 = (90.0 - lat2) * degrees_to_radians
# theta = longitude
theta1 = long1 * degrees_to_radians
theta2 = long2 * degrees_to_radians
# Compute spherical distance from spherical coordinates.
# For two locations in spherical coordinates
# (1, theta, phi) and (1, theta, phi)
# cosine( arc length ) =
# sin phi sin phi' cos(theta-theta') + cos phi cos phi'
# distance = rho * arc length
cos = math.sin(phi1) * math.sin(phi2) * math.cos(theta1 - theta2) \
+ math.cos(phi1) * math.cos(phi2)
arc = math.acos(cos)
# Remember to multiply arc by the radius of the earth
# in your favorite set of units to get length.
return arc * 6373
def median(mylist):
if len(mylist) == 0:
return 0
sorts = sorted(mylist)
length = len(sorts)
if not length % 2:
return (sorts[length / 2] + sorts[length / 2 - 1]) / 2.0
return sorts[length / 2]
def color_selctor(latency, distance):
expected_latency = distance / 200 * 2 # Fuer RTT
if latency <= expected_latency * 1: # wahrscheinlich fehler da zu schnell
colorhex = 'FF0000'
elif latency <= expected_latency * 1.5:
colorhex = '00FF00'
elif latency <= expected_latency * 2:
colorhex = 'FFFF00'
else:
colorhex = 'FF0000'
return simplekml.Color.hex(colorhex)
# ------------------------------------------------------------------
try:
(opts, args) = getopt.getopt(sys.argv[1:], 'hifr:k:')
for (opt, arg) in opts:
if opt == '-k':
APIKey = arg
elif opt == '-f':
ForceDownload = True
elif opt == '-h':
Usage()
quit()
else:
PrintLine('Unexpected option: %s' % opt)
Usage()
quit()
if len(args) < 1:
PrintLine('Measurement ID not specified')
Usage()
quit()
else:
if args[0].isdigit():
MeasurementID = int(args[0])
if MeasurementID <= 0:
PrintLine('Measurement ID must be > 0')
Usage()
quit()
else:
PrintLine('Number expected for measurement ID')
Usage()
quit()
except getopt.GetoptError:
Usage()
try:
IPCache = ipdetailscache.IPDetailsCache(Debug=False,
IP_ADDRESSES_CACHE_FILE='%s/ip_addr.cache' % DATA_DIR,
IP_PREFIXES_CACHE_FILE='%s/ip_pref.cache' % DATA_DIR)
except:
PrintLine('Error opening the IP details cache. Verify write permissions on %s for the running user.'
% DATA_DIR)
quit()
DataOK = False
if not ForceDownload:
try:
json_data = open('%s/m%s.json' % (DATA_DIR, MeasurementID))
data = json.load(json_data)
json_data.close()
DataOK = True
except:
pass
if not DataOK:
try:
urllib.urlretrieve('https://atlas.ripe.net/api/v1/measurement/%s/result/?key=%s'
% (MeasurementID, APIKey), '%s/m%s.json'
% (DATA_DIR, MeasurementID))
except:
PrintLine("Can't download measurement results.")
PrintLine('Verify that measurement ID (%d) is correct and that measurement is public, otherwise set the correct API key with -k option.'
% MeasurementID)
PrintLine('Check also that local directory (%s) has write permissions for the current user.'
% DATA_DIR)
quit()
try:
json_data = open('%s/m%s.json' % (DATA_DIR, MeasurementID))
data = json.load(json_data)
json_data.close()
DataOK = True
except:
pass
if not DataOK:
PrintLine("Can't get JSON data.")
quit()
# ------------------------------------------------------------------
h = getProbeInfo()
Rows = []
ASPaths = [] # [ [ { "ASN": xxx, "Holder": "xxx" }, ... ] ]
InvalidProbes = []
ProbesWithErrors = []
IncompleteProbes = []
ValidProbesCount = 0 # only results with one or more IP addresses different from both the source and the target in the middle of the path are considered valid
kml = simplekml.Kml()
reader = geoip2.database.Reader(GEOIP_FILE)
probe_details = getProbeInfo()
doc = kml.newdocument(name='UDM: ' + str(MeasurementID))
kml.document = doc
h = HTML('html')
t = h.table(border='1')
r = t.tr
r.td('Measurement ID')
r.td(str(MeasurementID))
r = t.tr
r.td('Destination IP')
r.td(str(data[0]['dst_addr']))
r = t.tr
r.td('Destination Name')
r.td(str(data[0]['dst_name']))
r = t.tr
r.td('Starttime:')
r.td(date.fromtimestamp(int(data[0]['timestamp'
])).strftime('%Y-%m-%d %H:%M:%S'))
r = t.tr
r.td('Endtime')
if 'endtime' in data[0]:
r.td(date.fromtimestamp(int(data[0]['endtime'
])).strftime('%Y-%m-%d %H:%M:%S'))
else:
r.td('NA')
r = t.tr
r.td('IP Version')
r.td(str(data[0]['af']))
r = t.tr
r.td('Paris ID')
if 'paris_id' in data[0]:
r.td(str(data[0]['paris_id']))
else:
r.td('NA')
r = t.tr
r.td('Protocol')
r.td(str(data[0]['proto']))
r = t.tr
r.td('Size of Packets')
r.td(str(data[0]['size']))
PrintLine(type(str(h)))
PrintLine(str(h))
doc.description = str(h)
# Probe Result
for result in data:
IPAddress = result['from']
if IPAddress == '':
IPAddress = result['src_addr']
if IPAddress == '':
continue
ProbeID = str(result['prb_id'])
if not int(ProbeID) in probe_details:
PrintLine('ProbeID to skip: ' + ProbeID)
continue
ASPath = []
TracerouteCompleted = False
Valid = False
# l = []
# l.append(IPAddress)
# l.append(geo.location.latitude)
# l.append(geo.location.longitude)
#
# m = IPAddress + geo.location.latitude + geo.location.longitude
# PrintLine(m)
IPAddressDetails = IPCache.GetIPInformation(IPAddress)
# PrintLine(IPAddressDetails)
fol = doc.newfolder(name=ProbeID)
# fol.description()
firstline = True
pnt = fol.newpoint(name=ProbeID, description=''.join([
'IP Address: ',
IPAddress,
'\nPrefix: ',
IPAddressDetails['Prefix'],
'\nASN: ',
IPAddressDetails['ASN'],
'\nHostname: ',
IPAddressDetails['HostName'],
'\nHolder: ',
IPAddressDetails['Holder'],
]), coords=[(probe_details[int(ProbeID)]['lon'],
probe_details[int(ProbeID)]['lat'])]) # coords=[(geo.location.longitude,geo.location.latitude)]
pnt.style.iconstyle.icon.href = \
'http://maps.google.com/mapfiles/kml/shapes/sunny.png'
pnt.style.iconstyle.scale = 3
ASPath.append({'ASN': IPAddressDetails['ASN'],
'Holder': IPAddressDetails['Holder']})
Cols = []
last_rtt = 0
last_longitude = 0
last_latitude = 0
last_IPAddress = ''
Cols.append(result['prb_id'])
Cols.append('{: <5}'.format('From'))
Cols.append(IPAddress)
Cols.append(IPAddressDetails['ASN'])
Cols.append(IPAddressDetails['Holder'])
Cols.append(IPAddressDetails['Prefix'])
Cols.append(IPAddressDetails['HostName'])
Cols.append(0)
Cols.append(255)
Cols.append(0)
Rows.append(Cols)
# Hops from each probe
for hop in result['result']:
if hop.get('error', '') == '':
LastIPAddress = ''
listrtt = []
for rttcounter in hop['result']:
if rttcounter.get('rtt', '') != '':
listrtt.append(float(rttcounter['rtt']))
for response in hop['result']:
if response.get('from', '') != '':
IPAddress = response['from']
PrintLine('Respone ----------------- ')
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(response)
PrintLine('Respone ----------------- ')
Valid = Valid or IPAddress != result['from'] \
and IPAddress != result['dst_addr']
if IPAddress != LastIPAddress:
IPAddressDetails = \
IPCache.GetIPInformation(IPAddress)
if IPAddressDetails['ASN'].isdigit():
if not IPAddressDetails['ASN'] in [AS['ASN'
] for AS in ASPath]:
ASPath.append({'ASN': IPAddressDetails['ASN'
],
'Holder': IPAddressDetails['Holder'
]})
Cols = []
Cols.append(result['prb_id'])
# traceroute complete?
if result['dst_addr'] == IPAddress:
TracerouteCompleted = True
Cols.append('{:<5}'.format('To'))
else:
Cols.append(' {:2d}. '.format(hop['hop']))
Cols.append(IPAddress)
Cols.append(IPAddressDetails['ASN'])
Cols.append(IPAddressDetails['Holder'])
Cols.append(IPAddressDetails['Prefix'])
Cols.append(IPAddressDetails['HostName'])
Cols.append('%.2f' % median(listrtt))
Cols.append(response['ttl'])
# Rows.append( Cols )
LastIPAddress = IPAddress
geo_available = True
try:
geo = reader.city(IPAddress)
except:
geo_available = False
if geo_available == False:
PrintLine('IP: ' + IPAddress
+ ' Geo Not Available')
continue
else:
PrintLine('IP: ' + IPAddress + ' Long: '
+ str(geo.location.longitude)
+ ' Lat: '
+ str(geo.location.latitude))
if last_latitude == 0 and last_longitude == 0:
last_latitude = \
float(probe_details[int(ProbeID)]['lat'
])
last_longitude = \
float(probe_details[int(ProbeID)]['lon'
])
last_rtt = median(listrtt)
Cols.append(0)
last_IPAddress = IPAddress
else:
distance = '%d' \
% distance_on_unit_sphere(last_latitude,
last_longitude,
geo.location.latitude,
geo.location.longitude)
line = fol.newlinestring(
name=''.join(['Path: ', last_IPAddress,
'-', IPAddress]),
coords=[(geo.location.longitude,
geo.location.latitude),
(last_longitude,
last_latitude)],
altitudemode=simplekml.GxAltitudeMode.clampToSeaFloor,
extrude=1,
tessellate=1,
visibility=1,
)
rtt = float(median(listrtt)) \
- float(last_rtt)
line.description = 'Distance: %s km \n' \
% distance + 'RTT: ' + str(rtt) + ' ms'
line.style.linestyle.color = \
color_selctor(rtt, int(distance))
last_rtt = median(listrtt)
last_latitude = geo.location.latitude
last_longitude = geo.location.longitude
Cols.append(distance)
Rows.append(Cols)
fol.newpoint(name=''.join([str(result['prb_id'
]), '_', str(hop['hop'])]),
description=''.join([
'IP Address: ',
IPAddress,
'\nPrefix: ',
IPAddressDetails['Prefix'],
'\nASN: ',
IPAddressDetails['ASN'],
'\nHostname: ',
IPAddressDetails['HostName'],
'\nHolder: ',
IPAddressDetails['Holder'],
]), coords=[(geo.location.longitude,
geo.location.latitude)])
else:
ProbesWithErrors.append(result['prb_id'])
break
if not result['prb_id'] in ProbesWithErrors:
if Valid:
if not TracerouteCompleted:
IncompleteProbes.append(result['prb_id'])
if ASPath != []:
if TracerouteCompleted or AlsoIncludeIncompletePaths:
ValidProbesCount = ValidProbesCount + 1
ASPaths.append(ASPath)
else:
InvalidProbes.append(result['prb_id'])
sys.stdout.write('\rProcessed probes: %d valid, %d with errors, %d incomplete paths'
% (ValidProbesCount, len(ProbesWithErrors),
len(IncompleteProbes)))
sys.stdout.flush()
sys.stdout.write('\r')
PrintLine('')
# ------------------------------------------------------------------
ColsMaxLen = []
for Col in zip(*Rows):
ColsMaxLen.append(max(len(str(Val)) for Val in Col))
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(ColsMaxLen)
LastProbeID = 0
LastASN = ''
kl = {}
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(Rows)
for R in Rows:
if R[0] != LastProbeID:
if LastProbeID != 0:
if LastProbeID in ProbesWithErrors:
PrintLine('')
PrintLine(' With errors')
else:
if LastProbeID in InvalidProbes:
PrintLine('')
PrintLine(' Invalid')
else:
if LastProbeID in IncompleteProbes:
PrintLine('')
PrintLine(' Incomplete')
PrintLine('')
LastProbeID = R[0]
LastASN = ''
kl[LastProbeID] = ''
PrintLine('Probe %s' % LastProbeID)
kl[LastProbeID] += 'Probe %s' % LastProbeID + '\n'
PrintLine('')
kl[LastProbeID] += '\n'
if R[3] != 'unknown':
if R[3] != LastASN:
if LastASN != '':
PrintLine('')
kl[LastProbeID] += '\n'
if R[4] != '':
PrintLine(' ASN %s - %s' % (R[3], R[4]))
kl[LastProbeID] += ' ASN %s - %s' % (R[3], R[4]) + '\n'
else:
PrintLine(' ASN %s' % R[3])
kl[LastProbeID] += ' ASN %s' % R[3] + '\n'
LastASN = R[3]
PrintNoNewLine(' %s' % R[1]) # hop n.
kl[LastProbeID] += ' %s' % R[1]
PrintNoNewLine(('{: >%d}' % ColsMaxLen[2]).format(R[2])) # IP
kl[LastProbeID] += ('{: >%d}' % ColsMaxLen[2]).format(R[2])
if R[5] != '':
PrintNoNewLine((' - {: >%d}' % ColsMaxLen[5]).format(R[5])) # Prefix
kl[LastProbeID] += (' - {: >%d}' % ColsMaxLen[5]).format(R[5])
if len(R) >= 8 and R[7] != '':
PrintNoNewLine((' - {: >%d} ms'
% ColsMaxLen[7]).format(float(R[7]))) # RTT
kl[LastProbeID] += (' - {: >%d} ms'
% ColsMaxLen[7]).format(float(R[7]))
if len(R) >= 10 and R[9] != '':
PrintNoNewLine((' - Distance {: >%d} km'
% ColsMaxLen[9]).format(R[9])) # Distance
kl[LastProbeID] += (' - Distance {: >%d} km'
% ColsMaxLen[9]).format(R[9])
if len(R) >= 9 and R[8] != '':
PrintNoNewLine((' - TTL {: >%d}' % ColsMaxLen[8]).format(R[8])) # TTL
kl[LastProbeID] += (' - TTL {: >%d}'
% ColsMaxLen[8]).format(R[8])
if R[6] != 'unknown' and R[6] != '':
PrintLine(', %s' % R[6]) # Hostname
kl[LastProbeID] += ', %s' % R[6] + '\n'
else:
PrintLine('')
kl[LastProbeID] += '\n'
PrintLine('Meine Ausgabe\n ----------------')
pp.pprint(kl)
PrintLine('Meine Ausgabe\n ----------------')
PrintLine('')
if len(IncompleteProbes) > 0:
PrintLine('Valid probes: %s - %s incomplete (%s)'
% (ValidProbesCount, len(IncompleteProbes),
', '.join(map(str, IncompleteProbes))))
else:
PrintLine('Valid probes: %s' % ValidProbesCount)
PrintLine('')
if len(InvalidProbes) > 0:
PrintLine('Invalid probes: %s (%s)' % (len(InvalidProbes),
', '.join(map(str, InvalidProbes))))
PrintLine('')
if len(ProbesWithErrors) > 0:
PrintLine('Probes with errors: %s (%s)' % (len(ProbesWithErrors),
', '.join(map(str, ProbesWithErrors))))
PrintLine('')
# ------------------------------------------------------------------
PrintLine('Considering the last %d ASes for each path, this is the number of probes which reach the target through them:'
% ASPathRadiusFromTarget)
SelectedASPaths = [ASPath[-ASPathRadiusFromTarget:] for ASPath in
ASPaths]
DistinctASPaths = []
for ASPath in SelectedASPaths:
if not ASPath in DistinctASPaths:
DistinctASPaths.append(ASPath)
Rows = []
LastRowID = 0
for ASPath in DistinctASPaths:
LastRowID = LastRowID + 1
for AS in ASPath:
Cols = []
Cols.append(LastRowID)
Cols.append('%d probes' % SelectedASPaths.count(ASPath))
Cols.append(AS['ASN'])
Cols.append(AS['Holder'])
Rows.append(Cols)
ColsMaxLen = []
for Col in zip(*Rows):
ColsMaxLen.append(max(len(str(Val)) for Val in Col))
LastRowID = 0
for R in Rows:
if R[0] != LastRowID:
PrintLine('')
PrintNoNewLine(('{: >%d}: ' % ColsMaxLen[1]).format(R[1]))
LastRowID = R[0]
else:
PrintNoNewLine(('{: >%d} ' % ColsMaxLen[1]).format(' '))
PrintNoNewLine(('{: >%d}, ' % ColsMaxLen[2]).format(R[2]))
PrintLine(R[3])
PrintLine('')
# ------------------------------------------------------------------
PrintLine(kml.allcontainers)
for b in kml.allcontainers:
b.description = kl[int(b.name)]
PrintLine(b.description)
kml.save(KML_FILE)
PrintLine(''.join(['KML File writen to: ', KML_FILE]))