How to copy a file from one server to another?

I have one server that has nothing but xls log files. Each file has a size of 5-15 MB, and it is dynamic in the sense that files are added at any given time. Now I need a way to execute the following process using Ruby.

  • Copy the file by sending the file name from one server that has nothing but log files to another server.
  • I need to pass the server password as an argument.
  • Everything happens in the background, which starts with a Ruby script.
+5
source share
4 answers

Here is how it works

I used the cheat net-ssh and net-scp, 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 copy the 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
+4
source

Net:: SCP Net:: SSH . , , . Net:: SSH, ssh.exec! .

Net:: SCP docs:

Net:: SCP SCP (Secure CoPy), Ruby . SCP, , .

Net:: SCP uri, Kernel # :

  # if you want to read from a URL voa SCP:
  require 'uri/open-scp'
  puts open("scp://user@remote.host/path/to/file").read

Net:: SSH docs:

require 'net/ssh'

Net::SSH.start('host', 'user', :password => "password") do |ssh|
  # capture all stderr and stdout output from a remote process
  output = ssh.exec!("hostname")

end , . output , .

Ruby , , , Ruby , , scp .

Net:: SCP Net:: SSH Net:: SFTP, , , SFTP . Net::SFTP::Operations::Dir Net::SFTP::Operations::Download .

rsync , @tadman. , .


?

rsync, . . , " ssh Ruby " " ruby ​​ssh intro".

Melding @tadman rsync Ruby, rsync ( Ruby)".

+10

You should probably just use it rsyncinstead of rolling your own thing. Use sshwith access to the public / private key and you will avoid the password. Using a password in general is probably a bad idea.

+1
source

It seems to be possible without scp

@session = Net::SSH.start(@ftpIp, @ftpUser, :password => @ftpPass)
@conn = Net::SFTP::Session.new(@session).connect!

def download_remote_folder(remote_path, local_path, use_ssh = @use_ssh)
    @conn ||= connect
    if (use_ssh)
      @conn.download!(remote_path,local_path,:recursive => true)
    else
      @conn.get(remote_path, local_path)
    end
  end

  def download_remote_file(remote_path, local_path, use_ssh = @use_ssh)
    @conn ||= connect
    if (use_ssh)
      @conn.download!(remote_path,local_path)
    else
      @conn.get_file(remote_path, local_path)
    end
  end
0
source

All Articles