Skip to content

Instantly share code, notes, and snippets.

@synsa
Forked from gorsuch/sshcmd.rb
Created January 26, 2022 05:28
Show Gist options
  • Save synsa/6b4f8292496b5cbc4313f55f941ef450 to your computer and use it in GitHub Desktop.
Save synsa/6b4f8292496b5cbc4313f55f941ef450 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