Python 预期问题

Python pexpect issue

好的,所以我正在尝试编写一个脚本来将配置保存在我的网络设备上。我在某个地方出错了,我不确定在哪里。它不会发送最终确认 "y" 字符,但我认为这是由于我告诉它所期望的。

当前代码:

import pexpect
import sys
import os
import time
import getpass

hostname = "switch1"
username = raw_input('Enter Your username: ')
password = getpass.getpass('Password:')
fout = file('mylog.txt','w')

child = pexpect.spawn('ssh %s@%s' % (username, hostname))
child.logfile_read = fout
child.expect('myusername@%s\'s password:' % hostname)
child.sendline(password)
child.expect('>')
child.sendline('enable')
child.expect('Password:')
child.sendline(password)
child.expect('#')
child.sendline('wr mem')
child.expect("Are you sure you want to save? (y/n) ")
child.sendline('y')
child.expect('#')
child.sendline('logout')

我也试过这行产生了相同的结果:

child.expect("\r\n\r\nThis operation may take a few minutes.\r\nManagement interfaces will not be available during this time.\r\n\r\nAre you sure you want to save? (y/n) ")

这是我的开关上的样子:

(switch1) #write mem

This operation may take a few minutes.
Management interfaces will not be available during this time.

Are you sure you want to save? (y/n) y

Config file 'startup-config' created successfully .


Configuration Saved!

(switch1) #

这是我在 运行 脚本时遇到的错误:

python wrmem.py 
Enter Your username: myusername
Password:
Traceback (most recent call last):
  File "wrmem.py", line 35, in <module>
    child.expect("Are you sure you want to save? (y/n) ")
  File "/usr/local/lib/python2.7/dist-packages/pexpect/spawnbase.py", line 327, in expect
    timeout, searchwindowsize, async_)
  File "/usr/local/lib/python2.7/dist-packages/pexpect/spawnbase.py", line 355, in expect_list
    return exp.expect_loop(timeout)
  File "/usr/local/lib/python2.7/dist-packages/pexpect/expect.py", line 104, in expect_loop
    return self.timeout(e)
  File "/usr/local/lib/python2.7/dist-packages/pexpect/expect.py", line 68, in timeout
    raise TIMEOUT(msg)
pexpect.exceptions.TIMEOUT: Timeout exceeded.
<pexpect.pty_spawn.spawn object at 0x7f5d9c82af90>
command: /usr/bin/ssh
args: ['/usr/bin/ssh', 'myusername@switch1']
buffer (last 100 chars): 'nagement interfaces will not be available during this time.\r\n\r\nAre you sure you want to save? (y/n) '
before (last 100 chars): 'nagement interfaces will not be available during this time.\r\n\r\nAre you sure you want to save? (y/n) '
after: <class 'pexpect.exceptions.TIMEOUT'>
match: None
match_index: None
exitstatus: None
flag_eof: False
pid: 22641
child_fd: 6
closed: False
timeout: 30
delimiter: <class 'pexpect.exceptions.EOF'>
logfile: None
logfile_read: <open file 'mylog.txt', mode 'w' at 0x7f5d9ae0c150>
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
searcher: searcher_re:
    0: re.compile("Are you sure you want to save? (y/n) ")

我再次认为它与 expect "are you sure you want to save" 行有关,但我不确定。我可以确认登录到交换机和其他命令工作,因为我有其他脚本,只是这个我想不通。感谢任何帮助。

查看日志:

searcher: searcher_re: 0: re.compile("Are you sure you want to save? (y/n) ")

"Are you sure you want to save? (y/n) " 编译为正则表达式会将 ?, (, / and ) 作为特殊字符,因此请尝试像这样对它们进行转义:

"Are you sure you want to save\? \(y\/n\) " 将匹配文本 "Are you sure you want to save? (y/n) " 检查 this

因此将此行更改为:

child.expect("Are you sure you want to save\? \(y\/n\) ")

Pexpect 也支持部分匹配。所以你也可以使用,

child.expect('Are you sure you want to save?')