Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

scp

ruby

key

I have a small problem here: I try to upload a file using SCP and Ruby to a server using a private key. The code looks like:

  def transfer_file(source_file, destination_file)
     $log.info("ScpDP: Key=#{@key}")
     Net::SCP.start(@host, @userName, :keys => @key ) do |scp|
       scp.upload!(source_file,@folder + destination_file, :ssh => @key)
     end
  end

However there is some problem, and not with the private key since we use it for daily purposes, and I get the following log error:

I, [2010-08-24T11:21:27.247847 #14310]  INFO -- : ScpDP: Key=/home/myself/.ssh/id_rsa
I, [2010-08-24T11:21:27.397971 #14310]  INFO -- : SCP did not finish successfully (1)   (Net::SCP::Error)
/usr/lib/ruby/gems/1.8/gems/net-scp-1.0.2/lib/net/scp.rb:351:in `start_command'
/usr/lib/ruby/gems/1.8/gems/net-ssh-2.0.11/lib/net/ssh/connection/channel.rb:585:in `call'
/usr/lib/ruby/gems/1.8/gems/net-ssh-2.0.11/lib/net/ssh/connection/channel.rb:585:in `do_close'
/usr/lib/ruby/gems/1.8/gems/net-ssh-2.0.11/lib/net/ssh/connection/session.rb:575:in `channel_close'
/usr/lib/ruby/gems/1.8/gems/net-ssh-2.0.11/lib/net/ssh/connection/session.rb:455:in `send'
/usr/lib/ruby/gems/1.8/gems/net-ssh-2.0.11/lib/net/ssh/connection/session.rb:455:in `dispatch_incoming_packets'
/usr/lib/ruby/gems/1.8/gems/net-ssh-2.0.11/lib/net/ssh/connection/session.rb:212:in `preprocess'
/usr/lib/ruby/gems/1.8/gems/net-ssh-2.0.11/lib/net/ssh/connection/session.rb:196:in `process'
/usr/lib/ruby/gems/1.8/gems/net-ssh-2.0.11/lib/net/ssh/connection/session.rb:160:in `loop'
/usr/lib/ruby/gems/1.8/gems/net-ssh-2.0.11/lib/net/ssh/connection/session.rb:160:in `loop_forever'
/usr/lib/ruby/gems/1.8/gems/net-ssh-2.0.11/lib/net/ssh/connection/session.rb:160:in `loop'
/usr/lib/ruby/gems/1.8/gems/net-ssh-2.0.11/lib/net/ssh/connection/session.rb:109:in `close'
/usr/lib/ruby/gems/1.8/gems/net-scp-1.0.2/lib/net/scp.rb:204:in `start'
/home/myself/work/server.rb:458:in `transfer_file'

Can you please point out what might be wrong here? I have quite limited Ruby experience at this stage.

like image 213
Ferenc Deak Avatar asked Aug 24 '10 09:08

Ferenc Deak


2 Answers

Seems that this is now possible. According to the net-scp docs, you can use a Net::SSH session to perform scp commands. Combined with this answer about using authenticating with a private key within Ruby:

require 'net/ssh'
require 'net/scp'

ssh_private_keys = ['ssh-rsa AAAAB3NzaC1yc2EAAA', 'ssh-rsa AAAAB3NzaC1yc2EAAA']
Net::SSH.start(hostname, username, key_data: ssh_private_keys, keys_only: true) do |ssh|
  ssh.scp.upload!(source_file, destination_file)
end
like image 130
Robin Daugherty Avatar answered Oct 07 '22 14:10

Robin Daugherty


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)

like image 38
Jed Schneider Avatar answered Oct 07 '22 14:10

Jed Schneider