cannot execute command after logging in with expect

I have the following script:

#!/usr/bin/expect -f

    set timeout 60

    spawn ssh -X user@login.domain.co.uk

    expect "Password:"
    # Send the password, and then wait for a shell prompt.
    send "password\r"
    exp_continue
    expect "user*"
    send "ls -la\r"

however I get the following:

Password: command returned bad code: -101
    while executing
"exp_continue"
    (file "./hpclogin.sh" line 10)

If I remove the exp_continue i.e.

#!/usr/bin/expect -f

    set timeout 60

    spawn ssh -X user@login.domain.co.uk

    expect "Password:"
    # Send the password, and then wait for a shell prompt.
    send "password\r"
    expect "user*"
    send "ls -la\r"

I can log in successfully, however the ls -la command does not get executed. Is there something wrong with the flow-control of my program?

Answers


exp_continue is only useful inside an expect block. For example:

spawn ssh -X user@example.com
expect {
    "continue connecting (yes/no)? " {
        send "yes\r"
        exp_continue"
    }
    "Password:"
}
send "password\r"

I think you're not seeing the ls output because you're not expecting to see anything after to send it. Depending on your workflow, here are 2 thoughts:

  1. add the command as arguments to ssh

    spawn ssh -X user@example.com ls -la
    expect {
        "continue connecting (yes/no)? " {
            send "yes\r"
            exp_continue"
        }
        "Password:"
    }
    send "password\r"
    expect eof
    
  2. expect a prompt after the command, and log out afterwards

    spawn ssh -X user@example.com
    expect {
        "continue connecting (yes/no)? " {
            send "yes\r"
            exp_continue"
        }
        "Password:"
    }
    send "password\r"
    expect $theprompt
    send "ls -la\r"
    expect $theprompt
    send "exit\r"
    expect eof
    

Of course, this would be much simpler using ssh keys:

ssh-keygen
ssh-copy-id user@example.com
ssh -X user@example.com ls -la

Need Your Help

Rails Dev Environment - Any Tips?

ruby-on-rails ruby-on-rails-3 development-environment sublimetext sublimetext2

I'm using the ruby environment on my host via ssh and all my development goes through sublimeText2.

Visual Studio 2013 - Debugging Ajax, where are the error messages in Internet Explorer Debugging tools

javascript jquery ajax

I had a problem with some of my Javascript / Ajax code basically I made a typo in an Ajax call. Something like the following - loadtext.html was incorrectly named lodtext.html (This is a simplified