Key based authenication with net-sftp in Ruby
Asked Answered
P

2

7

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

Phane answered 9/11, 2009 at 13:7 Comment(0)
W
12

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
Walston answered 23/9, 2014 at 22:24 Comment(2)
Thanks but We're getting below error when I tried your above sysntax, do you know the solution of it? SCP did not finish successfully (1): (Net::SCP::Error)Radiophone
There are a lot of possible things which could produce an error like that. You might be best off posting a new question, with the rest of the error message and the code you ran.Walston
M
8

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!

Marshallmarshallese answered 9/11, 2009 at 13:48 Comment(3)
Awesome! Thank you. I tried that and I'm now getting: c:/ruby/lib/ruby/gems/1.8/gems/net-sftp-2.0.2/lib/net/sftp.rb:43:in start': und efined method shutdown!' for nil:NilClass (NoMethodError) from C:/sourcecode/log_downloader/sftp.rb:7:in `download' from C:/sourcecode/log_downloader/sftp.rb:24 From the SSH logs, it looks like it doesn't do the Accepting public key, requesting signature step which winscp did where it asked me to accept the key? Or does the client take care of that for me?Phane
Does this work with Net::SFTP as well? or is it only with Net::SSH ?Bahuvrihi
Yes, this works with Net::SFTP as well. And if you're using Net::SSH/SFTP v2, you can pass the private key into .start as the :key_data option, if saving it to a file is not a good option for you.Suricate

© 2022 - 2024 — McMap. All rights reserved.