Class: Net::SSH::Connection::Session

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

Instance Method Summary collapse

Instance Method Details

#suexec(command, &block) ⇒ Object

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



9
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/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) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# 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