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 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 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_close { print "\n" } end end channel.wait end rescue Errno::ECONNREFUSED => e retry if (retries += 1) < 10 end end end