Checking BGP with PyEZ’s Table/View

In my last blog post, I have given a short introduction into Junos PyEZ. Now I will show you how to check BGP using Table/View and how you can create a Table/View from scratch. You can find a basic example to check BGP routes via Junos PyEZ Table/View by Rick Sherman in the official Google Group. I will use this example as a starting point.

Using PyEZ’s Table/Views

In the last past weeks, I have seen a few PyEZ projects where the information has been caught by executing an XML-RPC and parsing the whole XML-response. In my opinion using Tables and Views is simpler and a better approach. You do not have to parse any XML-structure by yourself. As already posted in my last blog post below you will find some useful J-NET community links about Table/View:

BGP-setup / Lab topology

Virtual lab topologyFor my tests, I use a very simple virtual lab topology with two vMX connected via two interfaces (ge-0/0/0 and ge-0/0/1). Both routers (vMX1 and vMX2) have an internal BGP session, and on vMX2 I’m exporting three static routes (, and into BGP. As you can see within the “show route”-output below, vMX1 receives these three routes.

I will use this simple setup for catching the BGP information from vMX1 through Junos PyEZ.

Checking BGP routes

The following Python script (Google Group) connects to the vMX1 and catches all BGP-routes, similar to the “show route protocol bgp extensive”-command:

Executing the script will give us the following output:

That’s easy, isn’t it? 🙂 Before creating a new Table/View for checking the BGP neighbor-state we have to understand the different elements.


With the Table, you specify which information you want to request. First of all, there is the Table name:

You can give it the name you want, and it will be used for catching the information:

After the name of the Table you have to declare what information you want to pick. It is similar to the regular “show”-commands you can execute on the device. We want to get the “show route”-output for protocol bgp (“show route protocol bgp extensive”). So we must first define the corresponding XML-RPC via “rpc:”. You can check on the vMX1 by entering the “show route | display xml rpc”-command:

This will give you the “get-route-information”. Also, you see the argument “<protocol>bgp</protocol>” which you have to define through “args:” if you want to filter the routes for protocol bgp. However, additional arguments are optional. Because we want to get as much information as possible we also declare “extensive: true” for an extensive output:

Then you have to declare the XML-paths for the item and the key-value of each item:

If you have a look at the XML-output which we get on vMX1 for the command “show route protocol bgp extensive | display xml” you will see that each route is represented in a separate <rt></rt> tag. So as we want to have each route as an item, we declare the XML path “route-table/rt”. The key-value – the unique identifier of each item – is optional and will be the route itself via “rt-destination”.


With the View, you can declare your item fields which are filled with the information of the XML-output. So this is a mapping of the information to your item’s fields. This is where the magic happens 🙂 In our Python script we declared the following data fields:

If we now take the XML-output and delete all the XML-tags we do not use we will get the following result:

This should clarify the XML-paths and how to get them 🙂

Creating Table/View for checking BGP neighborship

With the Python script above we get the output of all BGP routes. Now we will create a Table and View for checking the BGP neighbor state – similar to the “show bgp neighbor” command.

Table: bgpNeighbor

First, we will add the Table, which we name “bgpNeighbor.” Then we have to add the XML-RPC call. We get the XML-RPC via the “show bgp neighbor | display xml rpc”-output:  “get-bgp-neighbor-information”.

As item we declare the “bgp-peer”-entry – so every BGP neighbor will be matched to a unique item. You can identify which attribute you should use as item via the show-command with the “display xml” option (“show bgp neighbor | display xml”). It gives you the XML-output, where you can see that the XML-tag <bgp-peer> represents a single BGP neighbor. The “peer-address” will be the item’s key-value.

The View (or the mapping of all values to our own variables) will be named as “bgpNeighborView.” This will lead to the following YAML-lines:

View: bgpNeighborView

Now we have to add the View for the mapping of the XML-values to our fields. So we go through the XML-output, and we search for all the information we want to display. In our example, we will write down the BGP neighbors IP-address, the state of the neighborship (like established) and the BGP group-type (internal/external).

In addition to this, we will display the received, activated and accepted Routes. If you have a look at the XML-output you will see that this information is shown in the sub-tag “<bgp-rib>”:

Therefore you have to add the full XML-path via “bgp-rib/”. For the received routes this will lead to the statement “rt_rescv: bgp-rib/received-prefix-count” where “rt_rescv” is your item’s field. Following this scheme also for the active and accepted route this will give you the following View:

Now we are almost done 🙂 With adding output generation we will get the following Python-code:

And if we now execute the Python script we will get the following output:

I hope that this blog entry was useful for you and that you now know how to use PyEZ’s Table/View feature. I have omitted a few topics like filtering via the get method or some further tweaking of the Table/View. Thanks for reading this article.


4 comments on “Checking BGP with PyEZ’s Table/View”

  1. Hi,
    i have a case like this. One router have many bgp sessions. I want to know that one prefix was advertised to which bgp sessions?
    show route advertising-protocol bgp A x.x.x.x/24
    show route advertising-protocol bgp B x.x.x.x/24
    show route advertising-protocol bgp C x.x.x.x/24
    Can you help me to do it? or any similar script?

  2. Hey Stephan,
    many of you’re code listings contain html tags. Obvious in config snippets but pretty hard when it comes to xml ;-/


    1. Hi Markus, thanks for the hint. Yes, I changed some stuff with the WordPress plugins, and unfortunately, there are now some HTML tags in older blog articles. I will go through all code snippets and clean them up. Thanks, Stephan

Leave a Reply

Your email address will not be published. Required fields are marked *

© 2016 made with ♡ by stephan behrens - impress