Previously I have written a post about running telnet with Python. As everyone knows telnet is not the most secure protocol to manage devices. So I want to help you guys out with connecting to devices with SSH using python.
In this example I will be using fabric. Fabric is a python libary that helps you send SSH commands to devices. You can read more on their website about the libary: http://www.fabfile.org/
pip3 install fabric
Since I’m running python3 on my computer I run pip3, if you have installed python2.X you just need to have pip as the first word and not pip3 in the command.
from fabric import Connection
c = Connection(host = '10.78.23.12', user = 'username', connect_kwargs = {'password':'password'})
result = c.run('show version')
print(result)
The first line imports the module needed for connecting using SSH from the fabric libary. Then the connection to the host is performed with ip, username and password. The last two lines is a variable where the command is added to and a print command to display the information in the variable. The command in this script is just a simple show version to display the information on a cisco switch.
So here we go with my first test of python. After reading a few blogs on python and watched some videos I created my first script that actually does something on a switch. It’s not actually super useful but it’s something!
I got a Ubuntu machine that I connect to using SSH. On this computer I have used nano since it’s the only default editing tool I know how to use on a linux device (I really hate vi for text editing). When I get a little bit further along I’m going to set up my notepad++ client to automaticly upload my scripts since I like a little bit better to do text configs on my windows.
I’ll try to go trough the script almost line by line. I found the example in the python website and a youtube video.
import getpass
import sys
import telnetlib
The first part is importing moules to make the programming easier. In short terms it saves me alot of time making my own way of using the telnet protocol.
host = "10.10.10.30"
user = raw_input("Username: ")
password = getpass.getpass()
The second part is handeling the connection to the device. The first line is creating a variable called host. This is the IP or dns name for the device you are connecting to. Second line is creating the variable called user. The information does it get using an input when you run the command. You can see when to input the information when Username: is displayed. The last line in this section is creating the variable password. This uses the imported module password to not display the text when entered and hides it for us.
tn = telnetlib.Telnet(host)
This part is telling the python script to connect to the device with the IP address in the previous section. You can see the variable is with red text.
Now to the login part of the script. It first skips the MOTD or whatever is shown before the login prompt. The script is continiuing until it sees Username: When it reachs Username: it will enter the user variable (marked by red) that you enter in the previous section. This is ended by a \n to signal that the script should press enter. The script will then read until Password: shows up and ad the variable password ended with a \n. Pretty much the same as user
This part should be familiear to most cisco engineers. You can see the different commands in each line ended by \n to simulate the press of the enter key. It basicly sends out what you type in the command window.
print tn.read_all()
In the end it reads everything out that has been sent using the telnet session.
I have decided I want to try to program cisco switches and devices using python. At the moment my programming skills are limited to simple if and else. I now have a plan to configure a linux server with python on to do all my scripting.
The plan is to try to upload the different scripts I create here as I go along. Hopefully it will get useful for others too in the end and not just something for me. I will try to use various youtube videos and pages to learn me the diffferent things with oython and will link them from my upcoming posts as I go along.
If you have something you want me to write about or create a script it’s greatly apreciated!