ssh-keygen 接受标准输入
我试图使用变量通过 bash 作为输入而不是文件来调用 ssh-keygen 来获取公钥的指纹。我知道我可以使用临时文件来解决此问题,但由于超出此问题范围的原因,我不想这样做。
此方法不起作用,因为它说密钥文件无效(这肯定是正确的)。
echo $pubkey | ssh-keygen -lf /dev/stdin
此确实起作用,但不使用变量,而是使用文件。
ssh-keygen -lf alpha.pub
这确实工作,但不使用变量,而是使用重定向文件。
ssh-keygen -lf /dev/stdin < alpha.pub
这不起作用,因为我得到了一个不明确的重定向,
ssh-keygen -lf /dev/stdin < $(echo $pubkey)
我希望了解如何让 ssh-keygen 使用公钥从变量中读取数据,如果可能的话,解释一下为什么要重定向没有做我认为他们应该做的事情。具体来说,为什么 |
的行为与 <
不同,以及为什么第三个示例是不明确的重定向
。我在网上搜索,但许多重定向教程似乎没有回答我的问题。
I am trying to call ssh-keygen
using a variable through bash
as an input instead of a file to get a fingerprint of a public key. I am aware that I could use a temp file to get around this issue, but for reasons out of scope of this question, I do not want to.
This method does not work as it says the key file is invalid (it's correct for sure)
echo $pubkey | ssh-keygen -lf /dev/stdin
This does work, but is not using a variable, rather a file.
ssh-keygen -lf alpha.pub
This does work, but is not using a variable, rather a redirected file.
ssh-keygen -lf /dev/stdin < alpha.pub
This does not work because I get an ambiguous redirect
ssh-keygen -lf /dev/stdin < $(echo $pubkey)
I would appreciate some insight as to how to get ssh-keygen to read from a variable with a public key and if possible, an explanation as to why the redirects aren't doing what I think they should be doing. In specific why the |
behaves differently than the <
and why the third example is an ambiguous redirect
. I searched online but many of the redirect tutorials didn't seem to answer my questions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是使用文件
/dev/stdin
的单行代码,如其他答案中所述。请注意,这将破坏使用密码短语的私钥。它将与 AWS 或 OpenStack 生成的不使用密码短语的 pem 文件配合使用。
Here is a one liner using the file
/dev/stdin
as described in other answers.Note that this will break with private keys that use a passphrase. It will work with pem files generated by AWS or OpenStack which do not use passphrases.
如果要将字符串重定向为 stdin,请使用以下语法:
如果要将命令的输出当作文件来重定向,则可以这样做:
如果要将命令用作 OUTPUT 文件,或多或少是一样的:
If you want to redirect a string as stdin, use this syntax:
If you want to redirect the output of a command as if it was a file, you do it like this:
And if you want to use a command as an OUTPUT file, it's more or less the same:
我建议使用临时文件。问题是重定向时,BASH 需要一个文件。通过使用 $(echo $pubkey),bash 会抱怨,因为当完成替换时,它会查找替换创建的同名文件。
I would recommend using a temporary file. The issue is that redirecting, BASH expects a file. By using $(echo $pubkey), bash will complain because when it's done with the substitution, it will look for a file of that name that the substitution creates.
/dev/stdin 实际上是一个 unix 管道,而不是常规文件,因此 ssh-keygen 无法打开该文件
/dev/stdin 指的是使用 bash Heredoc 创建的常规文件。您可以验证这一点:
/dev/stdin is actually a unix pipe, not a regular file, so ssh-keygen fails to open the file
/dev/stdin refers to a regular file, created by using a bash heredoc. You can verify this:
从版本 7.2(于 2016 年 2 月 28 日发布)开始,现在可以通过传递
-
作为文件名来实现。 来自发行说明:Since version 7.2 (released on on 2016-02-28), this is now possible by passing
-
as the file name. From the release notes: