Class: Shaddox::Server::SSHActor

Inherits:
Actor
  • Object
show all
Defined in:
lib/shaddox/target.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, user, ssh_opts, &block) ⇒ SSHActor

Returns a new instance of SSHActor.



114
115
116
117
118
119
# File 'lib/shaddox/target.rb', line 114

def initialize(host, user, ssh_opts, &block)
  Net::SSH.start(host, user, ssh_opts) do |ssh|
    @ssh = ssh
    super(&block)
  end
end

Instance Method Details

#exec(command) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/shaddox/target.rb', line 120

def exec(command)
  exit_code = nil
  @ssh.open_channel do |channel|
    channel.exec(command) do |ch, success|
      #return nil if !success
      ch.on_data do |ch, data|
        $stdout.print data
        if data =~ /^\[sudo\] password for user:/
          channel.send_data(gets.strip)
        end
      end
      ch.on_extended_data do |ch, data|
        $stderr.print data
        if data =~ /^\[sudo\] password for user:/
          channel.send_data(gets.strip)
        end
      end
      ch.on_request('exit-status') do |ch, data|
        exit_code = data.read_long
      end
    end
  end
  @ssh.loop
  exit_code == 0 ? true : false
end

#write_file(content, dest_path) ⇒ Object



145
146
147
148
# File 'lib/shaddox/target.rb', line 145

def write_file(content, dest_path)
  require 'shellwords'
  exec "echo #{Shellwords.shellescape(content)} > #{dest_path}"
end