pexpect 期待不止一种模式
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')
我在这里要做的是:
- 通过解析启动顺序自动登录到设备的单用户模式。
- 正在通过控制台端口使用终端服务器连接到设备。 $ 我正在使用 pexpect,因为引导序列是单行输出。
- 预计 'loader>' 或 'OK ' 分别执行并执行下一行,但不会一起执行。
根据 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.
[...]
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')
我在这里要做的是:
- 通过解析启动顺序自动登录到设备的单用户模式。
- 正在通过控制台端口使用终端服务器连接到设备。 $ 我正在使用 pexpect,因为引导序列是单行输出。
- 预计 'loader>' 或 'OK ' 分别执行并执行下一行,但不会一起执行。
根据 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.
[...]