3

I want to copy all files from remote host /root/files/*.logto my current directory - ./files1/

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

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

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

ssh.scp.download!( '/root/files/myfile.log', './files1/' )

Can anyone help?

Thanks!

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

Comments

1

You could also use without any ruby gem. Using scp command.

Basic syntax of SCP

scp source_file_name username@destination_host:destination_folder

Or

scp username@remote:/file/to/send /where/to/put

Edit Update

Just remove timeout option will help you! and use :recursive => true option.

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

Net::SSH.start( scp_hostname, scp_username, :keys => scp_keys ) do |ssh|
    ssh.scp.download!( '/root/files/*.log', './files1/', :recursive => true )
    ssh.exec!( .. )
    ssh.exec!( .. )
end

Hope this help you !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.