Using Python to Connect to Cisco Devices

Using Python to Connect to Cisco Device:

Step 1

When getting started on your network automation journey, one of your best friends is going to be “Netmiko”. I am assuming you have Python installed and you can successfully install Netmiko.

pip install netmiko

If you have issues you need to get your Google on and make sure that’s all resolved before we can move on.

Step 2

Do you have a lab device or test device, ideally IOS or NX-OS to SSH to for testing your scripts? You should. If not, might be a good time to fire up GNS3 and setup a test environment. A couple of IOS or NX-OS virtual devices and maybe a Linux host that you can run your python scripts from.

Either way, let’s document your hostnames and IPs of the devices you wish to test against as well as your username/password needed. Make sure you can SSH in that there are no basic issues to overcome before getting started. It can throw off your scripts and cause frustration in your learning process.

Example:
Switch1 192.168.1.1 username password
Switch2 192.168.1.2 username password

Step 3

Let’s create a new .py file. Using the IDE (Recommend PyCharm or Visual Studio Code ) of your choice lets create ‘showclock.py’
First, we have to define a couple of things we want to use in our script, such as Netmiko.
This is done by adding:

from __future__ import print_function
import time
import netmiko

Step 4

Now let’s define our devices we want to connect to. This part can be done in so many ways.
You can reference a separate file that contains your host details. Which we will cover in another post.
You can put the devices in a list or a dict. Or you can do as I am about to show you and each device is in a dict that I then put in a list! Yes, I know, this is still a pain I battle with.

I am choosing this method to show you because I find it’s helpful to see as it’s easy to expand on if you needed to say define lists of devices based on different locations or data centers. I’ll explain later but let me show you the basics.

Example:

switch1 = {'hostname': 'switch1', 'ip': '192.168.1.1'}
switch2 = {'hostname': 'switch2', 'ip': '192.168.1.2'}

devices = [switch1, switch2]

I created a dictionary for each device that contains the hostname and IP. I then put those into a list called “devices”. This will make more sense in a later example when we are working with devices in multiple data centers / locations.

Step 5

Let’s save the output of what we send and receive from each device to a single file. I’ll do this the simplest way I know, however, I also know this is another point of contention with many who have more experience. They will tell you to use some logging feature but for now, lets do things this way.

 

I am creating a file that will include the month, day and year in the file name. Should look something like “datetime-audit-05122017.txt”.

You can read more about the “time.strftime” usage here:https://docs.python.org/2/library/time.html

Here are some examples of what things mean:

%a Locale’s abbreviated weekday name.
%A Locale’s full weekday name.
%b Locale’s abbreviated month name.
%B Locale’s full month name.
%c Locale’s appropriate date and time representation.
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%I Hour (12-hour clock) as a decimal number [01,12].
%j Day of the year as a decimal number [001,366].
%m Month as a decimal number [01,12].
%M Minute as a decimal number [00,59].

We basically open the file with a specific filename and we define it as “logmyscript”. Then you can see where we add to the file with “logmyscript.write”.

Step 6

Now we are going to tell Netmiko what we want to do for each of our devices. We are going to loop through each device in our list “devices” that we defined earlier.

We also add a couple of exceptions to handle without causing the script to stop. Without adding exceptions when you encounter an issue connecting to a device it would cause the script to stop. So if you are checking 100 devices and say you go through 48 devices without any issues but #49 is unreachable then the script would fail. You wouldn’t be able to proceed with the rest of the devices until you resolve the issue with #49.

 

Final Step!

Let’s add a few extra print and logging lines to just help show the end of each pass through the loop. I personally just like adding some extra like this to help me see what’s happening and where I may encounter issues along the way. It is not necessary and if you were automating this to run on its own in say a crontab you would not add any of the “print” lines in the script as that only displays while you are running the script. So its really only helpful when you are manually running it and watching it in real time.

The “logmyscript” lines will add things to the file we created. This can be useful even when it’s a scheduled job. You will be able to refer to the log and see what happened.

Here is some extra lines to play with:

 

Leave a Reply

Your email address will not be published.