Class: LearnOpen::Adapters::SshAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/learn_open/adapters/ssh_adapter.rb

Defined Under Namespace

Classes: UnknownError

Constant Summary collapse

SSH_AUTH_SUCCESS_EXIT_STATUS =
1
SSH_AUTH_FAILURE_EXIT_STATUS =
255

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user:, hostname:) ⇒ SshAdapter

Returns a new instance of SshAdapter.



9
10
11
12
# File 'lib/learn_open/adapters/ssh_adapter.rb', line 9

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

Instance Attribute Details

#hostnameObject (readonly)

Returns the value of attribute hostname.



4
5
6
# File 'lib/learn_open/adapters/ssh_adapter.rb', line 4

def hostname
  @hostname
end

#userObject (readonly)

Returns the value of attribute user.



4
5
6
# File 'lib/learn_open/adapters/ssh_adapter.rb', line 4

def user
  @user
end

Instance Method Details

#authenticated?Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/learn_open/adapters/ssh_adapter.rb', line 22

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

#public_keyObject



14
15
16
# File 'lib/learn_open/adapters/ssh_adapter.rb', line 14

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

#unauthenticated?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/learn_open/adapters/ssh_adapter.rb', line 18

def unauthenticated?
  !authenticated?
end