Table of Contents
Introduction
Full Script
Output
The telnetlib
library in Python is a library that provides support for Telnet protocols. It allows you to write scripts that can interact with remote devices using Telnet protocols, and automate tasks that would otherwise require manual intervention. The library provides a simple and straightforward interface to interact with Telnet devices, making it easy to perform tasks such as logging in to a remote device, executing commands, and retrieving data.
The telnetlib
library provides the Telnet
class, which provides the primary interface for interacting with Telnet devices. To use the library, you would start by creating an instance of the Telnet
class and connecting to a remote device using the open
method. Once connected, you can send commands to the remote device using the write
method, and retrieve the response using the read_until
method.
SSH is replacing telnet as telnet reveals all information in plain text. But still, some enterprises are using telnet to connect to switch or router. Most network automation libraries use SSH to connect, so those enterprises’ use of telnetlib is essential.
We often use CLI-based tools like Xshell or Putty to communicate with Router, Switch, or other networking devices. But performing the same task repeatedly on lots of devices not only kills our time but also error-prone. In this article, the python telnetlib library will be used to connect with Huawei devices. In a similar process, telnetlib can be used to connect Cisco devices. Here, Python 2.7 version is used.
Some methods of telnet class defined in telnetlib.
- read_very_eager() Helps to fetch the output after executing a command.
- read_until() Reads until find a provided string. Optionally timeout parameter can also use here to avoid the program running forever.
- write() This is used to execute a command in an established socket or connection.
- expect() Reads until matches a given list. Where in read_until() only one can pass a string. Similar to read_until() it accepts timeout parameter.
For more detail please visit telnetlib documentation page.
First, some necessary variables are defined, and necessary modules are imported. The Time module is used to provide some delay between running commands. The username and password lists contain likely words that will be prompted from the device at the time of providing username and password. Here, the first character for this field is omitted as the letter could be the upper or smaller case depending on different Huawei devices. Screen length is set to zero so that after executing a command, the output can come in a single screen.
1 2 3 4 5 6 7 8 9 10 |
import telnetlib, time ip = 'device ip' port = '23' username = 'username' password = 'password' expected_username_promt = ["sername:", "ogin:"] expected_password_promt = ["assword:"] new_line = "\r\n" command = 'display interface description' screen_length = 'screen-length 0 temporary' |
As necessary variable names are set, we can forward to make a connection to the IP and send username, password. For troubleshooting errors, we will use Python try-except method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
try: connection = telnetlib.Telnet(ip, port) print "connect success\n"; except IOError: print "IOError, could not open a connection " if connection: """send username""" try: connection.expect(expected_username_promt) connection.write(username + new_line) except IOError: print "sending username: %s failed" % username """send password""" try: connection.expect(expected_password_promt) connection.write(password + new_line) except IOError: print "sending password failed" |
After establishing a connection, we can wait for reading values to some desired value using the read_until() method. We have split our required device name portion using the “>” symbol. read_very_eager() function will continue reading until some cooked data available if any. We have to pass a new line after each command.
1 2 3 4 5 6 7 8 9 |
device_name = connection.read_until(">").split()[-1] print device_name connection.write(screen_length) connection.write(new_line) connection.write(command) connection.write(new_line) time.sleep(2) #waiting some time result = connection.read_very_eager() print result |
So the final script will be:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import telnetlib, time ip = 'device ip' port = '23' username = 'username' password = 'password' expected_username_promt = ["sername:", "ogin:"] expected_password_promt = ["assword:"] new_line = "\r\n" command = 'display interface description' screen_length = 'screen-length 0 temporary' try: connection = telnetlib.Telnet(ip, port) print "connect success\n"; except IOError: print "IOError, could not open a connection " if connection: """send username""" try: connection.expect(expected_username_promt) connection.write(username + new_line) except IOError: print "sending username: %s failed" % username """send password""" try: connection.expect(expected_password_promt) connection.write(password + new_line) except IOError: print "sending password failed" device_name = connection.read_until(">").split()[-1] print device_name connection.write(screen_length) connection.write(new_line) connection.write(command) connection.write(new_line) time.sleep(2) #waiting some time result = connection.read_very_eager() print result else: print "Please make a connection first." |
Output:
connect success
<Huawei_device>
screen-length 0 temporary
Info: The configuration takes effect on the current user terminal interface only.
< Huawei_device >display interface description
Interface Description
Eth0/0/0 HUAWEI, Ethernet0/0/0 Interface
GE0/2/0 HUAWEI, GigabitEthernet0/2/0 Interface
GE0/2/1 HUAWEI, GigabitEthernet0/2/1 Interface
GE0/2/2 HUAWEI, GigabitEthernet0/2/2 Interface
GE0/2/3 HUAWEI, GigabitEthernet0/2/3 Interface
GE0/2/4 HUAWEI, GigabitEthernet0/2/4 Interface
GE0/2/5 HUAWEI, GigabitEthernet0/2/5 Interface
GE0/2/6 HUAWEI, GigabitEthernet0/2/6 Interface
GE0/2/7 HUAWEI, GigabitEthernet0/2/7 Interface
GE0/2/8 HUAWEI, GigabitEthernet0/2/8 Interface
GE0/2/9 HUAWEI, GigabitEthernet0/2/9 Interface
GE0/2/10 HUAWEI, GigabitEthernet0/2/10 Interface
GE0/2/11 HUAWEI, GigabitEthernet0/2/11 Interface
GE0/2/12 HUAWEI, GigabitEthernet0/2/12 Interface
GE0/2/13 HUAWEI, GigabitEthernet0/2/13 Interface
GE0/2/14 HUAWEI, GigabitEthernet0/2/14 Interface
GE0/2/15 HUAWEI, GigabitEthernet0/2/15 Interface
GE0/2/16 HUAWEI, GigabitEthernet0/2/16 Interface
GE0/2/17 HUAWEI, GigabitEthernet0/2/17 Interface
GE0/2/18 HUAWEI, GigabitEthernet0/2/18 Interface
GE0/2/19 HUAWEI, GigabitEthernet0/2/19 Interface
GE0/2/20 HUAWEI, GigabitEthernet0/2/20 Interface
GE0/2/21 HUAWEI, GigabitEthernet0/2/21 Interface
GE0/2/22 HUAWEI, GigabitEthernet0/2/22 Interface
GE0/2/23 HUAWEI, GigabitEthernet0/2/23 Interface
GE0/2/24(10G)
GE0/2/25(10G)
NULL0 HUAWEI, NULL0 Interface
< Huawei_device >
The telnetlib
library is a powerful tool for automating tasks involving Telnet devices. Whether you need to perform routine maintenance tasks, monitor network devices, or retrieve data, the telnetlib
library provides an easy-to-use and reliable interface for interacting with Telnet devices.
0 Comments