Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Test: SSH Connection with multiple execs

View gist:3018233
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#!/usr/bin/env ruby
$VERBOSE=nil
require 'rubygems'
require 'net/ssh'
require 'logger'
logger=Logger.new(STDOUT)
 
Net::SSH.start( 'localhost', `whoami`.chomp) do |ssh|
rc, stdout, stderr = 0, "", ""
last_out, last_err = "", ""
ssh.open_channel do |channel| # open the SSH channel
channel.on_request("exit-status") do |ch, data| # handle the return code
logger.info last_out unless last_out == ""
logger.warn last_err unless last_err == ""
rc = data.read_long.to_i
ch.close # process finished, close the channel
end
channel.on_data do |ch, data| # handle STDOUT output
stdout << data
last_out << data
logger.debug(data)
if data[-1]=="\n"
logger.info(last_out.chomp) unless block_given?
last_out = ""
end
end
channel.on_extended_data do |ch, type, data| # handle STDERR output
options[:stderr_to_stdout] ? stdout << data : stderr << data # store error where appropriate
last_err << data
if data[-1]=="\n"
logger.warn(last_err.chomp) unless block_given?
last_err = ""
end
end
channel.exec "var='foo'; echo Value: $var"
channel.exec "echo Value: $var"
ssh.loop # wait for the SSH channel to close
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.