Three programming codes for autoTelnet

Source: Internet
Author: User

In some programming applications, we often involve the content related to autoTelnet. Here we will discuss the implementation of autoTelnet. We can implement this file in three ways. For details about the code implementation, refer to the following section.

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

(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 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!''  

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.