Created
March 23, 2012 05:40
-
-
Save cantremember/2167314 to your computer and use it in GitHub Desktop.
Net::SSH::Channel observer
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
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