使用expect将密码传递给ssh

发布于 2024-07-11 19:05:24 字数 447 浏览 6 评论 0原文

如何使用expect 将密码发送到ssh 连接。

假设密码是 p@ssword ssh 命令是 ssh [email protected]

我会做什么以使其输入当显示

[email protected]的密码时的密码:

使用 SSH 密钥对的正确操作不是一个选项,因为我必须使用 ssh (scp) 将密钥放在服务器上,这会要求输入密码。

How can I use expect to send a password to an ssh connection.

say the password was p@ssword
and the ssh command was
ssh [email protected]

What would I do with expect to a make it input the password when it says

[email protected]'s password:
?

The proper action of using an SSH key pair isn't an option because I would have to use ssh (scp) to put the key on the server, which would ask for a password.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

醉态萌生 2024-07-18 19:05:24

我总是使用“正确”的解决方案,但在其他情况下我使用了expect。

在这里我发现了以下建议:

#!/usr/local/bin/expect
spawn  sftp  -b cmdFile [email protected]
expect "password:"
send "shhh!\n";
interact

I always used the "proper" solution, but I used expect in other situations.

Here I found following suggestion:

#!/usr/local/bin/expect
spawn  sftp  -b cmdFile [email protected]
expect "password:"
send "shhh!\n";
interact
指尖凝香 2024-07-18 19:05:24

使用公钥身份验证并使用没有密码的密钥不是更容易吗?

当源计算机上的用户执行此操作以生成 RSA 密钥时,

ssh-keygen -t rsa

现在将 ~/.ssh/id_rsa.pub 复制到目标计算机并将其附加到目标用户的authorized_keys 文件中

Would it not be easier to use public key authentication and use a key with no passphrase?

As the user on the source machine do this to make an RSA key

ssh-keygen -t rsa

Now copy ~/.ssh/id_rsa.pub to the target machine and append it to the authorized_keys file of the target user

尐籹人 2024-07-18 19:05:24

您最快的前进方式(除非您想成为 Tcl 专家,这在 2009 年是……不寻常的……)可能是使用 autoexpect。 这是手册页:

http://expect.nist.gov/example/autoexpect.man .html

简而言之,启动 autoexpect,运行您的 ssh 会话,完成您需要做的事情,停止自动预期,然后敲击键盘敲打由此产生的混乱,直到它起作用:) I我假设你只需要快速破解即可整理好你的钥匙,然后,听起来你已经知道了分数。

还有这个问题它已经包含了一个接近您所寻求的示例。

Your quickest way forward (unless you want to become a Tcl expert, which would be... unusual... in 2009) is probably to use autoexpect. Here's the man page:

http://expect.nist.gov/example/autoexpect.man.html

In short, fire up autoexpect, run your ssh session, finish up what you need to do, stop autoexpecting and then beat your keyboard over the resulting mess until it works :) I'm assuming you don't need anything more than a quick hack to get your keys sorted out and then, well it sounds like you know the score already with that.

And there's this question which already contains an example close to what you seek.

岁月染过的梦 2024-07-18 19:05:24

Cygwin 有 autoexpect 只是不在 bin 包中。 运行 setup.exe 并搜索 Expect 并选中源复选框。 您将在 /usr/src 中看到生成的树,其中有一个 Expect/expect/examples 目录。 其中包含 autoexpect 脚本的副本。

Cygwin has autoexpect just not in the bin package. run setup.exe and search for expect and check the source checkbox. you will see the resulting tree in /usr/src and in there there is a expect/expect/examples directory. in there lives a copy of the autoexpect script.

迷雾森÷林ヴ 2024-07-18 19:05:24

密钥解决方案将不起作用...因为密钥必须只能由运行 ssh 的人读取。 在 xp 上,您无法使用正确的权限创建密钥结构。 所以 ssh 不会读取它们。 这可能已经改变,但最后我检查它仍然不起作用。

Key solution will not work... because the keys have to be readable only by the person running ssh. On xp you cannot create key structure with the correct permissions. So ssh will not read them. This may have changed, but last i checked it still not not work.

2024-07-18 19:05:24

我很确定不可能做你想做的事。 大多数提示输入密码的 *nix 应用程序直接从 TTY 读取,而不是从 stdin 读取,因此您无法通过管道输入密码。正如其他人提到的,您可以将 SSH 配置为不提示输入密码,如所解释的 此处

在我无缘无故地被否决后,我对 expect 命令做了更多研究,发现它有一个 send_tty 命令发送到 /dev/tty而不是标准输入,它实际上可能会做你想做的事......我以前不知道这个功能。 不过,我仍然建议将密钥放在服务器上。

I'm pretty sure it is not possible to do what you're trying to do. Most *nix applications that prompt for a password read from the TTY directly, not stdin, so you can't pipe the password in. You can, as others have mentioned, configure SSH to not prompt for a password, as explained here.

After I was downvoted for no apparent reason, I went and did a little more research on the expect command and discovered that it has a send_tty command that sends to /dev/tty instead of stdin, which might actually do what you want... I was previously unaware of this feature. I still recommend putting the key on the server, however.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文