Class: Cindy::Executor::SSH

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

Instance Method Summary collapse

Constructor Details

#initialize(cnx) ⇒ SSH

Returns a new instance of SSH.



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

def initialize(cnx)
    @cnx = cnx
end

Instance Method Details

#closeObject



38
39
40
# File 'lib/cindy/executor/ssh.rb', line 38

def close
    @cnx.close
end

#exec(command, stdin_str = nil, status_only = false) ⇒ 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
# File 'lib/cindy/executor/ssh.rb', line 10

def exec(command, stdin_str = nil, status_only = false)
    exit_status = 1
#                 if stdin_str
        stdout_str = stderr_str = ''
        @cnx.open_channel do |channel|
            channel.exec(command) do |ch, success|
                channel.on_data do |ch, data|
                    stdout_str += data
                end
                channel.on_extended_data do |ch, type, data|
                    stderr_str += data
                end
                channel.on_request 'exit-status' do |ch, data|
                    exit_status = data.read_long
                end
                channel.send_data stdin_str if stdin_str
                channel.eof!
            end
        end
        @cnx.loop
#                 else
#                     result = @cnx.exec!(command)
#                     result.chomp if result.respond_to? :chomp # as result can be nil <=> result.chomp if result <=> result.try? :chomp (rails way)
#                 end
    return nil if status_only && 0 != exit_status
    stdout_str.chomp
end