Sha256: b561bcdef5f2d23c420892f88457ec18a9995aabb77bc2e5a2c891d99759ecec

Contents?: true

Size: 994 Bytes

Versions: 1

Compression:

Stored size: 994 Bytes

Contents

module Quickmox

  class SSHTransport

    class SSHTransportError < StandardError
    end

    attr_accessor :session, :host, :user, :pass

    def initialize(host, user, pass)
      @host = host
      @user = user
      @pass = pass
    end

    def connect
      handle_exceptions do
        @session = Net::SSH.start(host,
                                  user,
                                  password: pass,
                                  auth_methods: %w(password),
                                  number_of_password_prompts: 0,
                                  timeout: 3)
      end
      self
    end

    def close
      handle_exceptions do
        session.close
      end
    end

    def exec!(cmd)
      handle_exceptions do
        session.exec!(cmd).chomp
      end

    end

    private

    def handle_exceptions
      begin
        yield
      rescue => e
        raise SSHTransportError, "Exception while talking to host #{host}: #{e}"
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
quickmox-0.2.2 lib/quickmox/ssh_transport.rb