How do I transfer files using SSH and SCP using Ruby calls?
17,202
Solution 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
Solution 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")
Related videos on Youtube
Author by
user705217
Updated on May 28, 2022Comments
-
user705217 almost 2 years
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?-
Admin about 13 yearsRe-asking the same question isn't acceptable behavior here. If you want to draw attention to your question, you will be allowed to place a bounty on it after two days. You can also edit your question to add additional information, which may make your question easier to understand and answer.
-
-
PJP about 13 yearsYou'll need to modify the samples to fit your situation. The sample code isn't handing you fish to eat, it's showing you the appropriate things to use to catch your own fish.
-
user705217 about 13 yearsHi Tin, when I run the script it shows NoMethod error..what does that mean??.....this is my script..require net/scp
-
Nick D over 5 yearsThis is a great answer but how would I do it if I wanted to use a wildcard or regex to grab multiple files like file1 file2 file3. I tried /file* in the remote path but it is looking for a file named file*. When I scp i know i can add quotes like this scp "1.1.1.1:/file*" /localDirectory.
-
PJP about 5 years@NickD: I think scp is the wrong tool if you want wildcards or regex. scp wants a path which it can recursively retrieve or an individual file. Perhaps sftp or something with more capability would help, or use a ssh session to find the files, then scp them individually..