Python for network engineers - telnetlib

The typical job of the network engineer is to manage the networks. To manage the network in traditional way, the network engineer connects to the CLI of the device and enters the series of commands. The most simple way to do the job is by using the telnet protocol. Python already includes telnet client in the standard library. The standard telnet library in python is called telnetlib. The goal of this post is to show, how to write a simple script, that connects to the router, executes some command and exits.

The telnetlib consists of python class Telnet. You have two options to initialise the Telnet instance and open the connection to the telnet client.

The first option is to create a instance of the Telnet and then use open() method to establish the connection.

conn = telnetlib.Telnet()'')

The other option is to establish connection during class initialisation.

conn = telnetlib.Telnet('')

To read the data that is received from the connection, you can use read_until() method. You need to provide the expected string to the method. In this phase, we will assume that router is configured with AAA and "Username: " string is returned from the router. It is a good practice to include some timeout to the method, to prevent the blocking in the code. So our code will be:

conn.read_until('Username: ', 3)

The method will read until the given string is matched or until the timeout is reached. If there is no match the method will return whatever was received.

Now we are ready to send some data via telnet connection. You can use the write() method. The write() method expects the string that should be send to the telnet server. So in our example, we need to send username together with new line symbol (\n).


After you are finished it is advisable to close the connection using close() method.


So lets write a simple program that connects to the router, sends the username and password, close the connection.

import telnetlib

# Establish the connection
conn = telnetlib.Telnet('')

# Read the output for the username request
output = conn.read_until('Username: ', 3)

# If 'Username: ' is found, send the username
if 'Username: ' in output:

# Read the output for the password request
output = conn.read_until('Password: ', 3)

# If 'Password: ' is found, send the password
if 'Password: ' in output:

# Read for hash prompt
output = conn.read_until('#', 3)

# Check for # in output
if '#' in output:
    conn.write('show ip interface brief\n')

# Read the output
output = conn.read_until('#', 3)

# Print the output
print output

# Close the connection


  1. Very interesting blog. Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definately interested in this one. Just thought that I would post and let you know. our home page

  2. i am for the first time here. I found this board and I in finding It truly helpful & it helped me out a lot. I hope to present something back and help others such as you helped me. TutuApp Android

  3. Fantastic blog! Do you have any tips and hints for aspiring writers? I’m planning to start my own website soon but I’m a little lost on everything. Would you propose starting with a free platform like WordPress or go for a paid option? There are so many options out there that I’m completely overwhelmed .. Any suggestions? Many thanks! AppEven

  4. I have been checking out a few of your stories and i can state pretty good stuff. I will definitely bookmark your blog appvn

  5. So in my aunt’s technology, lots of|there are many} households with multiple of} daughters, after which lastly a son. This name means “excellence and cleverness.” In the old times, only the intelligent women might survive within the palace, and thus people started naming their women Soo Min. Another stunning name is Bong-Cha, a Korean word meaning “superior” or “ultimate lady.” This name is usually given to a very delicate and feminine AV쏘걸 lady. There are differentKorean beliefsabout Korean names, making it obligatory to have a lovely and vital name for your child. Rainbow launched the teaser for his or her single "Sweet Dream" on June 20, 2011.