pexpect 正则表达式不起作用

dee*_*eep 1 python regex pexpect

我有一个脚本,它使用 pexpect telnet 到交换机并将其运行配置复制到 tftp 服务器。如果我给出主机名,那么脚本可以正常工作,但是在 pexect 中使用正则表达式时,会发生超时。代码如下:

child = pexpect.spawn('telnet ' +ip)
child.expect ('Login: ')
child.sendline (username)
child.expect ('Password: ')
child.sendline (password)
child.sendline ('enable')
child.expect('Password: ')
child.sendline(password)
child.expect('.*\-.*#')
child.sendline ('copy running-config tftp://10.0.37.111/'+filename+'.txt')
time.sleep(5)
Run Code Online (Sandbox Code Playgroud)

我将上述正则表达式作为我当前交换机的主机名是 Force10-60。谢谢。

Nut*_*ker 5

我知道这个问题已经被接受的答案,但我想添加一个关于如何处理匹配的正则表达式的解释。

所以,我的问题是我需要获取出现在两个特定字符串之间的字符串。这就是我解决问题的方法:

import pexpect

child = pexpect.spawn(...)
child.sendline(...)
child.expect(r'firstString(.*?)secondString')

regex_obj = child.match
print(regex_obj.group(1).decode('utf-8'))
Run Code Online (Sandbox Code Playgroud)

希望这会对某人有所帮助!