简体   繁体   English

在 SSH 连接中使用 Expect 脚本发送密码命令不起作用

[英]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:我希望能够使用使用 Expect 脚本的 SSH 连接远程登录到路由器,这就是我所拥有的:

#!/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.我第一次登录时,当它提示我“是/否”问题时,我通过手动键入“是”来使用我的 SSH 密钥进行身份验证。然后,在第一次尝试后,路由器会自动提示我输入密码。 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?所以,我的问题是,由于我对 Expect 和脚本的一般知识非常有限,所以我的问题是:这个问题的解决方案是什么? 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.我遇到了这个确切的问题,我无法在expect停止的地方“拿起”,我认为这将成为默认行为。

As @glennjackman said in the comments, adding interact at the end allows you to pick up where it left off.正如@glennjackman 在评论中所说,在最后添加interact可以让您从中断的地方继续。

#!/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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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