如何使用Ruby调用使用SSH和SCP传输文件?

时间:2011-04-13 03:34:56

标签: ruby-on-rails ruby rubygems

我在目录usr/share/ruby.rb中有一个文件。我想使用SSH和SCP使用Ruby调用将该文件传输到基于IP的远程设备。任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:21)

示例:

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

还有scp.upload

答案 1 :(得分:15)

Net::SSH库包含Net::SCP,因此您应该开始查看。

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