[Tutor] Pexpect maxread & searchwindowsize, timeout

Nathan Farrar nathan.farrar at gmail.com
Tue Oct 20 19:11:06 CEST 2009


I haven't been able to find any real examples of pexpect usage, nor
documentation.  Just little bits here and there, so I'm kind of
struggling through.

I've got the follow bit of code I'm working with:

def main():
    try:
        print 'attempting to spawn connection ... '
        session = pexpect.spawn('ssh username at x.x.x.x')
    except:
        print 'couldn\'t spawn connection ... '

    print 'waiting for password prompt ... '
    session.expect('password:')
    print 'received password prompt ... '

    try:
        print 'attempting to send password ... '
        session.sendline(password)
    except:
        print 'error sending password ... '

    print 'waiting for command prompt ... '
    session.expect(command_prompt)
    print 'received command prompt ... '

    try:
        print 'attempting to issue \'show version\' command ... '
        session.sendline([expect.TIMEOUT=1, 'show version'])
    except:
        print 'error issuing \'show version\' command ... '

    print 'waiting for command prompt ... '
    session.expect(command_prompt)
    print 'received command prompt ... '

    print 'attempting to print command results ... '
    print session.before

    print 'closing session ... '
    session.close()

if __name__=='__main__':
    main()

When I run this against a cisco device, it times out waiting for the
command prompt after issuing the show version command.  However, if I
change 'show version' to something like 'blah blah' it doesn't time
out, and I get the results of the command (an error message that is
much shorter in length).

I believe that the results of the show version command are simply too
large.  I think I may need to increase the size of maxread &
searchwindowsize & set the timeout to something lower than the
default, but I haven't been able to figure out how to do this
correctly yet.

Any help would be greatly appreciated.  I'm pulling my hair out.  Thank you.

-- 
"The presence of those seeking the truth is infinitely to be preferred
to the presence of those who think they've found it."

–Terry Pratchett


More information about the Tutor mailing list