Python3 Network Programming – Building lab with Netmiko, using JSON formatting within the Py3 scripts for readability!

NapJSONTop

I’m going to build out this lab a little bit more using Netmiko scripting to push configs to SW1 so we get a bit more output to review, and we will also be using JSON within the script as well to make it more readable that a big mashed up blob of output! ๐Ÿ™‚

First things first lets config NTP / Logging / SVI 20 / Int 20 / Configure host interfaces

Being that I keep all of my scripts of a rainy day, I can add a few extra lines to my Netmiko script used to configure SW1 as a “Core” switch in a previous lab, and just add a few extra lines of configuration for the VLAN and turning up its interfaces for the PC’s.

For this I will be using Netmiko to open this file to configure SW1 a bit further:

SwitchConfigfile

From old scripts I just clip out the pieces I need to make this new script for SW1:

NikoPalmLab1

So I throw both of those and execute them on the NetAuto Host to get things rolling:

NikoPalmLab2

Which you may see that big chunk of printing output (configs sent to the Switch) and think to yourself “Can’t there be a way to make that readable?” and the answer is Yes!! (at least for Netmiko / NAPALM output!)

We can do that with using JSON as the output encoding format for readability as I will do while working with NAPALM, as JSON actually uses its “key pair” formatting to give description : value output much like Python Dictionaries!

As always we want to verify verify verify, which I do Pinging PC1 to PC2 via SW1:

Pingyay

With that I will run the same script from the last lab to review that output:

NikoPalmLab3

As demonstrated NAPALM scripts are extremely simple yet powerful among multi-vendor environments, however the output is returned in a Python “Dictionary” format, which is where JSON comes to save the day (script).

This is why “pip3 install -U simplejson” was issued on the NetAuto Host last lab!

Lets take a look at what a couple of lines of JSON code added to the script can do!

Below is the NAPALM script with just one line added and another adjusted in the script:

NikoPalmLab4

This imports the JSON Python Library to allow me to adjust the print statement to format the output in JSON format, with an indentation of 4 like we use with Python Code.

Lets compare the output one next to the other here:

NikoPalmLab5

What a difference JSON formatting can make with just a couple of line differences!

Now to once again review this table before firing off a few more scripts with new values:

Napalm2

And I add this two lines from the full table (one cmd not shown here) :

NikoPalmLab7

I saw the “sort_keys=True” as a parameter that can be used that seems more like it is useful for Multi-Vendor Environments, or perhaps with different types of output it may format it slightly cleaner however with this Cisco Switch is appears the same to me.

When this script is issued on SW1 I get the following output in JSON formatting:

NikoPalmLab8

Now that I’ve beaten that dead horse back to death, these little changes in configs can be put into the code, extra lines added to retrieve bulk info, loops added to retrieve from multiple device, all of this output written to files as demonstrated in the “get backup” post I made within the Python3 posts, etc.

All that being said, I am going to get on with my long weekend, Happy 4th of July!

Next I will be adding some routers to run through some other NAPALM configs to this network, but wanted to demo quick how JSON can make that big blob of output returned by NAPALM much more readable for us mere mortals ๐Ÿ™‚

Until next time!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s