Module: Expect4r

Included in:
Base, Base, Ios, Iox, J
Defined in:
lib/expect4r.rb,
lib/expect/io.rb,
lib/expect/io.rb,
lib/misc/shell.rb,
lib/router/modes.rb

Defined Under Namespace

Modules: Router Classes: Base, BaseLoginObject, ConnectionError, ExpTimeoutError, Expect4rIO_Error, Ios, Iox, J, RShell, Shell, SpawnError

Instance Method Summary (collapse)

Instance Method Details

- (Object) child_exit



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/expect/io.rb', line 83

def child_exit
  if @proxy
    @proxy.logout
  else
    Process.kill(:KILL, @pid) if @pid
    @thread.kill if @thread
  end
rescue Errno::ESRCH, Errno::ECHILD => e
ensure
  @lp, @r, @w, @pid, @proxy = [nil]*5
end

- (Boolean) connected?

Returns:

  • (Boolean)


309
310
311
# File 'lib/expect/io.rb', line 309

def connected?
  @r && (not child_exited?)
end

- (Object) exp_print(s)



138
139
140
141
142
143
144
145
# File 'lib/expect/io.rb', line 138

def exp_print(s)
  exp_internal "print: #{s.inspect}, io_writer: #{@w}"
  return unless @w
  @w.print(s) and flush
rescue Errno::EIO, Errno::ECHILD
  child_exit
  raise
end

- (Object) exp_puts(s)



134
135
136
# File 'lib/expect/io.rb', line 134

def exp_puts(s)
  exp_print "#{s}\r"
end

- (Object) exp_send(lines, arg = {})



175
176
177
178
179
180
181
182
# File 'lib/expect/io.rb', line 175

def exp_send(lines, arg={})
  r=[]
  lines.each_line do |l|
    l = l.chomp("\n").chomp("\r").strip
    r << putline(l, arg) if l.size>0
  end
  lines.size==1 ? r[0] : r
end

- (Object) expect(match, ti = 5, matches = [])



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/expect/io.rb', line 184

def expect(match, ti=5, matches=[])
  t0 = Time.now
  rc, buf = catch(:done) do
    @r.readbuf(ti) do |r|
      if r._io_exit?
        throw :done, [:abort, r._io_string!]
      end
      case r._io_string
      when match
        r._io_save false, "matching PROMPT"
        # puts  "debug IO BUF 1 #{r._io_buf1.inspect}"
        throw(:done, [:ok, r._io_buf1])
        # FIXME
        # Watch out for the ping command !!!!
        # throw :done, [:ok, r._io_string!.chomp("\r\n")]
      when /(.+)\r\n/, "\r\n"
        r._io_save false, "matching EOL"
      else
      matches.each do |match, arg|
        if r._io_string =~ match
          r._io_save false, "match #{match}"
          if arg.is_a?(Proc)
            arg.call(self)
          else
            exp_puts arg
          end
        end
      end
      end
    end
  end
  case rc
  when :abort
    elapsed = Time.now - t0
    if elapsed < ti
      child_exit
      raise ConnectionError.new(buf)
    else
      raise ExpTimeoutError.new(buf, elapsed)
    end
  else
    @lp = buf.last
  end
  [buf, rc]
end

- (Object) get_prompt



230
231
232
# File 'lib/expect/io.rb', line 230

def get_prompt
  putline '', :no_trim=>true, :no_echo=>true
end

- (Object) getc



146
147
148
149
150
151
# File 'lib/expect/io.rb', line 146

def getc
  @r.getc if @r
rescue Errno::EIO, Errno::ECHILD
  child_exit
  raise
end

- (Object) interact(k = "C-z")



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/expect/io.rb', line 153

def interact(k="C-z")
  k.upcase!
  raise unless k =~ /C\-[A-Z]/
   unless connected?
  STDOUT.puts "\n\#\n\# #{k.gsub(/C\-/,'^')} to terminate.\n\#\n"
  reader :start
  writer(eval "?\\#{k}")
rescue
ensure
  begin
    reader :terminate
    putline ' ', :no_trim=>true, :no_echo=>true
  rescue => e
    exp_internal e.to_s
  end
end

- (Object) login(cmd, arg = {})



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/expect/io.rb', line 319

def (cmd, arg={})
  
  return if connected?

  if arg[:proxy]
     cmd, arg
  else
    spawn cmd
  end

  arg={:timeout=>13, :no_echo=>false}.merge(arg)
  timeout = arg[:timeout]
  no_echo = arg[:no_echo]

  output=[]
  t0 = Time.now
  ev, buf = catch(:done) do
    @r.readbuf(timeout) do |read_pipe|
      if read_pipe._io_exit?
        exp_internal "readbuf: _io_exit?"
        throw :done, [ :cnx_error,  read_pipe._io_buf1]
      end
      case read_pipe._io_string
      when spawnee_prompt
        read_pipe._io_save no_echo, "match PROMPT"
        throw(:done, [:ok, read_pipe._io_buf1])
      when /Last (L|l)ogin:/
        read_pipe._io_save no_echo   # consumes 
      when /(user\s*name\s*|login):\s*$/i
        read_pipe._io_save no_echo, "match USERNAME"
        exp_puts spawnee_username
      when /password:\s*$/i
        read_pipe._io_save no_echo, "match PASSWORD"
        @w.print(spawnee_password+"\r") and flush
      when /Escape character is/
        read_pipe._io_save no_echo, "match Escape char"
        io_escape_char_cb
      when /.*\r\n/
        exp_internal "match EOL"
        read_pipe._io_save no_echo, "match EOL"
      when /Are you sure you want to continue connecting \(yes\/no\)\?/
        read_pipe._io_save no_echo, "match continue connecting"
        exp_puts 'yes'
      when /(Login incorrect|denied, please try again)/
        spawnee_reset
      else
        # For objects that include Expect4r but do not subclass base Login class.
        @matches ||= []
        @matches.each { |match, _send|
          if read_pipe._io_string =~ match
            read_pipe._io_save no_echo, "match #{match}"
            if _send.is_a?(Proc)
              exp_puts _send.call
            else
              exp_puts _send
            end
          end
        }
      end          
    end
  end
  case ev
  when :cnx_error
    child_exit
    err_msg = "Could not connect to #{@host}:\n"
    err_msg += " >> #{cmd}\n    "
    err_msg += buf.join("\n    ")
    raise ConnectionError.new(err_msg)
  else
    @lp = buf.last
  end
  [buf, ev]
end

- (Object) login_by_proxy(proxy) Also known as: login_via

Raises:

  • (ArgumentError)


313
314
315
316
# File 'lib/expect/io.rb', line 313

def (proxy)
  raise ArgumentError, "Don't know how to login to #{proxy}" unless proxy.respond_to? :_login_
   :proxy=> proxy
end

- (Object) logout



120
121
122
123
# File 'lib/expect/io.rb', line 120

def logout
  child_exit
  @pid
end

- (Object) putc(c)



125
126
127
128
129
130
131
132
# File 'lib/expect/io.rb', line 125

def putc(c)
  return unless @w || c.nil?
  exp_internal "[#{c}]"
  @w.putc(c) and flush
rescue Errno::EIO
  child_exit
  raise 
end

- (Object) putcr



170
171
172
173
# File 'lib/expect/io.rb', line 170

def putcr
  putline '', :no_trim=>true, :no_echo=>true
  nil
end

- (Object) readline(ti = 0.2, matches = [])



302
303
304
305
306
307
# File 'lib/expect/io.rb', line 302

def readline(ti=0.2, matches=[])
  ret = expect(/(.+)\r\n/, ti, matches)
  ret[0][0].chomp
rescue ExpTimeoutError => ex
  ''
end

- (Object) spawn(cmd)



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/expect/io.rb', line 95

def spawn(cmd)
  begin
    child_exited = false
    @thread = Thread.new do
      PTY.spawn(cmd) do |pipe_read, pipe_write, pid|
        @r, @w, @pid = pipe_read, pipe_write, pid
        begin 
          Process.wait(@pid,0)
        rescue
        ensure
          child_exited = true
        end
      end
    end
    @thread.priority = -2
    unless child_exited
      while @r.nil?
        sleep(0.05) 
      end
    end
  rescue => e
    raise SpawnError.new(cmd)
  end
end