简体   繁体   中英

Send command for password doesn't work using Expect script in SSH connection

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM