Method: RHC::SSHHelpers#ssh_ruby

Defined in:
lib/rhc/ssh_helpers.rb

#ssh_ruby(host, username, command, compression = false, request_pty = false, &block) ⇒ Object

Public: Run ssh command on remote host

host - The String of the remote hostname to ssh to. username - The String username of the remote user to ssh as. command - The String command to run on the remote host. compression - Use compression in ssh, set to false if sending files. request_pty - Request for pty, set to false when pipe a file. block - Will yield this block and send the channel if provided.

Examples

ssh_ruby('myapp-t.protonbox.me',
          '109745632b514e9590aa802ec015b074',
          'pboxsh tail -f $PROTONBOX_LOG_DIR/*"')
# => true

Returns true on success



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/rhc/ssh_helpers.rb', line 169

def ssh_ruby(host, username, command, compression=false, request_pty=false, &block)
  debug "Opening Net::SSH connection to #{host}, #{username}, #{command}"
  exit_status = 0
  Net::SSH.start(host, username, :compression => compression) do |session|
    #:nocov:
    channel = session.open_channel do |channel|
      if request_pty
        channel.request_pty do |ch, success|
          say "pty could not be obtained" unless success
        end
      end
      channel.exec(command) do |ch, success|
        channel.on_data do |ch, data|
          print data
        end
        channel.on_extended_data do |ch, type, data|
          print data
        end
        channel.on_close do |ch|
          debug "Terminating ... "
        end
        channel.on_request("exit-status") do |ch, data|
          exit_status = data.read_long
        end
        yield channel if block_given?
        channel.eof!
      end
    end
    session.loop
    #:nocov:
  end
  raise RHC::SSHCommandFailed.new(exit_status) if exit_status != 0
rescue Errno::ECONNREFUSED => e
  debug_error e
  raise RHC::SSHConnectionRefused.new(host, username)
rescue Net::SSH::AuthenticationFailed => e
  debug_error e
  raise RHC::SSHAuthenticationFailed.new(host, username)
rescue SocketError => e
  debug_error e
  raise RHC::ConnectionFailed, "The connection to #{host} failed: #{e.message}"
end