bgp

- research & experiments with border gateway protocol
git clone git://git.acid.vegas/bgp.git
Log | Files | Refs | Archive | README

rpki.py (1390B)

      1 #!/usr/bin/env python3
      2 
      3 from pybgpstream import BGPStream
      4 from ipaddress import ip_network
      5 import requests
      6 import sys
      7 import json
      8 import argparse
      9 
     10 # Initialize BGPStream, with routeviews-stream project, filtering for amsix.
     11 stream = BGPStream(project="routeviews-stream", filter="router amsix")
     12 print("starting stream...", file=sys.stderr)
     13 
     14 # Debug Option to limit number of traces
     15 parser = argparse.ArgumentParser()
     16 parser.add_argument("-d", "--debug", type=int, help="Number of traces")
     17 args = parser.parse_args()
     18 
     19 # Counter
     20 counter = 0
     21 for record in stream.records():
     22     # Handles debug option
     23     if args.debug is None:
     24         pass
     25     elif counter >= args.debug:
     26         break
     27     else:
     28         counter += 1
     29 
     30     for elem in record:
     31         prefix = ip_network(elem.fields['prefix'])
     32         if elem.type == "A":
     33             # Lookup RPKI state based on announced route.
     34             request = requests.get(f"https://api.routeviews.org/rpki?prefix={prefix}", verify=False)
     35             response = request.json()
     36             # Skip all None responses
     37             if response[str(prefix)] is not None:
     38                 data = {
     39                     "prefix": str(prefix),
     40                     "rpki": response[str(prefix)],
     41                     "timestamp": response[str(prefix)]['timestamp']
     42                 }
     43                 # Output json to stdout
     44                 print(json.dumps(data))