I started my Python and programming journey by looking at the NetMiko python library. Looking at the code examples and uses that I have seen so far, it seemed to be the most flexible to work with the types of devices I wanted to control.
First steps first, you can find the library here: https://github.com/ktbyers/netmiko
Basic Use Example
From looking through the NetMiko github, there are code examples and a very good example of a basic use case. Let’s start taking a look at that first:
#!/usr/bin/env python from netmiko import Netmiko from getpass import getpass net_connect = Netmiko( "cisco1.twb-tech.com", username="pyclass", password=getpass(), device_type="cisco_ios", ) print(net_connect.find_prompt()) net_connect.disconnect() |
Walking through this script line by line makes a person realize how simply logical this is. Getting into this mindset and beginning to think in this logical manner is a great skill. From top to bottom, this script basically does this:
- Setup Script
- Import libraries
- Create the NetMiko connection object with needed values
- Execute the connection with the object that was created and do this within the print function. Then return the output to the user.
- Disconnect the session
There is one thing I want to focus on that is a large part of NetMiko. Take a look at this line:
print(net_connect.find_prompt()) |
We already know what the connection object that “net_connect” is from the example before, but notice how “find_prompt()” is attached to it? This is a specific method that is available to the “net_connect” object. In this case, it pulls the current prompt from the network device you are working with as shown below in the case of my home environment:
Only thing left at this point is to branch out and see what other methods are available and begin to see what is possible!