Telnet and Configure Network with Python Script

Muhammad Haris Maqsood
5 min readFeb 28, 2023

--

Telnetlib:

The telnetlib module provides a Telnet class that implements the Telnet protocol. See RFC 854 for details about the protocol. In addition, it provides symbolic constants for the protocol characters (see below), and for the telnet options. The symbolic names of the telnet options follow the definitions in arpa/telnet.h, with the leading TELOPT_ removed. For symbolic names of options which are traditionally not included in arpa/telnet.h, see the module source itself.

The symbolic constants for the telnet commands are: IAC, DONT, DO, WONT, WILL, SE (Subnegotiation End), NOP (No Operation), DM (Data Mark), BRK (Break), IP (Interrupt process), AO (Abort output), AYT (Are You There), EC (Erase Character), EL (Erase Line), GA (Go Ahead), SB (Subnegotiation Begin).

class telnetlib.Telnet(host=None, port=0[, timeout])

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).

Do not reopen an already connected instance.

This class has many read_*() methods. Note that some of them raise EOFError when the end of the connection is read, because they can return an empty string for other reasons. See the individual descriptions below.

A Telnet object is a context manager and can be used in a with statement. When the with block ends, theclose() method is called:

Telnet Objects

Telnet instances have the following methods:

Telnet.read_until(expected, timeout=None)

Read until a given byte string, expected, is encountered or until timeout seconds have passed.

When no match is found, return whatever is available instead, possibly empty bytes. Raise EOFError if the connection is closed and no cooked data is available.

Telnet.read_all()

Read all data until EOF as bytes; block until connection closed.

Telnet.read_some()

Read at least one byte of cooked data unless EOF is hit. Return b’’ if EOF is hit. Block if no data is immediately available.

Telnet.open(host, port=0[, timeout])

Connect to a host. The optional second argument is the port number, which defaults to the standard Telnet port (23). 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).

Do not try to reopen an already connected instance.

Telnet.close()

Close the connection.

Telnet.write(buffer)

Write a byte string to the socket, doubling any IAC characters. This can block if the connection is blocked. May raise OSError if the connection is closed.

Telnet Example

A simple example illustrating typical use with step by step explanation.

""" Import libraries in order to run the telnet script. 
First we import telnetlib library which is used to telnet the device.
Secondly we import the library getpass and used its getpass method to
get the password. """

import getpass
import telnetlib

""" In the next step we will set the host whether it will be local or
any other IP but it should be reachable in order to work it correctly. """

HOST = "localhost"

""" In the next step we will input the user to enter the remote
account name or username or login depending on the model and device
and stored it in a variable name user in this example.
And also we will input the password from user and stored in a
variable named password in this example. """

user = input("Enter your remote account: ")
password = getpass.getpass()

""" In next step we are using telnet class and pass HOST as an
argument to it and it will open a telnet connection or socket
with the target device. """

tn = telnetlib.Telnet(HOST)

"""In next step, we will read until the occurrence of that word which
is mentioned in the function argument. In this case this word is "login".
And this word, we will write the word that user entered above in the code """

tn.read_until(b"login: ")
tn.write(user.encode('ascii') + b"\n")

"""If there is any password entered by user then it will read until the
occurrence of word password and then after that it will write the password
what user entered. """

if password:
tn.read_until(b"Password: ")
tn.write(password.encode('ascii') + b"\n")
tn.write(b"ls\n")
print(tn.read_all().decode('ascii'))

Topology:

In this topology all network devices are part of same LAN. All are reachable i.e. all can ping each other.

As shown that Ubuntu interface has been configured with an IP of 192.168.74.135 with subnet mask of 255.255.255.0

As it can be seen that router is reachable from Ubuntu device as both are part of the same LAN.

R1:

As seen in the output, R1 has been just configured with an IP address of 192.168.74.130 with subnet mask of 255.255.255.0 and it is reachable to and from Ubuntu device as both are part of same network.

The script for telnet is given below but with additional configuration. We have configured loopback 0 and assign IP to it.

Debugging:
R1:
As you can see that we have enable debugging for telnet and it is shown in the figure that script has been pushed and loopback has been configured and it can be verified by checking router configurations.

--

--