Sep. 23, 2012

Using Telnet in Python

Using Telnet in Python

To make use of Telnet in Python, we can use the telnetlib module. 

That module provides a Telnet class that implements the Telnet protocol.

The Telnet module have several methods, in this example I will make use of these:
read_until, read_all() and write()

Telnet Script in Python

Let's make a telnet script
import getpass
import sys
import telnetlib

HOST = "hostname"

user = raw_input("Enter your remote account: ")

password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

tn.read_until("login: ")

tn.write(user + "

if password:
    tn.read_until("Password: ")
    tn.write(password + "



print tn.read_all()
At ActiveState you can find more Python scripts using the telnetlib, 
for example this script.

For more information about using the Telnet client in Python, please see the 
official documentation. 

Share this article

Recommended Python Training – DataCamp

For Python training, our top recommendation is DataCamp.

Datacamp provides online interactive courses that combine interactive coding challenges with videos from top instructors in the field.

Datacamp has beginner to advanced Python training that programmers of all levels benefit from.


Download Our Free Guide To Learning Python

* indicates required

Read more about:
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, only recommend products or services that we try personally and believe will add value to our readers.