rubysshscpnet-ssh

How to copy files using Net::SSH in Ruby?


So far as I can tell I need to use both Net::SCP and Net::SSH if I want to copy a file to a remote host and then manipulate it from the command line.

It would be nice to set up one SSH session, do the copy and then use the same connection to unpack the file and install them.

Am I missing something?


Solution

  • Net::SCP allows you to easily grab a Net::SCP reference from an existing Net::SSH session:

    require "net/ssh"
    require "net/scp"
    Net::SSH.start("remote.host", "username", :password => "passwd") do |ssh|
      ssh.scp.upload("/local/path", "/remote/path")
      ssh.exec("...insert commands...")
    end
    

    Read more here: http://net-ssh.github.io/net-scp/classes/Net/SCP.html