Friday, September 25, 2015

Python - Simple Example Telnet to a Cisco Router

Python has been gaining ground in the networking world for its simplicity. Network engineers rely heavily on utilities that makes planning, provisioning and fact gathering easier. The more the routers and switches you deal with, the more you need a tool to help to accomplish your day to day tasks.

The next example uses telnetlib ( short for telnet library) to connect to other devices via telnet. The device could be a Cisco, Juniper, linux.. virtually anything that has telnet server enabled on it

You can also check how to SSH using paramiko module here

let's first see the whole code

import getpass
import sys
import telnetlib

HOST = "192.168.1.28"user = raw_input("Enter your remote account: ")
password = getpass.getpass()

telnet = telnetlib.Telnet(HOST)

telnet.read_until("sername: ",3)
telnet.write(user + "\n")
if password:
   telnet.read_until("assword:",3)
   telnet.write(password + "\n")

telnet.write("show ip int br\n")
telnet.write("exit\n")
print telnet.read_all()

and here's the output for it

>>> Enter your remote account: cisco
Warning (from warnings module):  File "C:\Python27\lib\getpass.py", line 92    return fallback_getpass(prompt, stream)GetPassWarning: Can not control echo on the terminal.Warning: Password input may be echoed.Password: cisco 
Router#show ip int br
Interface              IP-Address      OK? Method Status                Protocol
GigabitEthernet1       unassigned      YES unset  administratively down down    
GigabitEthernet2       192.168.1.29    YES DHCP   up                    up      
GigabitEthernet0       192.168.1.28    YES manual up                    up      
Router#exit

>>> 
so here's what happened,

telnetlib has a class in it called Telnet, which is responsible for all the telnet operations.

to ease things up, we're only interested in 4 lines of the code

telnet = telnetlib.Telnet(HOST) abbreviating the telnetlib.Telnet(host) to a single word making it easier to work with

telnet.read_until("something",3) keeps reading the buffer until it reaches the text in green. the integer in blue states the timeout value in seconds

telnet.write("something \n") this writes to the buffer that is sent to the remote device the text in green, always remember to type \n as an enter, otherwise the script will be stuck in that command indefinitly

print telnet.read_all() the difference between this and read_until is that this one keeps reading until the buffer empty, the print command before is to make it appear on your screen
here's the script lines one by one.

  1. We imported three modules which are sys , getpass and telnetlib. the later is the one we're interested in. the telnet module abstracts opening a socket and establishing the telnet session between the PC and the router
  2. The script asks the user to manually input a string to assign to the variable named username.
  3. We assign a variable named telnet to handle telnetlib.Telnet(HOST).
  4. HOST is a string variable containing the IP address of the host router we want to telnet to
  5. We instruct the script to keep reading the output of the telnet session until it reaches "sername" which is username without the leading "U" letter. the reason I do this is because sometimes the letter is capital and sometimes it is small. with that we can just ignore the first letter.
  6. We try to input the password using the getpass module. the reason we use it is that it can control the vty line, no password will show up when you're putting the password but in our case it did.
  7. we then instruct the telnetlib to write to the line the command we want to pass to the router. the \n at the end is the equivilant of pressing the enter key or "new line"
  8. we then instruct the script to read all the text in the receive buffer.

At this moment you'd say, what am I supposed to do with this?! secure-CRT can do this and even more!

well, the point is you managed to get the script to communicate with your router. What you can do next is a lot lot more than you can imagine. in the next blog-post, i'll show you how you can connect to multiple routers and get some info from it, maybe the version of the software, modules, interfaces.. etc..

Feel free adding me to your RSS reader or subscribing directly to blogger as I will be doing a whole series about python. also sharing  this blog for people interested in networking and scripting would be a nice thing to do :)