Module: Expect4r

Included in:
Base, Ios, Iox, J, V
Defined in:
lib/expect4r.rb,
lib/expect/io.rb,
lib/misc/base.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, V

Instance Method Summary (collapse)

Instance Method Details

- (Object) _login(cmd, arg = {}) Also known as: login



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
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
# File 'lib/expect/io.rb', line 267

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
      
      @pre_matches ||= []
      @pre_matches.each do |match, _send|
        if read_pipe._io_string =~ match
          read_pipe._io_save no_echo, "match #{match}"
          if _send.is_a?(Proc)
            _send.call
          else
            exp_puts _send.to_s
          end
        end
      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_1 = buf[-2]
    @lp = buf.last
  end
  [buf, ev]
end

- (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)


257
258
259
# File 'lib/expect/io.rb', line 257

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

- (Object) exp_print(s)



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

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)



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

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

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



184
185
186
187
188
189
190
191
# File 'lib/expect/io.rb', line 184

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 = [])



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
229
230
231
232
233
234
235
236
237
# File 'lib/expect/io.rb', line 193

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



239
240
241
# File 'lib/expect/io.rb', line 239

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

- (Object) getc



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

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

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



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/expect/io.rb', line 154

def interact(k="C-t")
  raise unless k =~ /C\-[A-Z]/i
  unless STDOUT.tty? and STDIN.tty?
    $stderr.puts "Cannot interact: not running from terminal!"
    return
  end
   unless connected?
  @@interact_mutex ||= Mutex.new

  @@interact_mutex.synchronize {
    k.upcase!
    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_by_proxy(proxy) Also known as: login_via

Raises:

  • (ArgumentError)


261
262
263
264
# File 'lib/expect/io.rb', line 261

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
124
# File 'lib/expect/io.rb', line 120

def logout
  child_exit
  @lp, @_lp_1 = nil,nil
  @pid
end

- (Object) putc(c)



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

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



179
180
181
182
# File 'lib/expect/io.rb', line 179

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

- (Object) read_until(match, ti = 0.2)



250
251
252
253
254
255
# File 'lib/expect/io.rb', line 250

def read_until(match,ti=0.2)
  ret = expect(match, ti, [])
  ret[0][0].chomp
rescue ExpTimeoutError => ex
  ''
end

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



243
244
245
246
247
248
# File 'lib/expect/io.rb', line 243

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