How Python Telnet Is Changing Computer Protocols

Hello Geeks! I hope all are doing great. So today, in this article, we will see Telnet Library in python, i.e., “telnetlib”. We will see how we can use it to establish connections between two computers and the operations provided by the interface we get. So, let’s get started.

Telnet

So before heading toward the telnet library, let’s take a quick overview of what is telnet? So, telnet stands for Terminal Network. It enables the connection between two computers within the same network. Computers that start the connection are known as the local computer, while the other one which connects is known as remote or host computer. Once the connection is established, we can perform various operations like reading and editing files, running various programs, or checking emails. Moreover, one can also see various activities performed on the host computers. Once we understand this, the question arises: how can we do it.

So in this article, we will use the python telentlib to show the demonstration. But before jumping to that, first, understand the various methods of the given library.

Telnet Library

So, the telnet module came with a class named “Telnet,” which implements the telnet protocol. We can pass the host, port number, and timeout parameter while instantiating the object of the class. It will start the connection before the constructor returns. Once the connection is established, we can use the following methods to perform various operations on the host computer. Let’s see an example through which we get the idea of how telnet works.

Example 1:

import getpass
import telnetlib

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

tn = telnetlib.Telnet(HOST)

tn.read_until("login: ")
tn.write(user + "\n")
if password:
    tn.read_until("Password: ")
    tn.write(password + "\n")

tn.write("ls\n")
tn.write("exit\n")

print tn.read_all()

Explanation

In the above example, we first imported the telnetlib library and then specified the host or the remote computer IP address we wanted to connect. Once we get the host, we can establish the connection by initializing the object of the telnet class. For that, we passed the host parameter in that class. If it is encrypted, we read the username of the computer and the password for that. Once the connection is built, we read from the host computer using the read_all() method. Similarly, we can also write to the host computer using the write() method. It also has other ways which support different functions. Let’s see them one by one.

Different Python Telnet Methods

  • read_until(expected, timeout=None): This method is used to read string bytes from the host computer until some string is not encountered while reading. The expected string(let say) is passed as the argument in the method. If we want some time duration for which we want to read we can also pass it as the argument.
  • read_all(): This method is used to read all the bytes of string until the connection is disabled.
  • read_some(): This method is used to read data bytes until EOF is hit.
  • open(hostport=0[, timeout]): This method is used to establish the connection to some host. We need to pass host as the argument to specify it. We can also pass the port number on which we want to establish the connection. There is also an optional argument timeout which specify how long we want the connection.
  • close(): This method is used to close the connection and free our resources.
  • msg(string): This methods is used to print the debug message if debug is greater that 0.
  • write(): This method is used to write on the host computer. This is blocked if the connection is blocked or raises OSError if the conection is closed.

Example 2: Telnet using with Keyword

We can also use context manager to set the connection between the server and the local computer. For that, we will use the “with” keyword.

from telnetlib import Telnet
with Telnet('localhost', 23) as tn:
      tn.read_until("login: ")
      tn.write(user + "\n")
      if password:
          tn.read_until("Password: ")
          tn.write(password + "\n")

      tn.write("ls\n")
      tn.write("exit\n")

      print tn.read_all()

How to Disable Python Telnet Echo in telnetlib

To disable the echo in python telnetlib, you should use the following line of code.

tn.write(telnetlib.IAC + telnetlib.DONT + telnetlib.ECHO)

However, turning off the echo is not the solution one might look for. So, we tell that we will echo, which turns off the echoing at the other end. To do that, we will use the following line of code.

tn.write(telnetlib.IAC + telnetlib.WILL + telnetlib.ECHO)

Python Telnet vs Socket

However, both are used to access different computers using the programming interface. But both are different than each other in several ways. While telnet uses application layer protocol in the OSI model, Socket is a transport layer protocol. This is the fundamental difference between both.

Moreover, we use telnet when we need to pass the control information through the communication channel. When a connection starts, it runs a series of will/won’t/do/don’t message, which returns line buffering, character echo, e.t.c.

Once the connection is established, it listens to control instructions before responding, and hence it works with TCP/socket connections that don’t use telnet.

FAQs on Python Telnet

How to export python telnet output to file?

You can use the following line of code.
HOST = "192.131.244.100"
user = "user"
password = "user"
tn = Telnet(HOST)
tn.read_until("Login: ")
tn.write(user + "\n")
tn.read_until("Password: ")
tn.write(password + "\n")
time.sleep(5) tn.write("lr\n")
OUTPUT = tn.read_until(":~>") #Change :~> to what the telnet displays when finished parsing FILE=open(C:\\temp\\Logs\\output.txt", "w")
FILE.write(OUTPUT)
FILE.close()
tn.close()

Which command is used to get telnet output?

We can use the following commands to get telnet output.
read()
read_until()

Q3) How to disconnect telnet?

We use telnet.close() method to disconnect telnet.

Conclusion

So, today in this article, we have learned about the Telnetlib library in python. We have seen how to establish the connection between a host computer and a local computer. Then we have seen different available methods in the Telnet class that extend different functionalities on the host computer. They help us with reading and writing operations on the host computer. Besides that, we also saw different methods with different operations within the same class.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments