At last! Net::SSH 2.0 is available! Also available are Net::SFTP 2.0, Net::SCP 1.0, Net::SSH::Gateway 1.0, and Net::SSH::Multi 1.0.
1 2 3 4 5 |
$ gem install net-ssh \ net-sftp \ net-scp \ net-ssh-gateway \ net-ssh-multi |
All of these Ruby libraries are for communicating with remote servers via the SSH protocol in different ways.
Net::SSH and Net::SFTP are both significant upgrades from their previous incarnations; if you have used either library in the past, you’ll want to read the documentation (Net::SSH, Net::SFTP). The Net::SSH API is still fairly similar to the way it was before, but the Net::SFTP API is entirely different.
All have pretty complete RDoc documentation, so you should be able to employ “ri” to good effect to find your way around the libraries. (Try “ri Net::SSH”, for example, to get started.)
Per-library synopses follow.
Net::SSH 2.0
This is a significant upgrade from Net::SSH 1.x. Changes from 1.x include (but are not limited to):
1 2 3 4 5 6 |
require 'net/ssh' Net::SSH.start('localhost', 'jamis') do |ssh| ssh.exec('hostname') # prints the results to $stdout ssh.loop end |
This is a complete rewrite of the original Net::SFTP 1.x code, and shares very, very little in common with it. The new version has a much cleaner implementation and API, and provides some really handy methods for transferring files and directories.
1 2 3 4 5 6 7 8 9 10 |
require 'net/sftp' Net::SFTP.start('localhost', 'jamis') do |sftp| sftp.upload! "/local/file", "/remote/file" sftp.download! "/remote/file", "/local/file" sftp.file.open("/remote/file", "w") do |file| file.puts "here is some data" end end |
This provides a way to transfer files and directories via the SCP protocol, over Net::SSH.
1 2 3 4 5 6 |
require 'net/scp' Net::SCP.start('localhost', 'jamis') do |scp| scp.upload! "/local/file", "/remote/file" scp.download! "/remote/file", "/local/file" end |
This library makes it easy to tunnel connections though firewalls. You simply connect to the gateway machine, and then specify which ports you want forwarded.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
require 'net/ssh/gateway' gateway = Net::SSH::Gateway.new('host', 'user') gateway.ssh("host.private", "user") do |ssh| puts ssh.exec!("hostname") end gateway.open("host.private", 80) do |port| require 'net/http' Net::HTTP.get_print("127.0.0.1", "/path", port) end gateway.shutdown! |
== Net::SSH::Multi 1.0
This library makes it simple to open multiple Net::SSH connections and tie them all together, running commands in parallel (much like Capistrano does).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
require 'net/ssh/multi' Net::SSH::Multi.start do |session| # access servers via a gateway session.via 'gateway', 'gateway-user' # define the servers we want to use session.use 'user1@host1' session.use 'user2@host2' # define servers in groups for more granular access session.group :app do session.use 'user@app1' session.use 'user@app2' end # execute commands on all servers session.exec "uptime" # execute commands on a subset of servers session.with(:app).exec "hostname" # run the aggregated event loop session.loop end |
Enjoy!
No comments yet.
You must be logged in to add your own comment.