Rob F
Rob F

Reputation: 539

Why does this expect code not work?

Not sure if this is more of a scripting/unix question or a programming one, but I tried on the unix stackexchange and got no responses, so:

The following expect code seems to work; that is, it appears to enter text in answer to the password prompt. However, the device never actually mounts.

But if I simply enter the command into a shell and type the password in by hand the device mounts successfully.

So I'm curious where the input is actually ending up, as it never seems to 'catch' the password yet doesn't present an error message either? In fact the output looks exactly the same in both instances, but only in the case of running the command and typing the password manually do I see my files appear over the network.

Here is the code:

#!/usr/bin/expect
spawn sudo mount.cifs "//WinPC/My Pictures" /home/LinPC/Desktop/Pictures -o user=Me
expect "Password: " {
    set send_slow {1 .1}
    send -s "a_password"
}

UPDATE: Got the help I needed to make it work: had to insert 'expect eof' after sending the password so that it doesn't end prematurely. However I now wish to progress to changing it to 'expect_background' so that I can have the same trigger response to issuing multiple mount commands. The following ends prematurely, and 'expect eof' at the end causes an error:

expect_background "Password:" {
    send "a_password\r"
    expect eof
}
spawn sudo mount.cifs "//WinPC/My Pictures" /home/LinPC/Desktop/Pictures -o user=Me
expect eof

What should it look like?

UPDATE: the following code block illustrates the current problem:

expect_background "Password: " {
    send "a_password\r"
    expect eof
}
spawn sudo mount.cifs "//WinPC/My Pictures" /home/LinPC/Desktop/Pictures -o user=someone

expect "Password: " {
    send "a_password\r"
    expect eof
}
#The password prompt gets answered by 'expect' but not 'expect_background'.
#If I delete the last 'expect' and insert 'expect eof' it hangs for a short
#while at the password prompt (around 3 seconds) then exits.
#Why?

Upvotes: 2

Views: 659

Answers (1)

Dinesh
Dinesh

Reputation: 16428

Add one more expect statement after sending the password.

send -s "a_password\r"
expect eof 

The eof will make the Expect to wait till the end of program.

Upvotes: 3

Related Questions