module Meroku class Tunnel attr_accessor :ip, :username, :keys, :verify_host_key, :verbose def initialize(ip:,username:,keys:,verify_host_key:,verbose:) @ip = ip @username = username @keys = keys @verify_host_key = verify_host_key @verbose = verbose end def run(cmd) @verbose=true retries ||= 0 exit_code=nil Net::SSH.start(@ip, @username, password: 'password', keys: @keys, verify_host_key: @verify_host_key, timeout: 90) do |ssh| channel = ssh.open_channel do |ch| STDERR.print "#{cmd}\n" ch.exec cmd do |ch, success| raise "could not execute command" unless success ch.on_data do |c, data| if @verbose $stdout.print data else $stdout.print "." end end ch.on_extended_data do |c, type, data| if @verbose $stderr.print data else $stderr.print "." end end ch.on_request("exit-status") do |ch, data| exit_code = data.read_long end ch.on_close { print "\n" } end end channel.wait end fail "Ssh command returned non-zero" if status != 0 rescue Errno::ECONNREFUSED => e retry if (retries += 1) < 10 end end end