bgp- research & experiments with border gateway protocol |
git clone git://git.acid.vegas/bgp.git |
Log | Files | Refs | Archive | README |
pybgpstream-aspath.py (1511B)
1 import pybgpstream 2 import networkx as nx 3 from collections import defaultdict 4 from itertools import groupby 5 6 # Create an instance of a simple undirected graph 7 as_graph = nx.Graph() 8 9 bgp_lens = defaultdict(lambda: defaultdict(lambda: None)) 10 11 stream = pybgpstream.BGPStream( 12 # Consider this time interval: 13 # Sat, 01 Aug 2015 7:50:00 GMT - 08:10:00 GMT 14 from_time="2015-08-01 07:50:00", until_time="2015-08-01 08:10:00", 15 collectors=["rrc00"], 16 record_type="ribs", 17 ) 18 19 for rec in stream.records(): 20 for elem in rec: 21 # Get the peer ASn 22 peer = str(elem.peer_asn) 23 # Get the array of ASns in the AS path and remove repeatedly prepended ASns 24 hops = [k for k, g in groupby(elem.fields['as-path'].split(" "))] 25 if len(hops) > 1 and hops[0] == peer: 26 # Get the origin ASn 27 origin = hops[-1] 28 # Add new edges to the NetworkX graph 29 for i in range(0,len(hops)-1): 30 as_graph.add_edge(hops[i],hops[i+1]) 31 # Update the AS path length between 'peer' and 'origin' 32 bgp_lens[peer][origin] = \ 33 min(list(filter(bool,[bgp_lens[peer][origin],len(hops)]))) 34 35 # For each 'peer' and 'origin' pair 36 for peer in bgp_lens: 37 for origin in bgp_lens[peer]: 38 # compute the shortest path in the NetworkX graph 39 nxlen = len(nx.shortest_path(as_graph, peer, origin)) 40 # and compare it to the BGP hop length 41 print((peer, origin, bgp_lens[peer][origin], nxlen)) 42 43