-
-
Save synsa/6b4f8292496b5cbc4313f55f941ef450 to your computer and use it in GitHub Desktop.
ruby remote ssh cmd example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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