Search code examples
pythonpexpect

pexpect to expect more than one pattern


import pexpect # importing the python-expect
child = pexpect.spawn ('telnet x.x.x.x y')
child.expect ('Hit \[Enter\] to boot immediately\, or space bar for command prompt.', 300)
child.send ('\x20')
if child.expect ('loader>' or 'OK ', 10):
   child.sendline ('boot -s')

What I am trying to do here is:

  • automating login to single user mode for a device by parsing boot sequence.
  • Connecting to the device using a terminal server over the console port. $ I am using pexpect as the boot sequence is a single line output.
  • pexpect work for 'loader>' or 'OK ' separately & execute the next line but not together.

Solution

  • According to the manual:

    expect(pattern, timeout=-1, searchwindowsize=-1, async=False)

    This seeks through the stream until a pattern is matched. The pattern is overloaded and may take several types. The pattern can be a StringType, EOF, a compiled re, or a list of any of those types. Strings will be compiled to re types. This returns the index into the pattern list. If the pattern was not a list this returns index 0 on a successful match.
    [...]