Specific use of Telnetlib in python automatic operation and maintenance

  • 2021-11-10 10:26:34
  • OfStack

Directory foreword: 1. Import telnetlib library can be used directly. 2. Configure server, user name, password, cmd command, etc. 3. Functions

Foreword:

Telnet connection is used when connecting ZTE equipment (ZTE network card used by the system) remotely. There are two verifications when connecting. It is a tedious process to input user name and password and execute commands every time. Using telnetlib library provided by Python, scripts can be written to log in to the server in batches and execute commands to query data volume.

1. Importing the telnetlib library can be used directly.


from telnetlib import Telnet

2. Configure server, username, password, cmd command, and so on


############################## Configuration information is required ##########################
# Associated device server IP List 
Hosts=['192.168.1.xx','192.168.1.xx','192.168.1.xx','192.168.1.xx','192.168.1.xx','192.168.1.xx'] 
#  Login user name    
username = 'xx' 
#  Login password   
password = 'xx'
#EN Command 
EN = 'xx'
#2 Secondary authentication password 
ZXR10 = 'xx'   
#  Command prompt 
finish = 'xx#' 
# Commands to be executed 
commands = ['show lte data processing report','show interface xgei1/1']     
# Write output to file 
g_outfilePath = '/home/tnOutResult'
####################################################################

IP, username, pwd, etc. are passed into the function as parameters.


def do_telnet(Hosts, username, password, finish, commands):

3. Functions

The function mainly realizes the remote login and command execution of Telnet.


#  Connect Telnet Server   
        tn = Telnet(host, port=23, timeout=10)
        #tn.set_debuglevel(2) 

        #  Enter the login user name   
        tn.read_until('Username:')  
        tn.write(username + '\n') 

        #  Enter the login password   
        tn.read_until('Password:')  
        tn.write(password + '\n')

        #  Input command en  
        tn.read_until('ZXR10>')  
        tn.write(EN + '\n')

        #  Input 2 Secondary authentication password   
        tn.read_until('Password:')  
        tn.write(ZXR10 + '\n')

        #  Execute the command after logging in   
        tn.read_until(finish)  
        for command in commands:  
            tn.write(command + '\n')
            time.sleep(2)
            for i in range(10):
                tn.write(' ')
                time.sleep(0.1)
            Outresult = tn.read_very_eager()
    # Upon completion of execution, terminate Telnet Connect (or input exit Exit)   
    #tn.read_until(finish)  
    tn.close() # tn.write('exit\n')  

The read_very_eager () function is the read method in the telnetlib module, where the delay time. sleep (2) should be set to ensure that the data is read out.

Introduction of read method in telnetlib module:

read_until (): Returned when the desired information is present in the result. read_some (): Returns whenever there is a result. read_very_lazy (): Returns the data in the buffer.

telnetlib uses buffer processing, so the data is not returned immediately, but put in the buffer first. Much of the read processing revolves around this buffer. But when the buffer information arrives, it is not clear. Maybe it is fast, maybe it is slow, maybe it arrives separately, and maybe it is received immediately. Therefore, in the case where the data is not 1, read_until () is used to determine whether the data in the buffer has the desired content, and if not, wait unless the timeout time is reached.

See:

Telnetlib Use Tutorial

https://github.com/wnn0809/WebLovePy/blob/master/Py/Telnetlib%E8%BF%90%E7%BB%B4


Related articles: