[Tutor] Pexpect maxread & searchwindowsize, timeout

Nathan Farrar nathan.farrar at gmail.com
Tue Oct 20 21:02:07 CEST 2009


Thanks!  That solves my question about configuring the timeout value.
However - I still have the problem (I believe) that by default, I can
handle all the output.  When I issue the "show version" it fails, but
if I issue a command with less data returned it succeeds.

In the documentation I can see that pexpect uses a maxread and a
searchwindowsize value, but I'm not sure how to use them.

Any tips?  Thanks again.

On Tue, Oct 20, 2009 at 11:26 AM, vince spicer <vinces1979 at gmail.com> wrote:
>
>
> On Tue, Oct 20, 2009 at 11:11 AM, Nathan Farrar <nathan.farrar at gmail.com>
> wrote:
>>
>> 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
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>
> Looks like you are trying to end multiple commands to the sendline and not
> expect
> you can specify the timeout on the expect line
>
> try this code:
>
> #================================================
>
> 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('show version') #: send only command
>    except:
>        print 'error issuing \'show version\' command ... '
>
>    print 'waiting for command prompt ... '
>    session.expect(command_prompt, timeout=1) #: set the timeout here
>    print 'received command prompt ... '
>
>    print 'attempting to print command results ... '
>    print session.before
>
>    print 'closing session ... '
>    session.close()
>
> if __name__=='__main__':
>    main()
> #==============================
>
> Vince
>
>
>



-- 
"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