Ruby NET::SSH - Unable to connect to remote server

1.2k views Asked by At

I've been trying for days to connect my application to a remote server with the NET::SSH gem without success. 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)

I've seen many examples of this error with No start line appended however have not found anything to suggest workarounds to this. I've even uninstalled and reinstalled the 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.

Any help appreciated

3

There are 3 answers

0
kaybee99 On BEST ANSWER

The issue for me was the format of the private key. For some (still unexplained) reason Ruby didn't like the .ppk extension.

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. Give it a folder in which to put both keys.

  2. Add public key to ~/.ssh/authorized_keys file

  3. Move private key to local machine with WinSCP and use it as key or key_data for NET::SSH

2
Jeevan Dongre On

It can be problem of SSLv3. Re-generate your ssl certificates and try again.

0
kbrock On

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. 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