Python enables Telnet to remotely log on to the device and execute commands

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.