Python telnetlib Library Introduction

Python telnetlib Library Introduction


Introduction :

The telnetlib module provides a Telnet class that implements the Telnet protocol.

What is Telnet? :

Telnet is a type of network protocol which allows a user in one computer to logon to another computer which 
also belongs to the same network. The telnet command is used along with the host name and then the user credentials are entered. Upon successful login the remote user can access the applications and data in a 
way similar to the regular user of the system. Of course some privileges can be controlled by the 
administrator of the system who sets up and maintains the system.

Python telnetlib :

In Python telnet is implemented by the module telnetlib which has the Telnet class which has the required methods to establish the connection.
The telnetlib library is already included in the python package. We need not install it rather just import it
in our program as given below.


Python Telnet Class :


Telnet represents a connection to a Telnet server. The instance is initially not connected by default; the open() method must be used to establish a connection. Alternatively, the host name and optional 
port number can be passed to the constructor too, in which case the connection to the server will be 
established before the constructor returns. The optional timeout parameter specifies a timeout in 
seconds for blocking operations like the connection attempt (if not specified, the global default timeout 
setting will be used).
Telnet Object is a context manager and can be used in a with statement. When the with block ends, the close() method is called:

The with command will automatically close the connection to the server. 
Interact() function, emulates a very dumb Telnet client.

Some of the methods of the telnetlib.Telnet class are explained below.
  • Telnet.read_until - Read until a given string, expected, is encountered or until timeout seconds have passed.
  • Telnet.write - Write a string to the socket, doubling any IAC (Interpret as command) characters. This can block if the connection is blocked. May raise socket error if the connection is closed.
  • Telnet.read_all() - Read all data until EOF (End Of File); block until connection closed.

Telnet Example :


When we run the above program, we get the following output :

Please note that this output is specific to the remote computer whose details are submitted when the program is run.

Conclusion :

In this way we use the Telnet library of Python to perform different operations. We will see about more methods and their uses in the upcoming articles.




  • swara
    26-Sep-2020 04:08:34 PM
    Good information

  • Yash
    25-Sep-2020 11:26:22 PM
    //Enter Your Comment Here...Well explained 
  • Sushma
    19-Sep-2020 11:57:02 AM
    Loved it
  • Rishabh
    9-Sep-2020 10:32:22 PM
    Well written and informative %uD83D%uDCD3
    Keep it up%uD83D%uDC4D
  • Ayush
    9-Sep-2020 10:29:03 PM
    Ankur bhaiya OP%uD83D%uDD25
  • Lakshay
    9-Sep-2020 10:15:18 PM
    Amazing and very helpful content 

  • Shubham
    9-Sep-2020 10:11:24 PM
    Amazing %uD83E%uDD29