-
Notifications
You must be signed in to change notification settings - Fork 30
/
json2traceroute.py
74 lines (65 loc) · 1.94 KB
/
json2traceroute.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
#!/usr/bin/python
import sys
import json
from cymruwhois import Client
import cPickle as pickle
import time
if len(sys.argv) != 2:
print >> sys.stderr, "Usage: json2traceroute.py file"
sys.exit(1)
json_file = sys.argv[1]
data = json.load(file(json_file))
def whoisrecord(ip):
currenttime = time.time()
ts = currenttime
if ip in whois:
ASN,ts = whois[ip]
else:
ts = 0
if ((currenttime - ts) > 36000):
C = Client()
ASN = C.lookup(ip)
whois[ip] = (ASN,currenttime)
return ASN
try:
pkl_file = open('whois.pkl', 'rb')
whois = pickle.load(pkl_file)
except IOError:
whois = {}
# Create traceroute output
try:
for probe in data:
probefrom = probe["from"]
if probefrom:
ASN = whoisrecord(probefrom)
print "From: ",probefrom," ",ASN.asn," ",ASN.owner
print "Source address: ",probe["src_addr"]
print "Probe ID: ",probe["prb_id"]
result = probe["result"]
for proberesult in result:
ASN = {}
if "result" in proberesult:
print proberesult["hop"]," ",
hopresult = proberesult["result"]
rtt = []
hopfrom = ""
for hr in hopresult:
if "error" in hr:
rtt.append(hr["error"])
elif "x" in hr:
rtt.append(hr["x"])
elif "edst" in hr:
rtt.append("!")
else:
rtt.append(hr["rtt"])
hopfrom = hr["from"]
ASN = whoisrecord(hopfrom)
if hopfrom:
print hopfrom," ",ASN.asn," ",ASN.owner," ",
print rtt
else:
print "Error: ",proberesult["error"]
print ""
finally:
pkl_file = open('whois.pkl', 'wb')
pickle.dump(whois, pkl_file)