Using Ruby and Net-Ssh, How to Authenticate Using the Key_Data Parameter with Net::Ssh.Start

Public/Private key authentication for Ruby Net::SFTP

Net::SFTP.start passes its options hash directly to Net::SSH.start, so we should look to its documentation. It lists three options that look relevant:

  • :keys => an array of file names of private keys to use for publickey and hostbased authentication
  • :key_data => an array of strings, with each element of the array being a raw private key in PEM format.
  • :keys_only => set to true to use only private keys from keys and key_data parameters, even if ssh-agent offers more identities. This option is intended for situations where ssh-agent offers many different identites.

The answer to a related question suggests that you may need to use all three:

Net::SFTP.start(ftp_host, user,
key_data: [],
keys: "tmp/some-certs/privatekey.pem",
keys_only: true)

If you want to use the raw key data from the SOME_PRIVATE_KEY environment variable instead, it ought to look like this:

Net::SFTP.start(ftp_host, user,
key_data: [ ENV["SOME_PRIVATE_KEY"] ],
keys: [],
keys_only: true)

How do I do SCP with Ruby and a private key?

a brief look at this documentation suggests that it doesn't accept an ssh key option, as you are passing. But assuming you are right and I am wrong on that portion,

without seeing what value you are passing to transfer_file and what is stored in @folder, i can only guess, but assuming that they are both file objects, you can't concatenate the objects. you have to grab their path attributes. you might want to log the value of those two variables to make sure you are getting a path. you may also have better luck using the ruby "#{}" method to concat string arguments, again guessing here but

path = "#{@folder.path}/#{destination_file.path}" #=> "my_folder/destination_folder

and

scp.upload!(source_file,path, :ssh => @key)

Ruby ssh error Net::SSH::AuthenticationFailed while it works with PHP

I've found how to solve that problem: adding a :auth_methods => ['publickey','password'] parameter in the Net::SSH.start function.



Related Topics



Leave a reply



Submit