def exec(command, &block)
open_channel do |channel|
channel.exec(command) do |ch, success|
raise "could not execute command: #{command.inspect} (#{ch[:host]})" unless success
channel.on_data do |ch, data|
if block
block.call(ch, :stdout, data)
else
data.chomp.each_line do |line|
$stdout.puts("[#{ch[:host]}] #{line}")
end
end
end
channel.on_extended_data do |ch, type, data|
if block
block.call(ch, :stderr, data)
else
data.chomp.each_line do |line|
$stderr.puts("[#{ch[:host]}] #{line}")
end
end
end
channel.on_request("exit-status") do |ch, data|
ch[:exit_status] = data.read_long
end
end
end
end