使用SSH连接中的Expect脚本,无法发送密码命令 [英] Send command for password doesn't work using Expect script in SSH connection
问题描述
我希望能够使用Expect脚本通过SSH连接远程登录到路由器,
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.
我使用SSH密钥通过手动键入是"来进行身份验证.当它提示我是/否"时我第一次登录时会提出疑问.然后,在第一次尝试后,路由器会自动提示我输入密码.确切地说,它有两行:
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:
然后几秒钟后,它又回到了我的根目录,好像send命令在该密码提示后没有发送任何内容.
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.
因此,我对我的Expect和脚本的了解非常有限,我的问题是:对此有什么解决方案?怎么了?
So, my question, with my very limited knowledge about Expect and scripting in general, is: What is the solution for this? What was wrong?
推荐答案
我遇到了这个确切的问题,即我无法拾取" expect
中断的地方,我认为这将是默认行为
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.
就像@glennjackman在评论中说的那样,在末尾添加interact
可以让您从上次中断的地方继续学习.
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
这篇关于使用SSH连接中的Expect脚本,无法发送密码命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!