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


2 of 6

Using Net::SCP Independently of Net::SSH

Though Net::SCP makes heavy use of Net::SSH, it has an interface to use it without knowing how to use Net::SSH. This interface is best used if you only need to transfer files, not to run any commands or forward ports on the remote machine. If you already have an SSH connection open in your program, you should use the second method below.

Net::SCP is used in much the same way Net::SSH is. Simply call Net::SCP.start with the hostname, username and password, then use the upload and download methods to upload and download files.

Remember that when uploading or downloading files via SSH, the current working directory on the remote machine is always your home directory. Pathnames will either need to be relative to your home directory, or absolute paths. If you simply wish to upload a file to your home directory on the remote host, use a pathname of '.'.

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

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

Net::SCP.start( hostname, username, :password => password ) do|scp|
  scp.upload!( 'scp1.rb', '.' )
  scp.download!( 'test.txt', '.' )

As with the Net::SSH methods, the methods ending with the exclamation point will wait until the task finishes before returning.

  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.