Telnetlib Module#

The telnetlib is a Python module, that provides a Telnet() class that implements the Telnet protocol. Python’s telnetlib lets you easily automate access to Telnet servers (device), even from non-Unix machines. The telnetlib library is already included in the python package.

Note

telnetlib has deprecated since version 3.11, will be removed in version 3.13

With Python ‘help()’ function we check the ‘telnetlib’ module doc:

>>> help('telnetlib')
Help on module telnetlib:

# Output is Omitted

    Example:

    >>> from telnetlib import Telnet
    >>> tn = Telnet('www.python.org', 79)   # connect to finger port
    >>> tn.write(b'guido\r\n')
    >>> print(tn.read_all())
    Login       Name               TTY         Idle    When    Where
    guido    Guido van Rossum      pts/2        <Dec  2 11:10> snag.cnri.reston.

From the above output copy the example and amend it as below:

from telnetlib import Telnet
tn = Telnet('192.168.10.11')   # connecting device ip
tn.write(b'admin\n')  # Username 
tn.write(b'cisco\n')  # Password
tn.write(b'sh ip int bri\n')  # Cisco router command
tn.write(b'exit\n')  # Cisco router command
print(tn.read_all())  # Print the output

Output

root@NetworkAutomation-1:~# python3 01_basic_script.py
b'\r\n\r\nUser Access Verification\r\n\r\nUsername: admin\r\nPassword: \r\nR1>sh ip int bri\r\nInterface                  IP-Address      OK? Method Status                Protocol\r\nFastEthernet0/0            192.168.10.11   YES NVRAM  up                    up      \r\nFastEthernet0/1            unassigned      YES NVRAM  administratively down down    \r\nR1>exit\r\n'
root@NetworkAutomation-1:~#

The above text in byte string is difficult to understand by humans, to make it human readable we need to convert it in plain text string.

from telnetlib import Telnet
tn = Telnet('192.168.10.11')   # connect to finger port
tn.write(b'admin\n')  # Username
tn.write(b'cisco\n')  # Password
tn.write(b'sh ip int bri\n')  # Cisco router cmd
tn.write(b'exit\n')  # Cisco router cmd
print(tn.read_all().decode('ascii'))  # Print the output

Output

root@NetworkAutomation-1:~# python3 01_basic_script.py


User Access Verification

Username: admin
Password:
R1>sh ip int bri
Interface                  IP-Address      OK? Method Status                Protocol
FastEthernet0/0            192.168.10.11   YES NVRAM  up                    up
FastEthernet0/1            unassigned      YES NVRAM  administratively down down
R1>exit

Python module telnetlib not only has basic methods for sending and receiving data but also has a few techniques (methods) that will watch and wait for a particular string to arrive from the remote system. The standard way to use it, copy the Python script from telnetlib docs and amend it, as per your requirement.

import getpass
import telnetlib

# A variable for the device IP address
HOST = "192.168.10.10" 
# A variable for the username
user = input("Enter your Username: ")
# Prompt the user for a password without echoing 
password = getpass.getpass()


# Variable 'HOST' to pass in to the object
tn = telnetlib.Telnet(HOST)
# read until found the `Username:`
tn.read_until(b"Username: ")
# Convert sending string to `ascii` encoding
tn.write(user.encode('ascii') + b"\n")
if password:
    tn.read_until(b"Password: ")
    tn.write(password.encode('ascii') + b"\n")
tn.write(b"sh ip int bri\n")  # Cisco command
tn.write(b"exit\n")

# Function 'read_all()' catch all return string
print(tn.read_all().decode('ascii'))

Output

root@NetworkAutomation-1:~# python3 02_standard_script.py
Enter your Username: admin
Password:

R1>sh ip int bri
Interface                  IP-Address      OK? Method Status                Protocol
FastEthernet0/0            192.168.10.11   YES NVRAM  up                    up
FastEthernet0/1            unassigned      YES NVRAM  administratively down down
R1>exit

Note

getpass module allows you to request a password prompt without displaying the input characters.