Skip to content

Instantly share code, notes, and snippets.

@gorsuch
Created February 16, 2012 04:10
Show Gist options
  • Save gorsuch/1841901 to your computer and use it in GitHub Desktop.
Save gorsuch/1841901 to your computer and use it in GitHub Desktop.
ruby remote ssh cmd example
require 'net/ssh'
server = 'mem.sysarcana.com'
user = 'root'
pass = 'password'
command = 'uptime'
Net::SSH.start(server, user, :password => pass) do |session|
stdout = ''
stderr = ''
exit_code = nil
session.open_channel do |channel|
channel.on_data { |c, d| stdout += d }
channel.on_extended_data { |c, d| stderr += d }
channel.on_request('exit-status') { |c, d| exit_code = d.read_long }
channel.exec(command)
end
session.loop
p [ stdout, stderr, exit_code ]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment