bgp

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

pybgpstream-moas.py (1002B)

      1 #!/usr/bin/env python
      2 
      3 from collections import defaultdict
      4 import pybgpstream
      5 
      6 stream = pybgpstream.BGPStream(
      7     # Consider this time interval:
      8     # Sat, 01 Aug 2015 7:50:00 GMT -  08:10:00 GMT
      9     from_time="2015-08-01 07:50:00", until_time="2015-08-01 08:10:00",
     10     collectors=["rrc00"],
     11     record_type="ribs",
     12 )
     13 
     14 # <prefix, origin-ASns-set > dictionary
     15 prefix_origin = defaultdict(set)
     16 
     17 for rec in stream.records():
     18     for elem in rec:
     19         # Get the prefix
     20         pfx = elem.fields["prefix"]
     21         # Get the list of ASes in the AS path
     22         ases = elem.fields["as-path"].split(" ")
     23         if len(ases) > 0:
     24             # Get the origin ASn (rightmost)
     25             origin = ases[-1]
     26             # Insert the origin ASn in the set of
     27             # origins for the prefix
     28             prefix_origin[pfx].add(origin)
     29 
     30 # Print the list of MOAS prefix and their origin ASns
     31 for pfx in prefix_origin:
     32     if len(prefix_origin[pfx]) > 1:
     33         print((pfx, ",".join(prefix_origin[pfx])))
     34 
     35