The telnetlib() module

In this section, we are going to learn about the Telnet protocol and then we will do Telnet operations using the telnetlib module over a remote server.

Telnet is a network protocol that allows a user to communicate with remote servers. It is mostly used by network administrators to remotely access and manage devices. To access the device, run the Telnet command with the IP address or hostname of a remote server in your Terminal.

Telnet uses TCP on the default port number 23. To use Telnet, make sure it is installed on your system. If not, run the following command to install it:

$ sudo apt-get install telnetd

To run Telnet using the simple Terminal, you just have to enter the following command:

$ telnet ip_address_of_your_remote_server

Python has the telnetlib module to perform Telnet functions through Python scripts. Before telnetting your remote device or router, make sure they are configured properly and, if not, you can do basic configuration by using the following command in the router's Terminal:

configure terminal
enable password 'set_Your_password_to_access_router'
username 'set_username' password 'set_password_for_remote_access'
line vty 0 4
login local
transport input all
interface f0/0
ip add 'set_ip_address_to_the_router' 'put_subnet_mask'
no shut
end
show ip interface brief

Now, let's see the example of Telnetting a remote device. For that, create a telnet_example.py script and write following content in it:

import telnetlib
import getpass
import sys

HOST_IP = "your host ip address"
host_user = input("Enter your telnet username: ")
password = getpass.getpass()

t = telnetlib.Telnet(HOST_IP)
t.read_until(b"Username:")
t.write(host_user.encode("ascii") + b" ")
if password:
t.read_until(b"Password:")
t.write(password.encode("ascii") + b" ")

t.write(b"enable ")

t.write(b"enter_remote_device_password ") #password of your remote device
t.write(b"conf t ")
t.write(b"int loop 1 ")
t.write(b"ip add 10.1.1.1 255.255.255.255 ")
t.write(b"int loop 2 ")
t.write(b"ip add 20.2.2.2 255.255.255.255 ")
t.write(b"end ")
t.write(b"exit ")
print(t.read_all().decode("ascii") )

Run the script and you will get the output as follows:

student@ubuntu:~$ python3 telnet_example.py
Output:
Enter your telnet username: student
Password:


server>enable
Password:
server#conf t
Enter configuration commands, one per line. End with CNTL/Z.
server(config)#int loop 1
server(config-if)#ip add 10.1.1.1 255.255.255.255
server(config-if)#int loop 23
server(config-if)#ip add 20.2.2.2 255.255.255.255
server(config-if)#end
server#exit

In the preceding example, we accessed and configured a Cisco router using the telnetlib module. In this script, first, we took the username and password from the user to initialize the Telnet connection with a remote device. When the connection was established, we did further configuration on the remote device. After telnetting, we will be able to access a remote server or device. But there is one very important disadvantage of this Telnet protocol, and that is all the data, including usernames and passwords is sent over a network in a text manner, which may cause a security risk. Because of that, nowadays Telnet is rarely used and has been replaced by a very secure protocol named Secure Shell, known as SSH.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset