Skip navigation
All People > chapeter > Chad Peterson's Blog > 2015 > June > 26

Chad Peterson's Blog

June 26, 2015 Previous day Next day

Being able to programmatically interact with network devices and allow you to build very powerful tools, save time by automating repetitive tasks, and help prevent configuration mistakes.


Interacting with network devices is becoming much easier with RESTful API’s vs using Expect or something similar for interactive programming.


You don’t have to be a have to be a expert to create useful tools, a little bit of scripting knowledge can go a long way.  Most of what I write is in Python and its pretty easy to get started.  There are a lot of free resources on the internet that are very good if you are just getting started.



This is not a getting started with Python article, but more of a getting started with NXAPI + Python. I’m going to assume some Python familiarity.  I’ve tried to write the code so it is easy to discuss, and should be very easy to look at to understand what is being done.

To get started I recommend using “show” commands.  We don’t need to worry about as much error handling as we would if we were configuring the device.  We will mainly need to be concerned about how to extract the data we are interested in.


We also need to set a goal. To make this useful, think of something you log into your devices often to do. In this example we’ll be looking at up time and the version of code for a switch.

Goal:  Build 1 line tool to report up time and the version of code running on a switch

The traditional way to get this accomplished is to log into a switch and issue the “show version” command.  That command will generate the data we are interested in.


Getting started NXAPI sandbox:

We can use the NXAPI sandbox to get started very easily.  The sandbox will allow you to enter the commands you want on the CLI, and it will give you the XML/JSON that you need to send to the API.  It will even go a step further and write some basic Python for you.


It will also show you the result of the command, so you can see the data structure you will get back, and you can parse it correctly.


Laying down some code:

Before we get started we need to make sure we collect some information from the user.  IP address, username, and password should be enough for now.  Because I want this to be a 1 line command, we need to pass them into the script as arguments from the cli.


However we do want to have the option for the user to type in a password and have it NOT displayed…so we’ll use getpass for that.







To pass in arguments we are going to use argparse.  As we define our arguments (ip, user, password) we specify the flag to be used, how we refer to it internally, help information, and if its required.


Once we define all that, we will store all these into a variable called args.  We can get this info back by using args.ipaddress, args.user, args.password

Next we need to see if the password was sent.  If it wasn’t we will request it from the users and NOT display it back.  For this we will use getpass.getpass().  To keep things consistent we will keep it assigned to args.password




Just to show what we built, here is what the help for our script looks like:


CHAPETER-M-V072:blog chapeter$ python -h

usage: [-h] -ip IPADDRESS -user USER [-pass PASSWORD]

optional arguments:

-h, --help show this help message and exit

-ip IPADDRESS, --ipaddress IPADDRESS

IP address of switch

-user USER, --user USER


-pass PASSWORD, --password PASSWORD








Now that we can pass in the variables we need to log in and authenticate with the switch, we need to look at how we Post the JSON.


To explore how to begin using the switch’s API and the data we get back, we’ll use the NX-API Sandbox:





We know that the info we need would be generated when we type “show version”.  All we need to do is type in the commands in the sandbox and hit post.  It will show you the request format and the response:




To make things even easier, the sandbox can provide some base level python code that will push the generated JSON:








Now that we have some basic Python and the formatting of the JSON request we need to interact with the switch, lets add it to our script and modify the IP address and credentials.









The last variable we see here is “response".  This is the entire data structure of the response that we got back and saw in the API sandbox:




If we look at the data we receive back, we can see the fields we are interested in: “host_name”, “kickstart_ver_str”, “kern_uptm_days”, “kern_uptm_hrs”, “kern_uptm_mins”, “kern_uptm_secs”


These fields are buried a bit down.  Right now to reference the host_name field we’d need to do this:

hostname = response['ins_api']['outputs']['output']['body']['host_name']


To make this easy, lets create a new variable that only contains inside “body” and just refer to it:

response_body = response['ins_api']['outputs']['output']['body']

hostname = response_body[‘host_name’]

days = response_body['kern_uptm_days']

hrs = response_body['kern_uptm_hrs']

mins = response_body['kern_uptm_mins']

secs = response_body['kern_uptm_secs']

version = response_body['kickstart_ver_str']



The output:

Now that we have the data we are interest in, we need to display it back to the user on the CLI.


I want it to look like this:

“SWITCH is running VERSION has been up for X days, X hours, X minutes, X secs"


print('%s is running %s and has been up for %s days, %s hours, %s mins, %s secs') % (hostname, version, days, hrs, mins, secs)




Final Result:




When we run it:

python -ip -user admin -pass cisco

spine1 is running 7.0(3)I1(1) and has been up for 37 days, 22 hours, 55 mins, 34 secs


As you can see we can create a small program in under 50 lines of code (and most of it was automatically created for us).  While it may seem like quite a bit of effort to just get a little bit of information, so much of this could be re-used.  We could easily change this to pass in other commands by the CLI.  Of course we’d need to figure out how to parse out that data to make use of it.



I hope this is useful.  The NX-API Sandbox is a great tool to help make some quick hit tools.  You can download this example at .  I also have other tools I've built for NXAP at



Filter Blog

By date: By tag: