通过纯bash将密码传递给ssh [英] Pass a password to ssh in pure bash

查看:78
本文介绍了通过纯bash将密码传递给ssh的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用bash脚本将密码传递给ssh(是的,我知道我可以使用ssh密钥,但这不是我想要的).

I want to pass a password to ssh using a bash script (Yes, I know that there are ssh keys that I could use, but this is not what I intend).

我找到了一些使用expect的解决方案,但是由于它不是标准的bash工具,所以我想知道是否可以使用管道来做到这一点.

I found some solutions that were using expect but since it is not a standard bash tool I am wondering if I can do this using pipes.

有人可以向我解释为什么这样的事情吗?

Can someone explain to me, why exactly something like this:

echo "password\n" | ssh somehost.com

ssh somehost.com <(echo "password\n")

不起作用?有可能使其工作吗?也许将ssh作为另一个进程执行,获取其PID,然后直接向其发送字符串?

doesn't work? Is there any possibility to make it work? Maybe executing ssh as a different process, obtaining its PID and then sending a string directly to it?

推荐答案

由于我的问题没有确切答案,因此我进行了调查,研究了为什么在其他解决方案有效时我的代码不起作用,并决定发布我发现要完成的主题.
事实证明:

Since there were no exact answers to my question, I made some investigation why my code doesn't work when there are other solutions that works, and decided to post what I found to complete the subject.
As it turns out:

"ssh使用直接TTY访问来确保密码确实是 由交互式键盘用户发出." sshpass联机帮助页

"ssh uses direct TTY access to make sure that the password is indeed issued by an interactive keyboard user." sshpass manpage

回答了这个问题,为什么管道在这种情况下不起作用.显而易见的解决方案是创建条件,以便ssh认为"它可以在常规终端中运行,并且由于可以通过简单的posix函数来实现,因此这超出了简单的bash所提供的功能.

which answers the question, why the pipes don't work in this case. The obvious solution was to create conditions so that ssh "thought" that it is run in the regular terminal and since it may be accomplished by simple posix functions, it is beyond what simple bash offers.

这篇关于通过纯bash将密码传递给ssh的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆