Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pexpect baby steps
#8
Thank you heiner55, Minister of Silly Walks.
Appreciate your reply.
I am satisfied that a cached username is being used.
I have made some improvement now as shown below.
The child.logfile = sys.stdout command I did not have before and that is allowing me to see the output from the target box but only when I execute a child.expect('#'):
>>> import pexpect
>>> import sys
>>> child=pexpect.spawn("ssh bne-con-7")
>>> child.expect('Password:')
0
>>> child.sendline('1q2w#E$R5t6y')
13
>>> child.expect('#')
0
>>> child.sendline('sh clock')
9
>>> child.expect('#')
0
>>> child.logfile = sys.stdout
>>> 
>>> child.expect('#')
Notice above that I issue a second child.expect('#') which causes this error below. Presumably this is bacause an attempt is being made to empty an already empty buffer:
Error:
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/dist-packages/pexpect/spawnbase.py", line 315, in expect timeout, searchwindowsize, async) File "/usr/lib/python2.7/dist-packages/pexpect/spawnbase.py", line 339, in expect_list return exp.expect_loop(timeout) File "/usr/lib/python2.7/dist-packages/pexpect/expect.py", line 104, in expect_loop return self.timeout(e) File "/usr/lib/python2.7/dist-packages/pexpect/expect.py", line 68, in timeout raise TIMEOUT(msg) pexpect.exceptions.TIMEOUT: Timeout exceeded. <pexpect.pty_spawn.spawn object at 0x7fe7d84b5fd0> command: /usr/bin/ssh args: ['/usr/bin/ssh', 'bne-con-7'] searcher: None buffer (last 100 chars): '' before (last 100 chars): '' after: <class 'pexpect.exceptions.TIMEOUT'> match: None match_index: None exitstatus: None flag_eof: False pid: 15682 child_fd: 5 closed: False timeout: 30 delimiter: <class 'pexpect.exceptions.EOF'> logfile: <open file '<stdout>', mode 'w' at 0x7fe7d85e6150> logfile_read: None logfile_send: None maxread: 2000 ignorecase: False searchwindowsize: None delaybeforesend: 0.05 delayafterclose: 0.1 delayafterterminate: 0.1
In the same session I issue my sh clock command again and hey presto I have the output:
>>> child.sendline('sh clock')
sh clock
9
>>> 
>>> child.expect('#')
sh clock
14:06:28.471 EST Thu May 23 2019
XXX-XXX-X#0
>>>

My first real result.
Now I try a longer command.
This one as you can see requires the space bar to be hit to continue the output:
>>> child.sendline('sh ver')
sh ver
7
>>> child.expect('#')
sh ver
Cisco IOS Software, C2600 Software (C2600-ADVIPSERVICESK9-M), Version 12.4(19), RELEASE SOFTWARE (fc1)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2008 by Cisco Systems, Inc.
Compiled Fri 29-Feb-08 19:23 by prod_rel_team

ROM: System Bootstrap, Version 12.2(7r) [cmong 7r], RELEASE SOFTWARE (fc1)

XXX-XXX-X uptime is 8 weeks, 2 days, 2 hours, 6 minutes
System returned to ROM by power-on
System restarted at 15:04:36 EST-DLS Tue Mar 26 2019
System image file is "flash:c2600-advipservicesk9-mz.124-19.bin"


This product contains cryptographic features and is subject to United
States and local country laws governing import, export, transfer and
use. Delivery of Cisco cryptographic products does not imply
third-party authority to import, export, distribute or use encryption.
Importers, exporters, distributors and users are responsible for
compliance with U.S. and local country laws. By using this product you
agree to comply with applicable laws and regulations. If you are unable
to comply with U.S. and local laws, return this product immediately.

 --More--   
I hit the space bar and this causes another timeout error.
I have truncated the output this time:
Error:
Traceback (most recent call last): File "<stdin>", line 1, in <module> .. pexpect.exceptions.TIMEOUT: Timeout exceeded. .. delayafterterminate: 0.1
I go around this problem by issueing a terminal length 0 which tells the target box to send all output:
>>> 
>>> child.sendline('term le 0')
term le 0
10
>>> child.expect('#')
0
>>> 
It is interesting to me that I issue an child.expect('#') (above) which gives no output but generates no error either. This command has no output it just does it.
Now I try my command again and it all comes out:
>>> child.sendline('sh ver')
sh ver
7
>>> child.expect('#')
term le 0
XXX-XXX-X#sh ver
Cisco IOS Software, C2600 Software (C2600-ADVIPSERVICESK9-M), Version 12.4(19), RELEASE SOFTWARE (fc1)

.. 35 lines removed
.. 35 lines removed

Configuration register is 0x2102

XXX-XXX-X#0
>>> 
Now something else happens which I don't entirely understand - below
I issue a child.expect('product') instead of child.expect('#').
But I get no output.
So then I enter child.expect('#'). No output
child.expect('#') a second time, and then we have the output.
There are several seconds between me manually entering these commands.
It is inconceivable that the router is taking this long to reply.
>>> 
>>> child.sendline('sh ver')
sh ver
7
>>> child.expect('product')
0
>>> child.expect('#')
0
>>> child.expect('#')
sh ver
Cisco IOS Software, C2600 Software (C2600-ADVIPSERVICESK9-M), Version 12.4(19), RELEASE SOFTWARE (fc1)

This product contains cryptographic features and is subject to United
..
Configuration register is 0x2102

XXX-XXX-X#0
>>> 
This last sequence above is confusing.
Is Pexpect a bit flaky?
Upgrading to Python 3.x is a mission for me for various resons.
@heiner55 - I have just realised it was you who advised to upgrade!
Is 3.x more stable/better?
I am going to enjoy this journey.....
Reply


Messages In This Thread
Pexpect baby steps - by slouw - May-22-2019, 03:49 AM
RE: Pexpect baby steps - by heiner55 - May-22-2019, 06:45 AM
RE: Pexpect baby steps - by buran - May-22-2019, 07:17 AM
RE: Pexpect baby steps - by slouw - May-22-2019, 11:14 AM
RE: Pexpect baby steps - by buran - May-22-2019, 11:23 AM
RE: Pexpect baby steps - by heiner55 - May-23-2019, 05:15 AM
RE: Pexpect baby steps - by slouw - May-23-2019, 07:45 AM
RE: Pexpect baby steps - by buran - May-23-2019, 09:19 AM
RE: Pexpect baby steps - by heiner55 - May-23-2019, 10:21 AM
Pexpect baby steps.... - by slouw - May-22-2019, 08:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Up Sorting Steps MoreMoney 19 1,306 Mar-31-2024, 10:59 AM
Last Post: Pedroski55
  Reading and storing a line of output from pexpect child eagerissac 1 4,511 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  Use pexpect to send user input alisha17 0 2,070 May-10-2022, 02:44 AM
Last Post: alisha17
  Not able to read the text using pexpect/expect Bipinjohnson 7 4,318 Jan-10-2022, 11:21 AM
Last Post: Bipinjohnson
  Sudden Problem with pexpect gw1500se 3 2,561 Nov-19-2021, 11:21 PM
Last Post: bowlofred
  How to use pexpect in python? tiho_bg 1 1,611 Oct-30-2021, 02:50 PM
Last Post: Yoriz
  Pexpect timesout before executing whole output eagerissac 0 1,617 Jun-23-2021, 03:30 AM
Last Post: eagerissac
  pexpect startup help korenron 2 3,614 Apr-27-2021, 07:23 AM
Last Post: korenron
  Problem with pexpect.exception.TimeOUT korenron 0 3,425 Apr-12-2021, 03:25 PM
Last Post: korenron
  Next steps for using API data Rebster 6 2,708 Oct-10-2020, 05:35 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020