Class: SSH

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

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ SSH

Returns a new instance of SSH.



6
7
8
9
# File 'lib/ssh.rb', line 6

def initialize(config)
  @config = config
  @ssh = Net::SSH.start(@config[:host], @config[:user])
end

Instance Method Details

#closeObject



37
38
39
# File 'lib/ssh.rb', line 37

def close
  @ssh.close
end

#exec!(command) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ssh.rb', line 11

def exec!(command)
 
  stdout_data = ""
  stderr_data = ""
  exit_code = nil
  @ssh.open_channel do |channel|
    channel.send_channel_request "shell" do |ch, success|
      unless success
        abort "FAILED to connect"
      end
      channel.on_data {|c,data| stdout_data+=data.to_s }
      channel.on_extended_data {|c,data| stderr_data+=data.to_s }
      channel.on_request("exit-status") {|c,data| exit_code = data.read_long }
      ch.send_data "#{command}\n"
      ch.send_data "exit\n"
    end
  end
  @ssh.loop
  if exit_code > 0
    return false
  else
    return stdout_data.strip
  end
end