How do I transfer multiple files using Ruby Net::SSH.start?
I would suggest using net sftp for this as it allows globing directory which is more elegant. Here:
require 'net/sftp'
Net::SFTP.start( scp_hostname, scp_username, :keys => scp_keys, :timeout => 360 ) do |sftp|
sftp.dir.glob("/remote/path", "*.log") do |file|
sftp.download!( "/remote/path/#{file.name}", "./files1/#{file.name}" )
end
end
or with ssh you can use the following trick:
Net::SSH.start( scp_hostname, scp_username, :keys => scp_keys ) do |ssh|
logfiles = ssh.exec!( 'ls /remote/path/*.log' ).split
logfiles.each { |file|
ssh.scp.download!( file, file )
}
end
Ruby: how to do scp within ssh?
Well, answering your question:
Apparently yes, you can pass an ssh session as an argument.
scp = Net::SCP.new(session)
That's actually what Net::SCP.start
does https://github.com/net-ssh/net-scp/blob/master/lib/net/scp.rb#L202
If your script runs on machineA can't you just do:
Net::SCP.upload!("machineB", "user",
"~/some.tar.gz",
"~/.",
ssh: { password: "pwd" })
How do I copy a file from one server to another?
This is how it worked
I used the net-ssh & net-scp gem as suggested by @theTinMan and i was able to copy my files.
require 'rubygems'
require 'net/ssh'
require 'net/scp'
Net::SSH.start("ip_address", "username",:password => "*********") do |session|
session.scp.download! "/home/logfiles/2-1-2012/login.xls", "/home/anil/Downloads"
end
and to a copy a entire folder
require 'rubygems'
require 'net/ssh'
require 'net/scp'
Net::SSH.start("ip_address", "username",:password => "*********") do |session|
session.scp.download!("/home/logfiles/2-1-2012", "/home/anil/Downloads", :recursive => true)
end
Ruby net/scp, upload file from variable
While it will interpret a string as a file name, it should recognise a StringIO object as actual data to upload.
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)
Related Topics
Rails 4 Strong Parameters Without Required Parameters
Rails Nested With_Option :If Used in Validation
Active Admin: Sorting on Multiple Columns
Why Doesn't Module.Method_Defined(:Method) Work Correctly
Why Are Metaclasses Created in Ruby
Activerecord Association Select Counts for Included Records
How to Do Sti and Still Use Polymorphic Path Helpers
Programmatically Getting Full Ruby Version
Installing Native Ruby Extensions on Windows for Jekyll
Rails Return JSON Serialized Attribute With_Indifferent_Access
How to Pass <Arguments> to Irb If I Don't Specify <Programfile>
Making a Soap Request by Using Xml in Rails
Stubbing Chained Methods with Rspec
Consuming Non-Rest APIs in Rails with Activeresource