bgp

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

pybgpstream-communities.py (992B)

      1 #!/usr/bin/env python
      2 
      3 import pybgpstream
      4 from collections import defaultdict
      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=["rrc06"],
     11     record_type="ribs",
     12     filter="peer 25152 and prefix more 185.84.166.0/23 and community *:3400"
     13 )
     14 
     15 # <community, prefix > dictionary
     16 community_prefix = defaultdict(set)
     17 
     18 # Get next record
     19 for rec in stream.records():
     20     for elem in rec:
     21         # Get the prefix
     22         pfx = elem.fields['prefix']
     23         # Get the associated communities
     24         communities = elem.fields['communities']
     25         # for each community save the set of prefixes
     26         # that are affected
     27         for c in communities:
     28             community_prefix[c].add(pfx)
     29 
     30 # Print the list of MOAS prefix and their origin ASns
     31 for ct in community_prefix:
     32     print("Community:", ct, "==>", ",".join(community_prefix[ct]))
     33