class Net::SCP

Net::SCP implements the SCP (Secure CoPy) client protocol, allowing Ruby programs to securely and programmatically transfer individual files or entire directory trees to and from remote servers. It provides support for multiple simultaneous SCP copies working in parallel over the same connection, as well as for synchronous, serial copies.

Basic usage:

require 'net/scp'

Net::SCP.start("remote.host", "username", :password => "passwd") do |scp|
  # synchronous (blocking) upload; call blocks until upload completes
  scp.upload! "/local/path", "/remote/path"

  # asynchronous upload; call returns immediately and requires SSH
  # event loop to run
  channel = scp.upload("/local/path", "/remote/path")
  channel.wait
end

Net::SCP also provides an open-uri tie-in, so you can use the Kernel#open method to open and read a remote file:

# if you just want to parse SCP URL's:
require 'uri/scp'
url = URI.parse("scp://user@remote.host/path/to/file")

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

Lastly, Net::SCP adds a method to the Net::SSH::Connection::Session class, allowing you to easily grab a Net::SCP reference from an existing Net::SSH session:

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

Net::SSH.start("remote.host", "username", :password => "passwd") do |ssh|
  ssh.scp.download! "/remote/path", "/local/path"
end

Progress Reporting

By default, uploading and downloading proceed silently, without any outward indication of their progress. For long running uploads or downloads (and especially in interactive environments) it is desirable to report to the user the progress of the current operation.

To receive progress reports for the current operation, just pass a block to upload or download (or one of their variants):

scp.upload!("/path/to/local", "/path/to/remote") do |ch, name, sent, total|
  puts "#{name}: #{sent}/#{total}"
end

Whenever a new chunk of data is recieved for or sent to a file, the callback will be invoked, indicating the name of the file (local for downloads, remote for uploads), the number of bytes that have been sent or received so far for the file, and the size of the file.