简体   繁体   English

Ruby NET::SSH - 无法连接到远程服务器

[英]Ruby NET::SSH - Unable to connect to remote server

I've been trying for days to connect my application to a remote server with the NET::SSH gem without success.几天来,我一直在尝试使用 NET::SSH gem 将我的应用程序连接到远程服务器,但没有成功。 The issue seems to originate from the fact I need to use a private key file to authenticate the login.这个问题似乎源于我需要使用私钥文件来验证登录的事实。

require 'net/ssh'


def SSH(host, user, psw, keys, cmd)
  Net::SSH.start( host,
                   user,
                  :password => psw,
                  :host_key => "ssh-rsa",
                  :encryption => ["blowfish-cbc","aes256-cbc"],
                  :keys => keys,
                  :verbose => :debug,
                  :auth_methods => ["publickey","password"]
                  ) do|ssh|

    @result = ssh.exec!(cmd)
    puts @result
  end
  return @result
end

The debug output gives me the error:调试输出给了我错误:

Could not load private key file `C:/path_to_key/key.ppk': ArgumentError (Could not parse PKey)无法加载私钥文件“C:/path_to_key/key.ppk”:ArgumentError(无法解析 PKey)

I've seen many examples of this error with No start line appended however have not found anything to suggest workarounds to this.我已经看到许多此错误的示例,但没有附加No start line但是没有找到任何建议解决此问题的方法。 I've even uninstalled and reinstalled the NET::SSH gem.我什至卸载并重新安装了 NET::SSH gem。

I have no problem connecting through puTTY/WinSCP with the same credentials so I'm sure there are no remote authentication issues.我使用相同的凭据通过 puTTY/WinSCP 连接没有问题,所以我确定没有远程身份验证问题。

Any help appreciated任何帮助表示赞赏

It can be problem of SSLv3.这可能是 SSLv3 的问题。 Re-generate your ssl certificates and try again.重新生成您的 ssl 证书并重试。

The issue for me was the format of the private key.我的问题是私钥的格式。 For some (still unexplained) reason Ruby didn't like the .ppk extension.由于某些(仍然无法解释的)原因,Ruby 不喜欢.ppk扩展名。

To get round it绕过它

  1. ssh-keygen -t rsa (or dsa if you want more secure format) on the remote box to generate public/private key pair. ssh-keygen -t rsa (或 dsa,如果你想要更安全的格式)在远程盒子上生成公钥/私钥对。 Give it a folder in which to put both keys.给它一个文件夹,在其中放置两个键。

  2. Add public key to ~/.ssh/authorized_keys file将公钥添加到 ~/.ssh/authorized_keys 文件

  3. Move private key to local machine with WinSCP and use it as key or key_data for NET::SSH使用 WinSCP 将私钥移动到本地计算机,并将其用作 NET::SSH 的密钥或 key_data

My current solution is to ask the user to add the keys to the user agent:我目前的解决方案是要求用户将密钥添加到用户代理:

eval `ssh-agent -s`
ssh-add

While the above may not be known by some users, it is the standard way to let your shell know what keys to use for remote access.虽然某些用户可能不知道上述内容,但这是让您的 shell 知道用于远程访问的密钥的标准方法。 It handles typing in a password for more of a single sign on handling of credentials with passwords.它处理输入密码,以便在处理带有密码的凭据时进行更多的单点登录。

The most recent version of net ssh potentially no longer has this issue最新版本的 net ssh 可能不再有此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM