PYTHON TELNET LIBRARY (TELNETLIB).
Introduction :
What is Telnet? :
Telnet is a type of network protocol which allows a user in one computer to logon to another computer whichalso 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 away similar to the regular user of the system. Of course some privileges can be controlled by theadministrator 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 itin 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 optionalport number can be passed to the constructor too, in which case the connection to the server will beestablished before the constructor returns. The optional timeout parameter specifies a timeout inseconds for blocking operations like the connection attempt (if not specified, the global default timeoutsetting will be used).A 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.
Comments
swara
26-Sep-2020 04:08:34 PMYash
25-Sep-2020 11:26:22 PMSushma
19-Sep-2020 11:57:02 AMRishabh
9-Sep-2020 10:32:22 PMAyush
9-Sep-2020 10:29:03 PMLakshay
9-Sep-2020 10:15:18 PMShubham
9-Sep-2020 10:11:24 PM