Sunday, June 10, 2018

Checking Advertised BGP Routes To Peers - With PYEZ’S TABLE/VIEW


BGP doesn’t need any introduction because whenever we talk out highly scalable networks or Internet, we talk about BGP only. This is the best protocol to transport any number of routes. Whenever service provider or any customer runs BGP with its internal or external neighbors, they want to see what the BGP advertised routes to the neighbors are.

Every service provider or end customer wants to know what are the advertised routes towards the upstream peering so that during time of outages routes manipulation can be taken care. In this post, I have written a script by using the BGP Table View which can be used to dynamically create a view what is being advertised to peers by using different attributes mentioned in XML table.

Previous post “Display Static Routes With PyEZ Table and View” helps to understand how PYeZ framework can be used to display the static routes. I am using the same framework with different RPCs to show how dynamically we can display per peer neighbor advertised routes.

To achieve this, we need to know how many BGP neighbors we have and how many prefixes are advertised to each BGP neighbor.
Below is the python code which has been written to get the desired output of advertised route per neighbor.
       

from jnpr.junos import Device
from jnpr.junos.factory.factory_loader import FactoryLoader
import yaml
from jnpr.junos.op.xcvr import XcvrTable

yml = """
---
bgpAdvertiseRoutes:
 rpc: get-route-information
 args:
  advertising-protocol-name: bgp
  extensive: True
 item: route-table/rt 
 key:  rt-destination
 view: bgpAdvertiseView

bgpAdvertiseView:
 fields:
  rt_destination: rt-destination
  rt_prefix_length: rt-prefix-length
  rt_protocol_name: rt-entry/protocol-name
  bgp_group_name: rt-entry/bgp-group-name
  bgp_nh: rt-entry/nh/to
  rt_med: rt-entry/med
  rt_local_preference: rt-entry/local-preference
  rt_as_path: rt-entry/as-path
  rt_communities: rt-entry/communities/community
  
bgpSummary:
 rpc: get-bgp-summary-information
 
 
  """
globals().update(FactoryLoader().load(yaml.load(yml)))

with Device('your device ip address', port='7020', user='useryourname',passwd='yourpassword') as dev:
 op = dev.rpc.get_bgp_summary_information()
 for i in op.xpath('bgp-peer/peer-address'):
  #print(i.text)
  load_bgp = bgpAdvertiseRoutes(dev).get(neighbor=i.text)
  print("\n-----------------BGP Advertising Routes For:", i.text, "-----------------")
  for item in load_bgp:
   print('----------------------------------------------')
   print("Advertising_Route:",item.rt_destination,"Prefix_Lengh:",item.rt_prefix_length,"MED:",item.rt_med,"LP:",item.rt_local_preference,"AS_Path:",item.rt_as_path, "Communities:",item.rt_communities)



After running the above code, below is the output which can be visible on your python screen. Do ignore the public ip addresses, this is the lab network and only reachable in lab and has nothing to do with internet routes. In my lab, I have two peers and I am advertising the same routes to both the peers with communities.
       
C:\Python34>python c:\Lab\PyEZ\vmx_facts.py

-----------------BGP Advertising Routes For: 22.22.22.22 -----------------
----------------------------------------------
Advertising_Route: 2.2.2.8 Prefix_Lengh: 30 MED: 2 LP: 100 AS_Path: AS path: [100] I Communities: ['65001:11', '65001:33', '65001:172']
----------------------------------------------
Advertising_Route: 2.2.2.12 Prefix_Lengh: 30 MED: 2 LP: 100 AS_Path: AS path: [100] I Communities: ['65001:11', '65001:33', '65001:172']
----------------------------------------------
Advertising_Route: 2.2.2.16 Prefix_Lengh: 30 MED: 3 LP: 100 AS_Path: AS path: [100] I Communities: ['65001:11', '65001:33', '65001:172']
----------------------------------------------
Advertising_Route: 2.2.2.20 Prefix_Lengh: 30 MED: 3 LP: 100 AS_Path: AS path: [100] I Communities: ['65001:11', '65001:33', '65001:172']
----------------------------------------------
Advertising_Route: 22.22.22.22 Prefix_Lengh: 32 MED: 3 LP: 100 AS_Path: AS path: [100] I Communities: ['65001:11', '65001:33', '65001:172']
----------------------------------------------
Advertising_Route: 33.33.33.33 Prefix_Lengh: 32 MED: 1 LP: 100 AS_Path: AS path: [100] I Communities: ['65001:11', '65001:33', '65001:172']
----------------------------------------------
Advertising_Route: 44.44.44.44 Prefix_Lengh: 32 MED: 1 LP: 100 AS_Path: AS path: [100] I Communities: ['65001:11', '65001:33', '65001:172']
----------------------------------------------
Advertising_Route: 55.55.55.55 Prefix_Lengh: 32 MED: 2 LP: 100 AS_Path: AS path: [100] I Communities: ['65001:11', '65001:33', '65001:172']
----------------------------------------------
Advertising_Route: 66.66.66.66 Prefix_Lengh: 32 MED: 2 LP: 100 AS_Path: AS path: [100] I Communities: ['65001:11', '65001:33', '65001:172']

-----------------BGP Advertising Routes For: 33.33.33.33 -----------------
----------------------------------------------
Advertising_Route: 2.2.2.8 Prefix_Lengh: 30 MED: 2 LP: 100 AS_Path: AS path: [100] I Communities: ['65001:11', '65001:33', '65001:172']
----------------------------------------------
Advertising_Route: 2.2.2.12 Prefix_Lengh: 30 MED: 2 LP: 100 AS_Path: AS path: [100] I Communities: ['65001:11', '65001:33', '65001:172']
----------------------------------------------
Advertising_Route: 2.2.2.16 Prefix_Lengh: 30 MED: 3 LP: 100 AS_Path: AS path: [100] I Communities: ['65001:11', '65001:33', '65001:172']
----------------------------------------------
Advertising_Route: 2.2.2.20 Prefix_Lengh: 30 MED: 3 LP: 100 AS_Path: AS path: [100] I Communities: ['65001:11', '65001:33', '65001:172']
----------------------------------------------
Advertising_Route: 22.22.22.22 Prefix_Lengh: 32 MED: 3 LP: 100 AS_Path: AS path: [100] I Communities: ['65001:11', '65001:33', '65001:172']
----------------------------------------------
Advertising_Route: 33.33.33.33 Prefix_Lengh: 32 MED: 1 LP: 100 AS_Path: AS path: [100] I Communities: ['65001:11', '65001:33', '65001:172']
----------------------------------------------
Advertising_Route: 44.44.44.44 Prefix_Lengh: 32 MED: 1 LP: 100 AS_Path: AS path: [100] I Communities: ['65001:11', '65001:33', '65001:172']
----------------------------------------------
Advertising_Route: 55.55.55.55 Prefix_Lengh: 32 MED: 2 LP: 100 AS_Path: AS path: [100] I Communities: ['65001:11', '65001:33', '65001:172']
----------------------------------------------
Advertising_Route: 66.66.66.66 Prefix_Lengh: 32 MED: 2 LP: 100 AS_Path: AS path: [100] I Communities: ['65001:11', '65001:33', '65001:172']

References: STEPHAN BEHRENS has written great post on "CHECKING BGP WITH PYEZ’S TABLE/VIEW".
Click Here To Read Rest Of The Post...