Got it

SDN and automation - Python Telnetlib

Latest reply: Apr 19, 2022 07:38:21 1451 20 16 0 1

Introduction

The telnetlib is a module in the standard Python library. It provides the telnetlib.Telnet class for implementing the Telnet function. See RFC854 for details about the protocol: https://datatracker.ietf.org/doc/html/rfc854.html.

Different methods in the telnetlib.Telnet class are called to implement different functions.

from telnetlib import Telnet                 # Import the Telnet class of the telnetlib module.

tn = Telnet(host=None, port=0[, timeout])   # Create a Telnet connection to a specified server.

tn.read_all()                           # Invoke the read_all() method.

Telnetlib


For more information, see https://docs.python.org/3/library/telnetlib.html.

Configuration example

Telnetlib


The implementation process is as follows:

1. Configure the Telnet server

[Huawei] user-interface vty 0 4

[Huawei-ui-vty0-4] authentication-mode password

[Huawei-ui-vty0-4] set authentication password simple Huawei@123

[Huawei-ui-vty0-4] protocol inbound telnet

[Huawei-ui-vty0-4] user privilege level 15

[Huawei-ui-vty0-4] quit

[Huawei] telnet server enable

2. Manually verify and view the Telnet login procedure as a reference for code implementation.

Using the Windows OS as an example:

C:\>telnet 192.168.10.10

Login authentication

Password:

Info: The max number of VTY users is 5, and the number of current VTY users on line is 1.

The current login time is 2021-01-01 04:00.

%

<Huawei>

3. Compile and run Python code.


import telnetlib                                         # Imports the module.

host ='192.168.10.10'                               # Sets the IP address for a host

password = 'Huawei@123'                    # Sets the password for logging in to the device

tn = telnetlib.Telnet(host)                        # Logs in to the host through Telnet.

tn.read_until(b'Password:')                   # Prints data until 'Password: ' is displayed

tn.write(password.encode('ascii') + b'\n')      # Sets an ASCII password and starts a new line

print (tn.read_until(b'<Huawei>').decode('ascii))   # Prints data until <Huawei> is displayed.

tn.close()                                         # Closes the Telnet connection.


In Python, the encode() and decode() functions are used to encode and decode strings in a specified format, respectively. In this example, password.encode('ascii') is to convert the string HuaweiQ@123 into the ASCll format. The encoding format complies with the official requirements of the telnetlib module.

Add a string b, b'str', indicating that the string is a bytes object. In this example, b'Password': indicates that the string Password:' is converted into a string of the bytes type. The encoding format complies with the official requirements of the telnetlib module.

For more information about Python objects, see

https://docs.python.org/3/reference/datamodel.html#objects-values-and-types.

4. Verify the result.

# Run Python code in the compiler.

Info: The max number of VTY users is 5, and the number of current VTY users online is 1.

The current login time is 2021-01-01 04:00.

%

<Huawei>

5


The post is synchronized to: HCIA - Datacom class notes

Thanks for sharing.
View more
  • x
  • convention:

123
Back to list

Comment

You need to log in to comment to the post Login | Register
Comment

Notice: To protect the legitimate rights and interests of you, the community, and third parties, do not release content that may bring legal risks to all parties, including but are not limited to the following:
  • Politically sensitive content
  • Content concerning pornography, gambling, and drug abuse
  • Content that may disclose or infringe upon others ' commercial secrets, intellectual properties, including trade marks, copyrights, and patents, and personal privacy
Do not share your account and password with others. All operations performed using your account will be regarded as your own actions and all consequences arising therefrom will be borne by you. For details, see " User Agreement."

My Followers

Login and enjoy all the member benefits

Login

Block
Are you sure to block this user?
Users on your blacklist cannot comment on your post,cannot mention you, cannot send you private messages.
Reminder
Please bind your phone number to obtain invitation bonus.