How do I transfer files using SSH and SCP using Ru

2019-03-12 12:12发布

问题:

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?

回答1:

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



回答2:

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")