Thursday, December 4, 2014

Automating Telnet to Cisco/Juniper/Huawei Routers Using Python

When choosing a method for router automation, netconf, ssh, snmp etc are more reliable and stable than telnet but still you may it. I needed a telnet script/library which can execute multiple commands on a router and assign every output to a different variable so that i could. parse related areas.

Telnetlib is a native library in python and it can be used in the same way on all operating systems that support python. You should check the details (like how it is used basically) of the library from

In my opinion, a good telnet script for router automatization must support:

  • executing multiple commands in order
  • executing long commands (longer than terminal width)
  • assigning every result to a different variable or print it.


You can download the script files from here if you don't want to read the rest of the post.

We will use class logic in this script. Classes are very useful if you repeat the same tasks again and again. With a class it is possible to create many objects. For example  every telnet session to a different router could be an object of the same class, and every object could have different values. If you don't know much about classes in python, i would suggest you to take a lookout at page Jeff Knupp's page:


Login and Password Phrases

When telnetting to a router, different vendors has different login and password phrases. It is possible to wait for these phrases and then send the username or password. The variables  that we will use in the script  to identify these phrases  are:

login_phrase = ["sername:", "ogin:"]
password_phrase = ["assword:"]

You might realize that the first letters are missing, this is done to avoid case sensitivity.

Terminal Length

When you execute a command, a  router will return a limited output cause it has a preconfigured terminal length and if you wanted to see the continuning parts of the result you need to press "enter" key. When using a script we want the router to return an infinite output. To do so every vendor has a different command which we can assign to different variables:

ios_cli_length = " terminal length 0"
junos_cli_length = " set cli screen-length 0"
iosxr_cli_length = " terminal length 0"
vrp_cli_length = " screen-length 0 temporary"

Various Variables

line_break = "\r\n" #need to be send after a command to get it executed like sending "enter" key stroke
timeout_for_reply = 1 #1 second time variable
exclude_start = ("#", "$")


Init Definition

We need a init definition as we are using a class, so that every object that is created using this class could be identified.

class TELNET(object):
 """connect to hosts"""
 def __init__(self):
  self.connections = []
  self.device_names = []
  self.result_dictionary = {}

You may notice that inside the _init_ definition there is two list and one dictionary variable. We have created these empty  lists and dictionary to pass values between definitions inside the class. You will have a better understanding reading through the other definitions.

Connect Definition

This definition is the longest one in TELNET class. Inside the definition, you may notice that there is a router variable and it is a list. Telnetlib needs router hostname and port number for the connection to get established. And you will also need to enter username and password after getting connected, if it is needed. In "router" the positions of the elements must be like below. Based on your needs, you may enter this variable as a static element in the script file itself or you may make the script read it from a txt/xml file.

router = [ip,software_type,connection_type,port_number,username,password,first_command]
router = [,ios,telnet,23,admin,password,show ip bgp]

 def connect (self, router):
   connection = telnetlib.Telnet(router[0], router[3])
  except IOError:
   print "IOError, could not open a connection to %s" % router[0]
  """send username"""
   if router[4] != "":
    connection.write(router[4] + line_break)
  except IOError:
   #Send failed
   print "sending username %s failed" % router[4]
  """send password"""
   if router[5] != "":
    connection.write(router[5] + line_break)
  except IOError:
   #Send failed
   print "sending username %s failed" % router[5]

The upper part of the "connect" definition is for getting connected to the router and sending  username and password. The lower part, which you can see below, is for setting the terminal length based on router type and getting the device_name as a variable.

Why we are trying to take the device name as a variable ? Whenever the connection type is telnet, after executing a command you must be sure that you get the whole output. So if there is delay how long should you wait for the output, or how many bytes you should get ? The best way that i could find is to wait for the router_name to be send through the connection. This way even the execution of the command takes long, or there is a delay in network you can be sure that you get the whole output.

  """set terminal length and take device name"""
  try :
   if router[1] == "ios-xr":
    connection.write(iosxr_cli_length + line_break)
    device_name = connection.read_until(iosxr_cli_length).split()[-len(iosxr_cli_length.split(' '))]
   elif router[1] == "junos":
    connection.write(junos_cli_length + line_break)
    device_name = connection.read_until(">").split()[-1]
   elif router[1] == "vrp":
    connection.write(vrp_cli_length + line_break)
    device_name = connection.read_until(vrp_cli_length).split()[-len(vrp_cli_length.split(' '))]
   elif router[1] == "ios":
    connection.write(ios_cli_length + line_break)
    device_name = connection.read_until(ios_cli_length).split()[-len(ios_cli_length.split(' '))]
    print router[1] + " is not an appropriate connection type"
  except IOError:
   #Send failed
   print "setting terminal length failed"

If you examine inside the if confidition for  every router type we wait "timeout_for_reply" second, then send the terminal length command (line break is for "enter" key stroke). And for last we catch the device name by splitting the received data and getting the last value. You may add a "print device_name" statement to see the device name.

At the end of the "connect definition" we created a connections and device_names dictionaries, so that we can pass the active connection to the next definitions

Execute Definition

At this definition the aim is to execute commands, and if needed multiple commands in order. As we get the connection info as a variable from "connect" definition, the definiton is:

 def execute (self, router):
  for conn in self.connections:
   for device in self.device_names:
    conn.write(line_break) #if executing more than one, line break will push device name again and next read_until wont get stuck
    catch_end_of_output = [device+" "+line_break, device+line_break]
    self.result_dictionary[router[0]] = conn.expect(catch_end_of_output)[-1]

You may notice that catch_end_of_output is a dictionary and it contains device_name+ line_break elements with and without an empty space character. If you are execu_ing a long command, which is longer than the terminal width, the first part of the command  will be resent back to you from the router including the host name.(you may check this pcap file).It is not enough to wait only for the device name to get the output, as you will get it before executing the command. To solve this problem we can send another line_break after command execution (that is why you see 2 line+breaks for conn.write) and catch device_name+line_break in output.
Why our list has 2 elements with and without space character. This is because some routers return device name after line break with a space character while others send it without a space in order which i realised in various capture files. Close Definition As we get connected to the router with "connect" definition, we also need to close that session. For that purpose the definition is:

 def close(self):
  for conn in self.connections:

Reading Router Info From a Text File

After close definition, the TELNET class is ready to be used. Now we need the script to read connection information from a txt file.

## open routers.txt, clear comments, get all data as "lines" variable
f = open('routers.txt', "r")
lines =  [n for n in f.readlines() if not n.startswith(exclude_start)] #read the lines that does not start with the characters defined in exclude_estart variable
total_connections = len(lines) #determine number of routers/connections by counting the lines

#split "lines" variable and turn every line into new variables named as routerx which contains connection infos
for x in range(0, total_connections):
    globals()['router%s' % x] = (lines[x]).split(',')

You may also try getting the data from an xml file. Getting data from an xml file is easier than getting it from a text  cause you wont' need parsing the text file. You may have a look at xml.etree.elementtree python library from

Connecting, Executing and Printing

It is time to connect and execute the commands as all infos and class are ready. We will create a loop and for every router create an instance of the class (object) so that we session is created and command is executed.

##connect, execute command and print 
##connect, execute command and print  
for x in range(0, total_connections):
 telnet = TELNET()
 result= telnet.result_dictionary[(globals()["router"+str(x)][0])].split("\r\n")
 for line in result:
  print line


As everything is ready lets try adding two globally available looking glass and try getting BGP info for The routers.txt file must be in the same folder with the script. Here is the text info:

#Comments here
##,ios,telnet,23,,,show ip bgp,junos,telnet,23,rviews,rviews,show route protocol bgp

Here is the result when we call the script in Windows PowerShell


konjuge said...

great job :)

Alaa said...

Thank you very much, this worked perfectly.

I wasn't able to run multiple commands for same router, can you please explain how to do this?

Unknown said...

thank you bro. It's very useful! Nice job

nagibdaffron said...

What's New At the Hard Rock Hotel & Casino - Dr.MCD
At Hard Rock Hotel & Casino in Las Vegas, you'll 이천 출장샵 be met with, 강원도 출장샵 excited and excited to 광양 출장마사지 welcome back 김천 출장마사지 the rock stars who have 통영 출장샵 been rock stars since

Post a Comment


Internetworking Hints Copyright © 2011 -- Template created by O Pregador -- Powered by Blogger