diff --git a/main.py b/main.py index 33f46d9..739aca2 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,14 @@ -import rov +from rov import ROV +import json import csv +import re from pathlib import * manrs_asns = [] manrs_csv_filename = "manrs.csv" +canarie_route_filename = "canarie_routes.csv" +canarie_routes = {} +rov = ROV() def read_manrs_participant_file(filename): global manrs_asns @@ -15,21 +20,79 @@ def read_manrs_participant_file(filename): asn = asn.strip() manrs_asns.append(asn) +def read_canarie_routes(filename): + global canarie_routes + canarie_route_fn = Path.cwd() / filename + with open(canarie_route_fn) as csvfile: + fi_reader = csv.DictReader(csvfile, delimiter=',', quotechar='"') + for row in fi_reader: + #print(row) + route = row['route'] + origin = get_origin(row['as_path']) + canarie_routes[route] = origin + +def get_origin(as_path): + """ Given an as-path, return the origin ASN""" + p = re.compile(r'(\d+)') + asns = p.findall(as_path) + if asns: + origin = f"{asns[-1]}" + else: + origin = False + return origin + def is_manrs_participant(asn): global manrs_asns if len(manrs_asns) == 0: read_manrs_participant_file(manrs_csv_filename) - if asn.strip() in manrs_asns: - return True + asn = str(asn) + print(f"asn = {asn} in {manrs_asns}") + + if asn in manrs_asns: + return 'Yes' else: - return False + return 'No' +def rov_status(prefix, asn): + state = rov.check(prefix, asn) + print(prefix) + #print(json.dumps(state)) + return state + #print(state['irr']['descr']) -# This is a sample Python script. +def check_routes(report_filename): + global canarie_routes + report_filename= Path.cwd() / report_filename + with open(report_filename, 'w') as csvfile: + fieldnames = ['prefix', 'origin ASN', 'irr descr', 'rpki status', 'manrs participant'] + writer = csv.DictWriter(csvfile, fieldnames=fieldnames) + writer.writeheader() + for prefix in canarie_routes: + asn = canarie_routes[prefix] + state = rov_status(prefix, asn) + prefix = state['query']['prefix'] + asn = state['query']['asn'] + irr_descr = state['irr']['descr'] + rpki_status = state['rpki']['status'] + manrs_part = is_manrs_participant(asn) + writer.writerow({'prefix':prefix, + 'origin ASN':asn, + 'irr descr':irr_descr, + 'rpki status':rpki_status, + 'manrs participant': manrs_part}) + + + for prefix in canarie_routes: + asn = canarie_routes[prefix] + state = rov_status(prefix, asn) + + + +def init_rov(): + # rov.download_databases() + rov.load_databases() -# Press ⌃R to execute it or replace it with your code. -# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings. def print_hi(name): @@ -39,7 +102,9 @@ def print_hi(name): # Press the green button in the gutter to run the script. if __name__ == '__main__': - print(f"is ASN 123 {is_manrs_participant('123')}") - print_hi('PyCharm') + init_rov() + read_canarie_routes(canarie_route_filename) + check_routes('ca_report.csv') + # See PyCharm help at https://www.jetbrains.com/help/pycharm/