This entry is part 3 of 5 in the series Getting started with Cisco OnePK

The first logical step in controlling your network with python is to have a way of pulling important information from your network devices. This post will cover the gathering of interface and routing table information. In order to keep examples short and to the point, they  will use the one_connect.py script as a connection module as discussed in the second post of the series. My goal for these examples isn’t to show what you should have in your OnePk apps, but rather to introduce you to basic concepts so you can see how the different pieces work and how you might be able to get the information necessary in your applications.

Collect Interface IP and IPv6 addresses

This first example is similar to a show ip interface brief and show ipv6 interface brief at the same time. The script finds all interfaces on the device and prints out the address list. I’ll explain simple modifications you can make after you see the example below (you can also see it on GitHub here):

The output of this will look similar (but with different IP addresses) to this:

For this example, we needed to create an InterfaceFilter() object which takes in either an interface name, or an interface type to search for. An interface_type of 1 represents all interfaces types, so I’m really just telling the get_interface)list() method to return a list of all interface objects on the Network Element. Read more about the InterfaceFilter() object and interface types here.

Instead of just printing the interface IP(v6) addresses, we could also print the full L2 switchport information, similar to the output of show interface [Type0/1] switchport by using this code in the try loop:

Go ahead and run that to see what the output looks like, I’ll leave it as a surprise for you.

Print Route Table Information

Up next is an example that will gather information and print out something similar to the route table. This example has a lot more moving pieces than the previous one, so I’ll break it down after you see the code:

Which gives the output:

I hope you see that there’s a lot of room for customization in this example. The exact formatting of the information can be represented in an infinite number of ways, and there’s even more attributes for the NextHop() and Route() objects that are available (read about them here).

This example could easily be limited to either IPv4 or IPv6 routes only by only using one of the L3UnicastScope.AFIType values, and you can add additional filtering for route types and prefixes also. For example, to find only routes with a prefix of ‘192.168.0.0/24’ or longer, use this for your range value:

Output:

___

This is just the start! The next post will cover interacting with interfaces to shutdown and change configuration. There’s a lot to the OnePK SDK and I can’t cover it all, so start by reading the API docs here. Also, feel free to ask questions and I’ll do my best to answer them.

Series Navigation<< OnePK – Connecting to a Network ElementOnePK – Interacting with Interfaces >>

Leave a Reply

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