class Net::SSH::Connection::Session

Public Instance Methods

suexec(command, &block) click to toggle source

use a psuedo TTY so we can run sudo commands on more secured cloud instances

# File lib/theusual/ssh.rb, line 9
def suexec(command, &block)
  open_channel do |channel|
    channel.request_pty

    # add sudo prefix if need be
    command = "sudo #{command}" unless /^sudo /.match command

    channel.exec(command) do |ch, success|
      raise "could not execute command: #{command.inspect}" unless success

      channel.on_data do |ch2, data|
        if block
          block.call(ch2, :stdout, data)
        else
          $stdout.print(data)
        end
      end

      channel.on_extended_data do |ch2, type, data|
        if block
          block.call(ch2, :stderr, data)
        else
          $stderr.print(data)
        end
      end
    end
  end
end
suexec!(command, &block) click to toggle source
# File lib/theusual/ssh.rb, line 39
def suexec!(command, &block)
  block ||= Proc.new do |ch, type, data|
    ch[:result] ||= ""
    ch[:result] << data
  end

  channel = suexec(command, &block)
  channel.wait

  return channel[:result]
end