1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Net:SSH--Transferring Files with Net::SCP


3 of 6

Using Net::SCP With Net::SSH

If you already have a Net::SSH connection open, you don't need another one to transfer files. SCP works on channels just like executing commands and forwarding ports, so you don't need a second connection. The Net::SCP library adds a method called scp to Net::SSH::Connection::Session to give you an SCP object you can use. Using this object is done in the say way as the above example.

The following example will read a list of files ending in .log from the ls command, then proceed to download each of these files. Note that the only difference in the SCP portion is using ssh.scp to get the SCP Session object.

#!/usr/bin/env ruby
require 'rubygems'
require 'net/ssh'
require 'net/scp'

hostname = 'host
username = 'user'
password = 'pass'

Net::SSH.start( hostname, username, :password => password ) do|ssh|
  logfiles = ssh.exec!( 'ls *.log' ).split

  logfiles.each do|l|
    ssh.scp.download!( l, l )
  1. About.com
  2. Technology
  3. Ruby
  4. Networking
  5. SSH
  6. Net:SSH--Transferring Files with Net::SCP

©2014 About.com. All rights reserved.