Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I transfer files using SSH and SCP using Ruby calls?

I have a file in the directory usr/share/ruby.rb. I want to transfer that file to IP-based remote devices using SSH and SCP using Ruby calls. Can anyone help me?

like image 553
user705217 Avatar asked Apr 13 '11 03:04

user705217


People also ask

Can I transfer files with SSH?

It's based on the SSH protocol used with it. A client can use an SCP to upload files to a remote server safely, download files, or even transfer files via SSH across remote servers.

Does SCP work over SSH?

The scp command uses SSH to transfer data, so it requires a password or passphrase for authentication. Unlike rcp or FTP, scp encrypts both the file and any passwords exchanged so that anyone snooping on the network cannot view them.

What is SSH and SCP?

Secure copy protocol (SCP) is a means of securely transferring computer files between a local host and a remote host or between two remote hosts. It is based on the Secure Shell (SSH) protocol. "SCP" commonly refers to both the Secure Copy Protocol and the program itself.

How do I transfer files from SCP to local server?

Copy a Local File to a Remote System with the scp Command 0.2 is the server IP address. The /remote/directory is the path to the directory you want to copy the file to. If you don't specify a remote directory, the file will be copied to the remote user home directory.


2 Answers

example:

require 'net/scp'

    host = '10.10.10.10'
    login = 'foo'
    password = 'bar'

    Net::SCP.start(host, login, :password => password) do |scp|
      puts 'SCP Started!'
      scp.download('/usr/share/ruby.rb', '.')
    end

there's also an scp.upload

like image 154
Vlad Khomich Avatar answered Sep 22 '22 01:09

Vlad Khomich


The Net::SSH library includes Net::SCP, so you should start looking there.

From the Net::SCP docs:

  require 'net/scp'

  # upload a file to a remote server
  Net::SCP.upload!("remote.host.com", "username",
    "/local/path", "/remote/path",
    :password => "password")

  # download a file from a remote server
  Net::SCP.download!("remote.host.com", "username",
    "/remote/path", "/local/path",
    :password => password)

  # download a file to an in-memory buffer
  data = Net::SCP::download!("remote.host.com", "username", "/remote/path")
like image 34
the Tin Man Avatar answered Sep 22 '22 01:09

the Tin Man