Re: [Tutor] Pexpect maxread & searchwindowsize, timeout

2009-10-20 Thread Nathan Farrar
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  wrote:
>
>
> On Tue, Oct 20, 2009 at 11:11 AM, Nathan Farrar 
> 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 usern...@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  -  tu...@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 usern...@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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pexpect maxread & searchwindowsize, timeout

2009-10-20 Thread vince spicer
On Tue, Oct 20, 2009 at 11:11 AM, Nathan Farrar 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 usern...@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@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 usern...@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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pexpect

2008-09-20 Thread Steve Poe

James,

I've not used pexpect, but I've done  this on a Cisco switch. I found  
using

time.sleep and read_until of the telnet class to be helpful.

10  tn = telnetlib.Telnet('')
 11 #tn.set_debuglevel(9)
 12 tn.read_until('Username: \xff', 5)
 13 time.sleep(10)
 14 tn.write('\n')
 15 tn.read_until('\r\nPassword: ', 5)
 16 #time.sleep(2)
 17 tn.write('\n')
 18 tn.read_until('#', 5)
 19 tn.write('term len 0 \n')
 20 tn.read_until('#', 5)
 21 tn.write('show version |  i restarted\n')
 22 RouterStartupTimestamp = tn.read_until('#', 2)
 23 tn.read_until('#', 2)
 24 tn.write('show call history voice last 100\n')


HTH,

Steve

On Sep 20, 2008, at 8:43 AM, James wrote:


Folks,

Does anyone here have experience with pexpect? I'm trying to write a
pexpect script which will log into a network device, gather
statistics, and then dump the raw output into a file (or a string so
that I can manipulate it).

I'm not having much luck. Because the connection is telnet I was able
to use wireshark (a sniffer) to see the traffic between my computer
(which is running the pexpect script) and the network device.
Everything seems to go find until I "expect" the network device's
prompt and it just hangs. Eventually pexpect throws an exception and
times out.

I'd be happy to share some code when I get to the computer I've been
testing on. :) In the time being, has anyone seen a problem like this
before?

Thanks!
-j
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor