Berlin Berlin - 1 year ago 84
Ruby Question

How do I transfer multiple files using Ruby Net::SSH.start?

I want to copy all files from remote host

to my current directory -

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

Net::SSH.start( scp_hostname, scp_username, :keys => scp_keys, :timeout => 360 ) do |ssh|!( '/root/files/*.log', './files1/' )
ssh.exec!( .. )
ssh.exec!( .. )

I got an exception :

caught exception SCP did not finish successfully (1): scp: /root/files/*.log: No such file or directory

But it worked when I copied a specific file!( '/root/files/myfile.log', './files1/' )

Can anyone help?


Answer Source

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|!( "/remote/path/#{}", "./files1/#{}" )

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|!( file, file )
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download