Class: CCSH::SSH::RemoteCommand

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRemoteCommand

Returns a new instance of RemoteCommand.



30
31
32
33
34
35
36
# File 'lib/ccsh/ssh.rb', line 30

def initialize
    @command  = nil
    @hostname = nil
    @options  = []
    @stdout = ''
    @stderr = ''
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



16
17
18
# File 'lib/ccsh/ssh.rb', line 16

def command
  @command
end

#hostnameObject

Returns the value of attribute hostname.



17
18
19
# File 'lib/ccsh/ssh.rb', line 17

def hostname
  @hostname
end

#optionsObject

Returns the value of attribute options.



18
19
20
# File 'lib/ccsh/ssh.rb', line 18

def options
  @options
end

#passwordObject

Returns the value of attribute password.



21
22
23
# File 'lib/ccsh/ssh.rb', line 21

def password
  @password
end

#portObject

Returns the value of attribute port.



20
21
22
# File 'lib/ccsh/ssh.rb', line 20

def port
  @port
end

#private_keyObject

Returns the value of attribute private_key.



22
23
24
# File 'lib/ccsh/ssh.rb', line 22

def private_key
  @private_key
end

#return_codeObject

Returns the value of attribute return_code.



27
28
29
# File 'lib/ccsh/ssh.rb', line 27

def return_code
  @return_code
end

#return_signalObject

Returns the value of attribute return_signal.



28
29
30
# File 'lib/ccsh/ssh.rb', line 28

def return_signal
  @return_signal
end

#stderrObject

Returns the value of attribute stderr.



26
27
28
# File 'lib/ccsh/ssh.rb', line 26

def stderr
  @stderr
end

#stdoutObject

Returns the value of attribute stdout.



25
26
27
# File 'lib/ccsh/ssh.rb', line 25

def stdout
  @stdout
end

#userObject

Returns the value of attribute user.



19
20
21
# File 'lib/ccsh/ssh.rb', line 19

def user
  @user
end

Instance Method Details

#execute!Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ccsh/ssh.rb', line 38

def execute!
    @options = {
        :password => @password,
        :keys     => [@private_key],
        :port     => @port,
        :host_key => @options['host_key'],
        :timeout  => @options['timeout'],
    }.select {|key,value| value != nil}

    raise "something" unless CCSH::Utils.valid_ssh(@options)

    Net::SSH.start(@hostname, @user, @options) do |ssh|
        ssh.open_channel do |ch|
            ch.exec @command do |ch, success|
                raise "Could execute command #{command} on #{host}" unless success
            end

            ch.on_data do |c, data|
                @stdout << data
            end

            ch.on_extended_data do |c, type, data|
                @stderr << data
            end

            ch.on_request("exit-status") do |c,data|
                @return_code = data.read_long
            end
        
            ch.on_request("exit-signal") do |c, data|
                @return_signal = data.read_long
            end

        end.wait    
    end

    return self
end