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.



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

def initialize(config)
  @config = config
  "SSH: #{@config[:user]}@#{@config[:host]}".log
  @ssh = Net::SSH.start(@config[:host], @config[:user])
end

Instance Method Details

#closeObject



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

def close
  @ssh.close
end

#exec!(command) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ssh.rb', line 10

def exec!(command)
  # I am not awesome enough to have made this method myself
  # I've just modified it a bit
  # Originally submitted by 'flitzwald' over here: http://stackoverflow.com/a/3386375
  stdout_data = ""
  stderr_data = ""
  exit_code = nil
  "#{command.strip}".log
  @ssh.open_channel do |channel|
    channel.exec(command) do |ch, success|
      unless success
        abort "FAILED: couldn't execute command (ssh.channel.exec)"
      end
      channel.on_data do |ch,data|
        stdout_data+=data
      end

      channel.on_extended_data do |ch,type,data|
        stderr_data+=data
      end

      channel.on_request("exit-status") do |ch,data|
        exit_code = data.read_long
      end
    end
  end
  @ssh.loop
  stdout_data.strip.log
  stderr_data.strip.log
  if exit_code > 0
    return false
  else
    return stdout_data.strip
  end
end