views:

318

answers:

1

Hello,

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

A: 

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!

knoopx
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': undefined method `shutdown!' for nil:NilClass (NoMethodError) from C:/sourcecode/log_downloader/sftp.rb:7:in `download' from C:/sourcecode/log_downloader/sftp.rb:24From 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?
Ben Hall