4

I want to be able to log in remotely to the router using an SSH connection using an Expect script, and this is what I have:

#!/usr/bin/expect
set host "192.168.1.50"
set user "root"
set password "mypass"
spawn ssh "$user\@host"
expect "word:"
send "$password\r"

When I tried to use that, the router became unresponsive (and possibly timed out). When it went to password prompt line, it immediately went back out.

I did authentication using my SSH key by manually typing "yes" when it prompted me with "yes/no" questions the first time I logged in. Then, after the first try, the router just automatically prompted me with the password. To be exact, it has two lines:

User Authentication
Password:

Then after a couple of seconds, it went back to my root as if the send command did not send anything after that password prompt.

So, my question, with my very limited knowledge about Expect and scripting in general, is: What is the solution for this? What was wrong?

2
  • 3
    After send, add the line interact so you can regain control. If that doesn't fix it, run with expect -d to see debug output. Commented Nov 4, 2015 at 21:37
  • it works, thank you very much. Commented Nov 4, 2015 at 21:48

1 Answer 1

3

I had this exact problem where I wasn't able to "pick up" where expect left off, which I thought was going to be the default behavior.

As @glennjackman said in the comments, adding interact at the end allows you to pick up where it left off.

#!/usr/bin/expect
set host "192.168.1.50"
set user "root"
set password "mypass"
spawn ssh "$user\@host"
expect "word:"
send "$password\r"
interact
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.