ris.py: switch to dns query to have builtin cache

This commit is contained in:
Alarig Le Lay 2022-08-19 11:07:55 +02:00
parent 7bbdcb2204
commit 46a90db4f9
No known key found for this signature in database
GPG key ID: C865491BF197B54F

14
ris.py
View file

@ -1,13 +1,13 @@
#!/usr/bin/env python #!/usr/bin/env python
import argparse import argparse
import cymruwhois
import itertools import itertools
import json import json
import pydot import pydot
import random import random
import time import time
import urllib.request import urllib.request
from dns import resolver
parser = argparse.ArgumentParser(description=''' parser = argparse.ArgumentParser(description='''
Draw a graph from RIS BGP data. A PNG file will be generated with name Draw a graph from RIS BGP data. A PNG file will be generated with name
@ -46,18 +46,24 @@ edges = {}
dot_graph = pydot.Dot('BGPMAP', graph_type='digraph') dot_graph = pydot.Dot('BGPMAP', graph_type='digraph')
cymruwhois_client = cymruwhois.Client() resolv = resolver.Resolver()
resolv.timeout = 0.5
for peer in paths: for peer in paths:
print(peer, paths[peer]) print(peer, paths[peer])
string_prepended_path = ['AS'+v for v in paths[peer][:-1]] string_prepended_path = ['AS'+v for v in paths[peer][:-1]]
print(string_prepended_path) print(string_prepended_path)
cymruwhois_query = cymruwhois_client.lookupmany_dict(string_prepended_path)
for previous_asn, asn in pairwise(paths[peer]): for previous_asn, asn in pairwise(paths[peer]):
as_name = cymruwhois_query['AS'+previous_asn].owner try:
as_name = resolv.query(f"AS{previous_asn}.asn.cymru.com", "TXT")
as_name = as_name.response.answer[0].to_text().split('|')[4]
as_name = as_name.strip().replace("'","").replace('"','')
except Exception as e:
print(e, previous_asn)
as_name = ''
print(as_name) print(as_name)
edge_tuple = (previous_asn, asn) edge_tuple = (previous_asn, asn)