Skip to content

Instantly share code, notes, and snippets.

@cantremember
Created March 23, 2012 05:40
Show Gist options
  • Save cantremember/2167314 to your computer and use it in GitHub Desktop.
Save cantremember/2167314 to your computer and use it in GitHub Desktop.
Net::SSH::Channel observer
def observer_struct
@observer_struct ||= Struct.new(:channel, :stdout, :stderr, :exit_status, :exit_signal)
end
def observe_channel(ch, ops={})
observer = observer_struct.new(ch, '', '', 0, nil)
is_puts = !!ops[:puts]
# stdout
ch.on_data do |ch, data|
$stdout.print data if is_puts
observer.stdout << data
end
# only handle stderr
ch.on_extended_data do |ch, type, data|
next unless [1, :stderr].include? type
$stderr.print data if is_puts
observer.stderr << data
end
ch.on_request("exit-status") do |ch, data|
observer.exit_status = data.read_long
end
ch.on_request("exit-signal") do |ch, data|
observer.exit_signal = data.read_long
end
observer
end
def open_channel(ssh, ops={}, &block)
ch = if block_given?
ssh.open_channel &block
else
ssh.open_channel
end
# somebody's watching you
self.observe_channel ch, ops
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment