Class: Net::SSH::Stream

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Stream

Returns a new instance of Stream.



9
10
11
# File 'lib/net/ssh/stream.rb', line 9

def initialize(*args)
  @session ||= Net::SSH.start *args
end

Class Method Details

.start(*args) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/net/ssh/stream.rb', line 56

def self.start(*args)
  stream = self.new *args
  yield stream
ensure
  stream.close
  nil
end

Instance Method Details

#closeObject



13
14
15
16
17
18
# File 'lib/net/ssh/stream.rb', line 13

def close
  if @session && !@session.closed?
    @session.close
    @session = nil
  end
end

#exec(command, options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/net/ssh/stream.rb', line 20

def exec(command, options={})
  stdout = options.fetch(:stdout, STDOUT)
  stderr = options.fetch(:stderr, STDERR)

  exit_code = nil
  
  @session.open_channel do |channel|
    channel.exec(command) do |ch, success|
      unless success
        abort "FAILED: couldn't execute command (ssh.channel.exec)"
      end
      
      channel.on_data do |ch, data|
        stdout << data
      end

      channel.on_extended_data do |ch, type, data|
        stderr << data
      end

      channel.on_request("exit-status") do |ch, data|
        exit_code = data.read_long
      end
    end
  end

  @session.loop
  
  exit_code
end

#exec!(command, options = {}) ⇒ Object



51
52
53
54
# File 'lib/net/ssh/stream.rb', line 51

def exec!(command, options={})
  exit_code = self.exec command, options
  raise "Command fail - Exit code #{exit_code} - #{command}" if exit_code != 0
end