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:
- #!/usr/bin/expect
- set timeout 100
- set TERM xterm
- set SERVER "10.32.17.10"
- set USER "root"
- set PASSWD "123456"
- spawn Telnet
- expect "Telnet> "
- send "open $SERVERr"
- expect "Username:"
- send "$USERr"
- expect "Password:"
- send "$PASSWDr"
- expect "longjiang-zero>"
- send "enr"
- expect "Password:"
- send "$PASSWDr"
- expect "longjiang-zero#"
- send "conf tr"
- expect "longjiang-zero(config)#"
- send "int fa0/1r"
- expect "longjiang-zero(config-if)#"
- send "switchport mode multir"
- expect "longjiang-zero(config-if)#"
- send "endr"
- expect "longjiang-zero#"
- send "exitr"
- interact
Iii. Python implementation. File Name: autoTelnet. py. The Code is as follows:
- #!/usr/bin/python
- import Telnetlib
- host = ''10.32.17.10''
- user = ''root''
- password = ''123456''
- commands = [''en'',password,''conf t'',''int fa0/1'',''switchport mode multi'',''end'']
- tn = Telnetlib.Telnet(host)
- tn.read_until("Username:")
- tn.write(user + "n")
- tn.read_until("Password:")
- tn.write(password + "n")
- for command in commands:
- tn.write(command+''n'')
- tn.write("exitn")
- print tn.read_all()
- print ''Finish!''