Send command for password doesn't work using e

2019-07-09 14:40发布

问题:

I want to be able to login remotely to the router using SSH connection using 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 couple 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? Is there any idea what was wrong?

回答1:

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