Python pexpect无法识别“0”;“更多”;关于思科
正在尝试登录到Cisco路由器。它没有在--More--“提示符下发送正确的命令。我想这可能和我发送的内容有关。我需要在收到更多提示时发送一个空格命令 它将发送一个“\”字符,但网络也将发送一个“”或任意字母 当前代码:Python pexpect无法识别“0”;“更多”;关于思科,python,cisco,pexpect,Python,Cisco,Pexpect,正在尝试登录到Cisco路由器。它没有在--More--“提示符下发送正确的命令。我想这可能和我发送的内容有关。我需要在收到更多提示时发送一个空格命令 它将发送一个“\”字符,但网络也将发送一个“”或任意字母 当前代码: import pexpect import sys import os import getpass hostname = "router-2" username = raw_input('Enter Your username: ') password = getpass.
import pexpect
import sys
import os
import getpass
hostname = "router-2"
username = raw_input('Enter Your username: ')
password = getpass.getpass('Password:')
fout = file('agglog.txt','w')
child = pexpect.spawn('ssh %s@%s' % (username, hostname))
child.logfile_read = fout
child.expect('Password:')
child.sendline(password)
child.expect('>')
child.sendline('enable')
child.expect('Password:')
child.sendline(password)
child.expect('#')
child.sendline('show processes cpu history')
i = child.expect(["--More--","#"])
if i==0:
child.sendline(' ') ###PROBLEM IS HERE
else:
child.sendline('show processes cpu | i Core 0|Core 1')
child.expect('#')
child.sendline('logout')
再一次,在
--更多提示:当尝试发送space命令时,它只是挂起,好像在等待什么。在Cisco路由器上,当您在该提示符下按空格键时,它会显示更多输出。由于权限原因,“终端长度0”在这种情况下不是选项
非常感谢您的帮助
编辑:
以下是输出结果:
router-2#show processes cpu history
History information for system:
111112222222222111111111122222111111111122222111111111111111
777770000000000999998888833333888889999900000999998888899999
100
90
80
70
60
50
40
30
20 ************************************************************
10 ************************************************************
0....5....1....1....2....2....3....3....4....4....5....5....
0 5 0 5 0 5 0 5 0 5
CPU% per second (last 60 seconds)
222222222222222222221222222222222222222222222222222222222222
342000300104111121019410422602031010060202210143001042120601
--More--
我假设pexpect刚好在最后一行之前超时,因为它期待#,相反它得到了另一个--更多--?你认为你需要使用while循环,比如:
while count < 10:
output = child.readline()
if '--More--' in output:
child.sendline(' ')
elif '#' in output:
child.sendline('logout')
count +=1
time.sleep(1)
计数<10时:
output=child.readline()
如果输出中出现“--More--”:
child.sendline(“”)
输出中的elif“#”:
child.sendline('logout')
计数+=1
时间。睡眠(1)
在每次循环之前,最好先休眠一秒钟,让终端有时间输出下一页
希望这有帮助
编辑:
嗯,这有点奇怪,expect和readline无法解析——更多——但它在pexpect缓冲区中,它看到了它。我已经对此进行了测试,它在我身上起了作用:
child.sendline('show processes cpu history')
count = 0
while count < 100:
output = child.readline().strip()
if '--More--' in child.buffer: ### read buffer like so
print output
child.sendline(' ')
elif '#' in output:
print 'done'
child.sendline('logout')
break
count +=1
child.sendline('show processs cpu history')
计数=0
当计数小于100时:
输出=child.readline().strip()
如果在child.buffer:####中有'--More--',请这样读取缓冲区
打印输出
child.sendline(“”)
输出中的elif“#”:
打印“完成”
child.sendline('logout')
打破
计数+=1
如果您尝试打印child.buffer,您会注意到路由器终端发送了很多,因此您可能需要调整循环计数。对于任何偶然发现此线程的人,我过去通过首先发送以下命令来回避此问题:
terminal length 0
这将阻止设备“分页”输出,以便您可以停止并读取它
该命令仅与当前打开的会话相关,并在断开连接后消失。Ok尝试了该操作。还是一样的问题。当“--More--”存在时,不会发送任何内容。我已经编辑了我的原始答案,请告诉我它是否适用于您,这是一个有点奇怪/有趣的问题。