python pexpect - 不接受期望输出

时间:2013-08-13 19:50:54

标签: python pexpect

我正在使用pepexpect登录路由器。我这样做的方法是登录linux服务器,然后登录路由器。

usr = 'myusername'
pwd='mypwd'
child.sendline('ssh -o StrictHostKeyChecking=no -l '+usr+' '+ip)
index = child.expect(['assword:', pexpect.EOF,pexpect.TIMEOUT])

当我手动登录路由器时,我得到以下内容:

WARNING NOTICE: This is a private system. The actual or attempted, unauthorized
access, use or modification of this system is strictly prohibited.
Individuals undertaking such unauthorized access, use or modification are
subject to company disciplinary proceedings and/or criminal and civil penalties
under applicable domestic and foreign laws. The use of this system may be
monitored and recorded for administrative and security reasons in accordance
with local law. If such monitoring and/or recording reveals possible evidence
of criminal activity, the results of such monitoring may be provided to law
enforcement officials. Continued use of this system after receipt of this
notice constitutes consent to such security monitoring and recording.
!
Global Baseline Configuration:  v1.0

Cisco Wide Area Application Engine

username@10.58.218.237's password: 

即使密码在输出上,索引结果也是2 = pexpect.TIMEOUT 我的脚本工作在其他路由器上,但​​我不知道为什么不在这个上工作。感谢

1 个答案:

答案 0 :(得分:1)

我很难使用pexpect来填充提示,但我已经设法写了一些有用的东西:

def call_and_type(command, prompt_regex, entry):
    p = pexpect.spawn(command, logfile=sys.stdout, maxread=16384)
    index = p.expect_exact([prompt_regex, pexpect.EOF])
    if index == 0:
        p.setecho(False)
        p.sendline(entry)
        while p.read():
            pass