Class: Hussh::Channel

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ Channel

Returns a new instance of Channel.



3
4
5
6
# File 'lib/hussh/channel.rb', line 3

def initialize(session)
  @session = session
  @request_pty = false
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



16
17
18
# File 'lib/hussh/channel.rb', line 16

def command
  @command
end

#exec_blockObject (readonly)

Returns the value of attribute exec_block.



17
18
19
# File 'lib/hussh/channel.rb', line 17

def exec_block
  @exec_block
end

Instance Method Details

#closeObject



83
84
85
86
87
88
89
90
91
92
# File 'lib/hussh/channel.rb', line 83

def close
  if have_real_channel?
    @real_channel.close
    @session.update_recording(@command, @stdout) if @stdout
  else
    stdout = @session.response_for(@command)
    @on_data_callback.call(self, stdout) if stdout && @on_data_callback
    @on_extended_data_callback.call(self, @stderr) if @stderr && @on_extended_data_callback
  end
end

#exec(command, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hussh/channel.rb', line 18

def exec(command, &block)
  @command = command
  Hussh.commands_run << @command
  if !@session.has_response?(@command)
    open_real_channel
    request_pty(&@request_pty_callback) if @request_pty
    on_data(&@on_data_callback) if @on_data_callback
    if @on_extended_data_callback
      on_extended_data(&@on_extended_data_callback)
    end

    @real_channel.exec(command) do |ch, success|
      @exec_result = success
      block.call(self, success) if block
    end
  elsif block_given?
    yield(self, true)
  end
end

#have_real_channel?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/hussh/channel.rb', line 8

def have_real_channel?
  !!@real_channel
end

#on_data(&block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hussh/channel.rb', line 53

def on_data(&block)
  if have_real_channel?
    @real_channel.on_data do |ch, output|
      @stdout ||= ''
      @stdout << output
      block.call(ch, output) if block
    end
  else
    @on_data_callback = block
  end
end

#on_extended_data(&block) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/hussh/channel.rb', line 65

def on_extended_data(&block)
  if have_real_channel?
    @real_channel.on_extended_data do |ch, output|
      @stderr ||= ''
      @stderr << output
      block.call(ch, output) if block
    end
  else
    @on_extended_data_callback = block
  end
end

#open_real_channelObject



12
13
14
# File 'lib/hussh/channel.rb', line 12

def open_real_channel
  @real_channel ||= @session.real_session.open_channel
end

#request_pty(&block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/hussh/channel.rb', line 38

def request_pty(&block)
  if have_real_channel?
    @real_channel.request_pty do |ch, success|
      block.call(ch, success) if block
    end
  else
    @request_pty = true
    @request_pty_callback = block
  end
end

#requested_pty?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/hussh/channel.rb', line 49

def requested_pty?
  @request_pty
end

#waitObject



77
78
79
80
81
# File 'lib/hussh/channel.rb', line 77

def wait
  if @real_channel
    @real_channel.wait
  end
end