Expect script to automate telnet login

I am trying to create a wait script for automatically logging into my device via telnet

If there are no multiple capabilities for the wait command, the script works fine and logs into the system.

#!/usr/bin/expect set timeout 20 set ip [lindex $argv 0] set port [lindex $argv 1] set user [lindex $argv 2] set password [lindex $argv 3] spawn telnet $ip $port expect "'^]'." sleep .1; send "\r"; sleep .1; expect "login:" send "$user\r" expect "Password:" send "$password\r"; interact 

The script script works fine and successfully logs when I pass the correct parameters. But as soon as I add additional branches (for error handling) to the expect command, the script gets stuck at the login: prompt. After some time, it prints Script Error Any help ?? Invalid script below.

 #!/usr/bin/expect set timeout 20 set ip [lindex $argv 0] set port [lindex $argv 1] set user [lindex $argv 2] set password [lindex $argv 3] spawn telnet $ip $port expect "'^]'." sleep .1; send "\r"; expect { "login:" { send "$user\r" expect "Password:" send "$password\r"; interact } "host: Connection refused" { send_user "ERROR:EXITING!" exit } } 

PS: This script needs to be further developed in order to wait for additional tips for loading various build images on the device. Only telnet (console) connection works. therefore ssh is not an option.

+6
source share
1 answer

My bad. The problem was in braces. They must be on the same line as the wait command.

+4
source

Source: https://habr.com/ru/post/899429/


All Articles