1. Home
  2. Computing & Technology
  3. Ruby

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

By Amanda & Michael Morin, About.com

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 )
  end
end
Explore Ruby
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Ruby
  4. Networking
  5. SSH
  6. Net:SSH--Transferring Files with Net::SCP

©2009 About.com, a part of The New York Times Company.

All rights reserved.