Telnet based command execution on Cisco/Huawei devices

Featured Image
Hasan-Uz-Zaman Ashik

Written by Hasan-Uz-Zaman

March 12, 2021

h

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.

  1. read_very_eager() Helps to fetch the output after executing a command.
  2. read_until() Reads until find a provided string. Optionally timeout parameter can also use here to avoid the program running forever.
  3. write() This is used to execute a command in an established socket or connection.
  4. 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. 

 

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. 

 

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.

 

So the final script will be:

 

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.

You May Also Like…

Hasan-Uz-Zaman

Hasan-Uz-Zaman

Sr. Network Engineer

Zaman is passionate about Network Automation, Cloud Automation, DevOps, SDN, and Web Application development. He has developed network monitoring tools, automation scripts, and web applications, focusing on integrating advanced technologies to optimize and secure operations.

Let's start talking!

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *