Sha256: 17c304218b0ee1828e97ff6f9c0557d6434b266f4d5865082f3ed9c7164833d1

Contents?: true

Size: 1.01 KB

Versions: 6

Compression:

Stored size: 1.01 KB

Contents

module LearnOpen
  module Adapters
    class SshAdapter
      attr_reader :user, :hostname

      SSH_AUTH_SUCCESS_EXIT_STATUS = 1
      SSH_AUTH_FAILURE_EXIT_STATUS = 255

      def initialize(user:, hostname:)
        @user = user
        @hostname = hostname
      end

      def public_key
        File.read("#{ENV['HOME']}/.ssh/id_rsa.pub").chomp
      end

      def unauthenticated?
        !authenticated?
      end

      def authenticated?
        _stdout, stderr, status = LearnOpen.system_adapter.run_command_with_capture("ssh -T #{user}@#{hostname}")

        case status.exitstatus
        when SSH_AUTH_SUCCESS_EXIT_STATUS
          true
        when SSH_AUTH_FAILURE_EXIT_STATUS
          case stderr
          when /permission denied/i
            false
          else
            raise LearnOpen::Adapters::SshAdapter::UnknownError
          end
        else
          raise LearnOpen::Adapters::SshAdapter::UnknownError
        end
      end

      class UnknownError < StandardError
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
learn-open-1.2.28 lib/learn_open/adapters/ssh_adapter.rb
learn-open-1.2.27 lib/learn_open/adapters/ssh_adapter.rb
learn-open-1.2.26 lib/learn_open/adapters/ssh_adapter.rb
learn-open-1.2.24 lib/learn_open/adapters/ssh_adapter.rb
learn-open-1.2.23 lib/learn_open/adapters/ssh_adapter.rb
learn-open-1.2.22 lib/learn_open/adapters/ssh_adapter.rb