I have the following expect script to:
- SSH to host
- accept password from user if required
- Once ssh successful (prompt appears), set an environment variable
- Once back at prompt again, run a program for network testing
- Once back at prompt again, exit expect and close ssh session
#!/usr/bin/expect
set hostname [lindex $argv 0]
set working_dir [lindex $argv 1]
set package [lindex $argv 2]
set tcl_test $working_dir/$package.tcl
set config $working_dir/$package.config
set logdir $working_dir/$package
set timeout 30
set prompt "\~\\\]\\\$"
eval spawn ssh $hostname
expect {
-re "(yes/no)" {send "yes\r"}
-re "password\:" {
interact {
-o
-re $prompt {send "export VARIABLE1=$working_dir\"\r"}
-re $prompt {send "issue-test-command -config $config -module $tcl_test\r"}
}
}
}
The password match and interact works correctly. As soon as $prompt is matched, the environment variable export command is issued:
-re $prompt {send "export VARIABLE1=$working_dir\"\r"}
This is correct, but then rather than moving on to the line:
-re $prompt {send "issue-test-command -config $config -module $tcl_test\r"}
The same line is read, the pattern is matched again and the same command is performed. This results in a permanent loop of issuing the same command.
How can I force interact to move on to the next line, or use a different expect built-in to achieve what I’m after?
Imagine you’re giving instructions to someone:
You wouldn’t be surprised if they get confused. That’s what you’re doing to Expect with multiple
-re $promptbranches. However Expect just takes the first instruction you tell it.Try this:
BTW:
exp_continueaftersend "yes\r"exportcommand