def ssh_ruby(host, username, command, compression=false, request_pty=false, &block)
debug "Opening Net::SSH connection to #{host}, #{username}, #{command}"
exit_status = 0
options = {:compression => compression}
options[:verbose] = :debug if debug?
Net::SSH.start(host, username, options) do |session|
channel = session.open_channel do |channel|
if request_pty
channel.request_pty do |ch, success|
say "pty could not be obtained" unless success
end
end
channel.exec(command) do |ch, success|
channel.on_data do |ch, data|
print data
end
channel.on_extended_data do |ch, type, data|
print data
end
channel.on_close do |ch|
debug "Terminating ... "
end
channel.on_request("exit-status") do |ch, data|
exit_status = data.read_long
end
yield channel if block_given?
channel.eof!
end
end
session.loop
end
raise RHC::SSHCommandFailed.new(exit_status) if exit_status != 0
rescue Errno::ECONNREFUSED => e
debug_error e
raise RHC::SSHConnectionRefused.new(host, username)
rescue Net::SSH::AuthenticationFailed => e
debug_error e
raise RHC::SSHAuthenticationFailed.new(host, username)
rescue SocketError => e
debug_error e
raise RHC::ConnectionFailed, "The connection to #{host} failed: #{e.message}"
end