使用 Ruby 和 net-ssh,如何使用 Net::SSH.start 的 key_data 参数进行身份验证?

发布于 2024-11-27 12:54:39 字数 285 浏览 2 评论 0原文

我已经阅读了 net-ssh 文档,但仍然感到困惑。我可以手动进行身份验证(使用 ssh -i ...),也可以将密钥放入文件中并使用 :keys 参数。但是,我不想使用 :keys 参数,我想使用 :key_data 参数。谁能举一个可行的例子吗?由于某种原因,直接将字符串输入 :key_data 不起作用,并且它会给出错误:“既没有 PUB key 也没有 PRIV key:: 嵌套 asn1 错误”。当然,我用 google 搜索了一下,它基本上告诉我要确保密钥是 PEM 格式。当然是这样。有什么想法吗?如果需要的话我可以提供更详细的信息...

I've read the net-ssh documentation, and I am still perplexed. I can authenticate manually (using ssh -i ...), and also by placing the key in a file and using the :keys parameter. However, I dont want to use the :keys parameter, I want to use the :key_data parameter. Can anyone give a working example? For some reason, directly feeding a string into :key_data is not working, and it gives the error: "Neither PUB key nor PRIV key:: nested asn1 error". Of course I googled that, and it basically tells me to make sure the key is in PEM format. And, of course it is. Any ideas? I can provide more detailed info if needed...

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

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

发布评论

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

评论(2

归途 2024-12-04 12:54:39

我看到这个问题已经很老了,但无论如何我都会把答案告诉你,以防万一我遇到了同样的问题并且我刚刚解决了它。

请注意,在以下代码中,包含 RSA 密钥的字符串在任何地方都没有缩进。密钥的第二行中没有任何前导空格。当我粘贴密钥时,TextMate 将其放在那里。我将其删除,它就像一个魅力。

#!/usr/bin/env ruby
require 'rubygems'
require 'net/ssh'

HOST = '172.20.0.31'
USER = 'root'

KEYS = [ "-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAqccvUza8FCinI4X8HSiXwIqQN6TGvcNBJnjPqGJxlstq1IfU
kFa3S9eJl+CBkyjfvJ5ggdLN0S2EuGWwc/bdE3LKOWX8F15tFP0=
-----END RSA PRIVATE KEY-----" ]

Net::SSH.start( HOST, USER, :key_data => KEYS, :keys_only => TRUE) do|ssh|
result = ssh.exec!('ls')
puts result
end

I see this question in pretty old but I am going to throw the answer to you anyway just in case as I had the same issue and I just solved it.

In the following code note that the string containing the RSA key is not indented at all anywhere. The second line of the key does not have any leading space in it. TextMate put this there when I pasted the key in. I removed it and it worked like a charm.

#!/usr/bin/env ruby
require 'rubygems'
require 'net/ssh'

HOST = '172.20.0.31'
USER = 'root'

KEYS = [ "-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAqccvUza8FCinI4X8HSiXwIqQN6TGvcNBJnjPqGJxlstq1IfU
kFa3S9eJl+CBkyjfvJ5ggdLN0S2EuGWwc/bdE3LKOWX8F15tFP0=
-----END RSA PRIVATE KEY-----" ]

Net::SSH.start( HOST, USER, :key_data => KEYS, :keys_only => TRUE) do|ssh|
result = ssh.exec!('ls')
puts result
end
云淡月浅 2024-12-04 12:54:39

我添加了一些我在挖掘库后发现的更多信息...

从 2.9.2 开始,如果您的意图是仅使用 key_data 中提供的密钥,则还必须在加载之前指定一组空白密钥key_data,否则会加载一些默认的键。

就我而言,它尝试加载的身份文件之一受密码保护,因此它要求我提供密码,尽管我的意图根本不是使用该身份文件。

使用上面的示例,在 2.9.2 中,您可以通过执行以下操作获得相同的效果:

#!/usr/bin/env ruby
require 'rubygems'
require 'net/ssh'

HOST = '172.20.0.31'
USER = 'root'

KEYS = [ "-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAqccvUza8FCinI4X8HSiXwIqQN6TGvcNBJnjPqGJxlstq1IfU
kFa3S9eJl+CBkyjfvJ5ggdLN0S2EuGWwc/bdE3LKOWX8F15tFP0=
-----END RSA PRIVATE KEY-----" ]

Net::SSH.start( HOST, USER, :keys => [], :key_data => KEYS, :keys_only => TRUE) do|ssh|
result = ssh.exec!('ls')
puts result
end

I'm adding a little more info that I discovered myself after digging around the library...

Since 2.9.2, if your intention is to use only the key provided in key_data, you must also specify a blank set of keys before loading your key_data, or it will load some default keys.

In my case, one of those identity files it tried to load was passphrase-protected, so it asked me for my passphrase, though my intention was not to use that identify file at all.

Using the example above, in 2.9.2, you can get the same effect by doing something like this:

#!/usr/bin/env ruby
require 'rubygems'
require 'net/ssh'

HOST = '172.20.0.31'
USER = 'root'

KEYS = [ "-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAqccvUza8FCinI4X8HSiXwIqQN6TGvcNBJnjPqGJxlstq1IfU
kFa3S9eJl+CBkyjfvJ5ggdLN0S2EuGWwc/bdE3LKOWX8F15tFP0=
-----END RSA PRIVATE KEY-----" ]

Net::SSH.start( HOST, USER, :keys => [], :key_data => KEYS, :keys_only => TRUE) do|ssh|
result = ssh.exec!('ls')
puts result
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文