Ruby 中使用 net-sftp 进行基于密钥的身份验证

发布于 2024-08-10 00:37:19 字数 546 浏览 2 评论 0原文

我希望能够使用 SFTP 登录多个服务器并下载某些文件,以帮助在问题出现时进行调试。虽然我们可以使用客户端,但我们希望开始自动化流程以简化一切。

我的第一次尝试看起来像这样:

def download(files_to_download, destination_directory)
    Net::SFTP.start(@server, @username, :password => @password) do |sftp|
        files_to_download.each do |f|
            local_path = File.join(destination_directory, File.basename(f))
            sftp.download!(f, local_path)
        end
    end
end

虽然这有效,但这意味着我们需要密码。理想情况下,我想使用公钥身份验证,但是我在文档或在线中看不到任何对此的引用 - 这可能吗?

我不想使用 chilkat。

谢谢

I want to be able to use SFTP to login into a number of servers and download certain files to help debug issues as and when they arise. While we could use a client, we wanted to start automating the process to streamline everything.

My first attempt looks something like this:

def download(files_to_download, destination_directory)
    Net::SFTP.start(@server, @username, :password => @password) do |sftp|
        files_to_download.each do |f|
            local_path = File.join(destination_directory, File.basename(f))
            sftp.download!(f, local_path)
        end
    end
end

While this works, it means we need the password. Ideally, I want to be using public key authentication however I can't see any reference to this in the documentation or online - is this possible?

I would prefer not to use chilkat.

Thanks

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

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

发布评论

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

评论(2

心不设防 2024-08-17 00:37:19

如果您想直接指定密钥(或其他 SSH 选项) 你可以先 打开一个 Net:: SSH 连接,然后从那里执行 SFTP 操作。

Net::SSH.start("localhost", "user", keys: ['keys/my_key']) do |ssh|
  ssh.sftp.upload!("/local/file.tgz", "/remote/file.tgz")
  ssh.exec! "cd /some/path && tar xf /remote/file.tgz && rm /remote/file.tgz"
end

这也适用于 Net::SCP

Net::SSH.start('localhost', 'user', keys: ['keys/my_key'] ) do |ssh|
  ssh.scp.download("/local/file.txt", "/remote/file.txt")
end

If you want to directly specify the key (or other SSH options) you can first open a Net::SSH connection, and then do SFTP operations from there.

Net::SSH.start("localhost", "user", keys: ['keys/my_key']) do |ssh|
  ssh.sftp.upload!("/local/file.tgz", "/remote/file.tgz")
  ssh.exec! "cd /some/path && tar xf /remote/file.tgz && rm /remote/file.tgz"
end

This also works for Net::SCP

Net::SSH.start('localhost', 'user', keys: ['keys/my_key'] ) do |ssh|
  ssh.scp.download("/local/file.txt", "/remote/file.txt")
end
恬淡成诗 2024-08-17 00:37:19

这是自动完成的,只需上传您的公钥即可开箱即用。

使用公钥/私钥进行连接

在显式密码身份验证之前始终会尝试公钥/私钥,
即使您提供密码。因此,如果您只想使用公钥/私钥
身份验证,只需从参数列表中删除密码即可。
如果您可以成功获取会话句柄,那么您的密钥设置正确!

It's automatically done, just upload your public key and should work out of the box.

Connecting using public/private keys

Public/private keys are always tried before the explicit password authentication,
even if you provide a password. Thus, if you only want to use public/private key
authentication, simply remove the password from the argument list.
If you can successfully obtain a session handle, then your keys are set up correctly!

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