2

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 root@10.9.8.2
expect "root@10.9.8.2'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.

2

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 root@10.9.8.2\r"
expect "root@10.9.8.2's password:"
send "SecondPassword\r"
expect eof

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.