#Encoding=utf-8ImportTelnetlibImport Timedefdo_telnet (Host, username, password, finish, commands):#connecting to the Telnet serverTN = Telnetlib. Telnet (Host, port=23, timeout=10) Tn.set_debuglevel (2) #Enter login user nameTn.read_until ('ENTER USERNAME <') Tn.write (username+'\ r \ n') #Enter login PasswordTn.read_until ('ENTER PASSWORD <') tn.write (password+'\ r \ n') #Execute command When login is completeTn.read_until (finish) forCommandinchCommands:result= Tn.write ('%s\r\n'%command); #after execution, terminate Telnet connection (or enter exit Exit)Tn.read_until ('COMMAND EXECUTED') Tn.close ()#tn.write (' exit\n ')if __name__=='__main__': #configuration OptionsHost ='10.68.165.25' #Telnet Server IPUsername ='SYSTEM' #Login User NamePassword ='SYSTEM' #Login Passwordfinish ='Level COMMAND <___>' #Command Promptcommands = ['Zusi;'] Do_telnet (Host, username, password, finish, commands)
Next: How to output the execution result of write???
Python enables Telnet to remotely log on to the device and execute commands