Using bash commands in expect script
I've tried using some bash commands in an expect script before spawning anything, and this doesn't seem to work:
#!/usr/bin/expect
sudo ifdown usb0
sudo ifup usb0
expect "[sudo] password for stud:"
send "FirstPassword\r"
spawn ssh [email protected]
expect "[email protected]'s password:"
send "SecondPassword\r"
expect eof
I've tried running this with the first part of the script commented out (because it's not neccesary to do this check 100% of the time, just better), but in that case I enter the device, and literally can't do anything there. I'd like to ifdown ifup, enter pass, ssh, enter pass, and scope to that shell.
Nope. you can't expect to run bash commands in an expect interpreter, just like you can't run perl commands in a python interpreter -- they're different languages. If you want to run some bash commands that require user interaction (sudo), then you have to spawn bash
set prompt {\$ $} ; # this is a regular expression that should match the
# *end* of you bash prompt. Alter it as required.
spawn bash
expect -re $prompt
send "sudo ifdown usb0\r"
expect {
"[sudo] password for stud:" {
send "FirstPassword\r"
exp_continue
}
-re $prompt
}
send "sudo ifup usb0\r"
expect -re $prompt
send "ssh [email protected]\r"
expect "[email protected]'s password:"
send "SecondPassword\r"
expect eof