好的,我正在编写一个脚本来保存我的网络设备上的配置。我哪里出错了,我不知道在哪里。它不会发送最后确认的"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“你确定你想保存”行有关,但我不确定。我可以确认登录到开关和其他命令工作,因为我有其他脚本,只是这一个,我不知道。任何帮助都是非常感谢的。
发布于 2018-03-20 20:53:07
查看日志:
searcher: searcher_re: 0: re.compile("Are you sure you want to save? (y/n) ")
将"Are you sure you want to save? (y/n) "编译为regex将使?, (, / and )成为特殊字符,因此尝试将它们赋值如下:
"Are you sure you want to save\? \(y\/n\) ",它将与文本"Are you sure you want to save? (y/n) "复选这匹配。
因此,将这一行更改为:
child.expect("Are you sure you want to save\? \(y\/n\) ")
发布于 2018-03-20 21:19:53
Pexpect还支持部分匹配。所以你也可以用,
child.expect('Are you sure you want to save?')https://stackoverflow.com/questions/49393652
复制相似问题