Send command for password doesn't work using expect script in ssh connection

时光总嘲笑我的痴心妄想 提交于 2019-12-23 23:43:33

问题


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


来源:https://stackoverflow.com/questions/33531131/send-command-for-password-doesnt-work-using-expect-script-in-ssh-connection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!