[Tutor] Pexpect maxread & searchwindowsize, timeout

vince spicer vinces1979 at gmail.com
Tue Oct 20 19:26:14 CEST 2009


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091020/96c30b17/attachment.htm>


More information about the Tutor mailing list