在Ubuntu的Bashpipe道答案

在一个bash脚本中,我想知道如何pipe道答案,例如:

ssh -l username -i /home/ubuntu/.ssh/id host.com < yes 

ssh连接会询问我是否想继续连接,并且我想自动input答案,然后input,但我的脚本“<yes”似乎不起作用。 有没有人知道如何正确input和pipe道在Ubuntu下bash响应“是”?

您通常只会询问您是否希望在ssh执行主机密钥检查时进行连接。 而不是尝试通过使用期望或pipe道来禁用,也许你可以在你的SSHconfiguration中禁用它。

StrictHostKeyChecking no添加到你的~/.ssh/config ,ssh将不再询问你是否要连接,它只会连接。

使用ssh -o "StrictHostKeyChecking no" -l username -i /home/ubuntu/.ssh/id host.com来完全避免这个问题。

首先,你应该试过这个,而不是< yes

 $ echo "yes" | ssh -l username -i /home/ubuntu/.ssh/id host.com 

其次,即使这不会工作,因为SSH不读取标准input的答案,它直接从terminal读取,所以你需要像预期 :

 $ expect -c 'spawn dislocate ssh -l username -i /home/ciupicri/.ssh/id_rsa test3 expect { "(yes/no)? " { send "yes\n" expect { "Warn" exit } } }' $ dislocate 

dislocate是Expect自带的一个命令(至less在Fedora上)。 当被问到connect? [y]时回答y connect? [y] connect? [y]

你也可以尝试使用pexpect ,一个纯Python类似于Expect的模块。 查看它的hive.pysshls.py示例。