As a network engineer by trade, I'm familiar with networking protocols and their operations as well as managing configurations for devices such as routers, switches, firewalls and others. Lately with the amount of devices that require config changes or even just a basic bootstrapping to get them off the ground for a deployment I've begun using Python3 more and more for this process. With the focus of automation and Software Defined....everything lately I wanted to begin a sort of blogging journey to chronicle the things I'm learning each day and hopefully along the way help out someone else trying to learn.
We are going to design this simple script to get a command output from a switch in our example and in my next post we can build upon this and run configuration commands to create a new VLAN and tag it to a port. I'm using an Ubuntu 16.04 server to execute the script and an HP Procurve 2530 switch I have to test this out on. Keep in mind the Netmiko library for Python supports a pretty wide list of devices including the following at the current time of this writing:
I am using Python 3.6 in this example but I'm going to try to make this script compatible with Python 2 releases as well in case you are using a system running Python version 2.x. If you want to find out what your system's Python version is using as a default you can run the following command in a terminal window: python --version
Here is the start of the script we will be making, I will go over each line to describe what is going on at each step:
#!/usr/bin/env python
from __future__ import print_function
import netmiko
from getpass import getpass
NOTE You may have to install the netmiko library in your Python environment, you can use something like pip to install this by running in your terminal:
sudo pip install netmiko
So we imported the libraries we need and now it's time to get started working with returning command output from Netmiko. To start we need to build a connection object using Netmiko that will be the interactive ssh shell we use later. We can do this using:
connection = netmiko.ConnectHandler(ip='172.31.1.251',
device_type='hp_procurve',
username='admin',
password=getpass('Enter a password for the switch:\n'))
print(connection)
connection.disconnect()
You can replace the device_type= section with any of the supported types I listed above and of course the username and IP in the example are just filler so put your info in there. The print statement will simply return some info regarding the socket we established and finally we want to disconnect the socket as this is just a simple test to validate functionality. Python's error messages if you encounter any are usually pretty human readable and to the point but common errors would be Host timeout or Authentication failure messages.
If everything goes as expected when you run the script you will get a prompt to enter your password which is what the password=getpass('Enter a password for the switch:\n') section does. Keep in mind you won't see what you are typing. If things go well after you hit enter you will see something like the following:
Enter a password for the switch:
<netmiko.hp.hp_procurve_ssh.HPProcurveSSH object at 0x0000025C7B41B240>
#!/usr/bin/env python
from __future__ import print_function
import netmiko
from getpass import getpass
connection = netmiko.ConnectHandler(ip='172.31.1.251',
device_type='hp_procurve',
username='admin',
password=getpass('Enter a password for the switch:\n'))
print(connection)
result = connection.find_prompt()
result += connection.send_command('show version')
print(result)
connection.disconnect()
Then once we run this we should get output similar to the below, of course it will vary depending on your switch model, etc..
So we've successfully ran a simple show command against our switch, feel free to play around with different commands to get output. In later posts I will get into running commands against multiple devices as well as catching exceptions that may occur while attempting to hit multiple hosts in a script but for now we are on the right foot to get something good started.
Thanks for reading and I hope you found this helpful or interesting. Please feel free to comment with ideas you'd like to see, and don't hesitate to follow or upvote!