Class: Spectre::SSH::SshConnection

Inherits:
Object
  • Object
show all
Includes:
Delegate
Defined in:
lib/spectre/ssh.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, username, opts, logger) ⇒ SshConnection

Returns a new instance of SshConnection.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/spectre/ssh.rb', line 17

def initialize host, username, opts, logger
  opts[:non_interactive] = true

  @logger = logger
  @host = host
  @username = username
  @opts = opts
  @session = nil
  @exit_code = nil
  @output = ''
end

Instance Attribute Details

#exit_codeObject (readonly)

Returns the value of attribute exit_code.



15
16
17
# File 'lib/spectre/ssh.rb', line 15

def exit_code
  @exit_code
end

#outputObject (readonly)

Returns the value of attribute output.



15
16
17
# File 'lib/spectre/ssh.rb', line 15

def output
  @output
end

Instance Method Details

#can_connect?Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/spectre/ssh.rb', line 76

def can_connect?
  @output = nil

  begin
    connect!
    @session.open_channel.close
    @output = "successfully connected to #{@host} with user #{@username}"
    @exit_code = 0
    return true
  rescue StandardError => e
    @logger.error e.message
    @output = "unable to connect to #{@host} with user #{@username}"
    @exit_code = 1
  end

  false
end

#closeObject



70
71
72
73
74
# File 'lib/spectre/ssh.rb', line 70

def close
  return unless @session and !@session.closed?

  @session.close
end

#connect!Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/spectre/ssh.rb', line 57

def connect!
  return unless @session.nil? or @session.closed?

  begin
    @session = Net::SSH.start(@host, @username, @opts)
  rescue SocketError
    raise SshError, "Unable to connect to #{@host} with user #{@username}"
  rescue Net::SSH::AuthenticationFailed
    raise SshError, "Authentication failed for #{@username}@#{@host}. \
      Please check password, SSH keys and passphrases."
  end
end

#exec(command, log: true) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/spectre/ssh.rb', line 94

def exec command, log: true
  connect!

  log_str = "ssh #{@username}@#{@session.host} -p #{@session.options[:port]} #{command}"

  @session.open_channel do |channel|
    channel.exec(command) do |_ch, success|
      abort "could not execute #{command} on #{@session.host}" unless success

      @output = ''

      channel.on_data do |_ch, data|
        @output += data
      end

      channel.on_extended_data do |_ch, _type, data|
        @output += data
      end

      channel.on_request('exit-status') do |_ch, data|
        @exit_code = data.read_long
      end

      channel.on_request('exit-signal') do |_ch, data|
        @exit_code = data.read_long
      end
    end
  end.wait

  @session.loop

  log_str += "\n" + @output
  @logger.info(log_str) if log
end

#file_exists(path) ⇒ Object



47
48
49
50
# File 'lib/spectre/ssh.rb', line 47

def file_exists path
  exec "ls #{path}"
  @exit_code.nil? || @exit_code.zero?
end

#owner_of(path) ⇒ Object



52
53
54
55
# File 'lib/spectre/ssh.rb', line 52

def owner_of path
  exec "stat -c %U #{path}"
  output.chomp
end

#passphrase(phrase) ⇒ Object



43
44
45
# File 'lib/spectre/ssh.rb', line 43

def passphrase phrase
  @opts[:passphrase] = phrase
end

#password(pass) ⇒ Object



33
34
35
36
# File 'lib/spectre/ssh.rb', line 33

def password pass
  @opts[:password] = pass
  @opts[:auth_methods].push('password') unless @opts[:auth_methods].include? 'password'
end

#private_key(file_path) ⇒ Object



38
39
40
41
# File 'lib/spectre/ssh.rb', line 38

def private_key file_path
  @opts[:keys] = [file_path]
  @opts[:auth_methods].push('publickey') unless @opts[:auth_methods].include? 'publickey'
end

#username(user) ⇒ Object



29
30
31
# File 'lib/spectre/ssh.rb', line 29

def username user
  @username = user
end