Python telnet programming code

Source: Internet
Author: User

In settings related to telnet, we usually access other languages for editing. Here we will mainly introduce how to use scripts to implement this operation. Including the implementation process of Python telnet. Let's take a look at the content of the article.

I. Shell implementation, file name: autotelnet. sh, the Code is as follows:

 
 
  1. (sleep 1;echo "root";sleep 1;echo "123456";sleep 1;echo "en";sleep 1;echo "1qazse4";sleep 1;echo "conf t";sleep 1;echo "int fa0/1";sleep 1;echo "switchport mode multi";sleep 1;echo "end";sleep 1;echo "exit") | telnet 10.32.17.10 

Ii. Verify CT. The file name is autotelnet. exp. The Code is as follows:

 
 
  1. #!/usr/bin/expect  
  2. set timeout 100  
  3. set TERM xterm  
  4. set SERVER "10.32.17.10"  
  5. set USER "root"  
  6. set PASSWD "123456"  
  7. spawn telnet  
  8. expect "telnet> "  
  9. send "open $SERVERr"  
  10. expect "Username:"  
  11. send "$USERr"  
  12. expect "Password:"  
  13. send "$PASSWDr"  
  14. expect "longjiang-zero>"  
  15. send "enr"  
  16. expect "Password:"  
  17. send "$PASSWDr"  
  18. expect "longjiang-zero#"  
  19. send "conf tr"  
  20. expect "longjiang-zero(config)#"  
  21. send "int fa0/1r"  
  22. expect "longjiang-zero(config-if)#"  
  23. send "switchport mode multir"  
  24. expect "longjiang-zero(config-if)#"  
  25. send "endr"  
  26. expect "longjiang-zero#"  
  27. send "exitr"  
  28. interact 

Iii. Python telnet implementation. File Name: autotelnet. py. The Code is as follows:

 
 
  1. #!/usr/bin/python  
  2. import telnetlib  
  3. host = ''10.32.17.10''  
  4. user = ''root''  
  5. password = ''123456''  
  6. commands = [''en'',password,''conf t'',''int fa0/1'',''switchport mode multi'',''end'']  
  7. tn = telnetlib.Telnet(host)  
  8. tn.read_until("Username:")  
  9. tn.write(user + "n")  
  10. tn.read_until("Password:")  
  11. tn.write(password + "n")  
  12. for command in commands:  
  13. tn.write(command+''n'')  
  14. tn.write("exitn")  
  15. print tn.read_all()  
  16. print ''Finish!'' 

Now we have completed the Python telnet implementation process.

Related Article

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.