Using python and telnet

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(<span style="color:#ff0000;">host</span>)

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.

tn.read_until("Username: ")
tn.write(<span style="color:#ff0000;">user</span> + "\n")
if password:
tn.read_until("Password: ")
tn.write(<span style="color:#ff0000;">password</span> + "\n")

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

tn.write("enable\n")
tn.write("Cisco\n")
tn.write("conf t\n")
tn.write("vlan 20\n")
tn.write("name guest\n")
tn.write("vlan 100\n")
tn.write("name production\n")
tn.write("end\n")
tn.write("exit\n")

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.

The complete script will then be this:

import getpass
import sys
import telnetlib

host = "10.10.10.30"
user = raw_input("Username: ")
password = getpass.getpass()

tn = telnetlib.Telnet(host)

tn.read_until("Username: ")
tn.write(user + "\n")
if password:
tn.read_until("Password: ")
tn.write(password + "\n")

tn.write("enable\n")
tn.write("Cisco\n")
tn.write("conf t\n")
tn.write("vlan 20\n")
tn.write("name guest\n")
tn.write("vlan 100\n")
tn.write("name production\n")
tn.write("end\n")
tn.write("exit\n")

print tn.read_all()

I have also attached a screenshot from the Linux server when I’m running the script
telnetcreatevlan

Advertisements

Cisco and python programming

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!