Class: Ssh

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

Instance Method Summary collapse

Constructor Details

#initialize(server, quiet = false) ⇒ Ssh

Returns a new instance of Ssh.



4
5
6
7
8
# File 'lib/relay.rb', line 4

def initialize(server, quiet = false)
  @server = server
  @output = []
  @quiet = quiet
end

Instance Method Details

#run(command) ⇒ Object



36
37
38
39
# File 'lib/relay.rb', line 36

def run(command)
  puts "\e[1m\e[33m$ #{command.strip}\e[0m" unless @quiet
  @stdin.puts(command)
end

#startObject



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
# File 'lib/relay.rb', line 10

def start
  Open3.popen3("ssh -T #{@server}") do |stdin, stdout, stderr|
    threads = []

    @stdin, @stdout, @stderr = stdin, stdout, stderr

    threads << Thread.new do
      while line = stderr.gets
        $stderr.puts(line) unless @quiet
      end
    end

    threads << Thread.new do
      while line = stdout.gets
        @output << line
        $stdout.puts(line) unless @quiet
      end
    end

    yield(self)
    stdin.close
    threads.each {|t| t.join }
    @output
  end
end