Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pexpect baby steps
#1
I did say baby steps.
I know I am logging in ok as I can do a show users on the target box and confirm that.
I want to see the output of my command sh clock but am getting a time out.
What am I doing wrong?


>>> import pexpect
>>> child=pexpect.spawn("ssh XXX-XXX-7")
>>> child.expect('Password:')
0
>>> child.sendline('1234567890ab')
13
>>> child.expect('#')
0
>>> child.sendline('sh clock')
9
>>> child.expect('#')
0
>>> print child.read()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/pexpect/spawnbase.py", line 407, in read
self.expect(self.delimiter)
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 0x7fe471eb3310>
command: /usr/bin/ssh
args: ['/usr/bin/ssh', 'XXX-XXX-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: 31253
child_fd: 6
closed: False
timeout: 30
delimiter: <class 'pexpect.exceptions.EOF'>
logfile: None
logfile_read: None
logfile_send: None
maxread: 2000
ignorecase: False
searchwindowsize: None
delaybeforesend: 0.05
delayafterclose: 0.1
delayafterterminate: 0.1
>>>
Reply
#2
You should use Python 3.x instead of Python 2.7
Reply
#3
@slouw, please, use BBCode when post code, traceback, output.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
@buran happy to repost if you delete this one?
I cant find an edit button...
Reply
#5
Just post here. I can add them for you, but there are many [b] tags to remove
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
I did say baby steps.
I know I am logging in ok as I can do a show users on the target box and confirm that.
I want to see the output of my command sh clock but am getting a time out.
What am I doing wrong?


>>> import pexpect
>>> child=pexpect.spawn("ssh XXX-XXX-7")
>>> child.expect('Password:')
0
>>> child.sendline('1234567890ab')
13
>>> child.expect('#')
0
>>> child.sendline('sh clock')
9
>>> child.expect('#')
0
>>> print child.read()
Error:
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/dist-packages/pexpect/spawnbase.py", line 407, in read self.expect(self.delimiter) 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 0x7fe471eb3310> command: /usr/bin/ssh args: ['/usr/bin/ssh', 'XXX-XXX-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: 31253 child_fd: 6 closed: False timeout: 30 delimiter: <class 'pexpect.exceptions.EOF'> logfile: None logfile_read: None logfile_send: None maxread: 2000 ignorecase: False searchwindowsize: None delaybeforesend: 0.05 delayafterclose: 0.1 delayafterterminate: 0.1 >>>
Reply
#7
I think your server "xxx-xxx-7" does not work like you expect
because you are getting a timeout:
Output:
pexpect.exceptions.TIMEOUT: Timeout exceeded.
Maybe your server asks you for a user before it wants a password.
Reply
#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
#9
(May-23-2019, 07:45 AM)slouw Wrote: Is 3.x more stable/better?
I am going to enjoy this journey.....
If not for something else, official support for python2 ends 1.1.2020
py3 is the future, it's stable and mature for long time now
see http://py3readiness.org/
356 out of top 360 packages (most downloaded packages on PyPI) are py3 ready
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
Glad to hear, your program works now better.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Up Sorting Steps MoreMoney 19 781 Mar-31-2024, 10:59 AM
Last Post: Pedroski55
  Reading and storing a line of output from pexpect child eagerissac 1 4,249 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  Use pexpect to send user input alisha17 0 1,890 May-10-2022, 02:44 AM
Last Post: alisha17
  Not able to read the text using pexpect/expect Bipinjohnson 7 4,046 Jan-10-2022, 11:21 AM
Last Post: Bipinjohnson
  Sudden Problem with pexpect gw1500se 3 2,394 Nov-19-2021, 11:21 PM
Last Post: bowlofred
  How to use pexpect in python? tiho_bg 1 1,531 Oct-30-2021, 02:50 PM
Last Post: Yoriz
  Pexpect timesout before executing whole output eagerissac 0 1,500 Jun-23-2021, 03:30 AM
Last Post: eagerissac
  pexpect startup help korenron 2 3,493 Apr-27-2021, 07:23 AM
Last Post: korenron
  Problem with pexpect.exception.TimeOUT korenron 0 3,300 Apr-12-2021, 03:25 PM
Last Post: korenron
  Next steps for using API data Rebster 6 2,538 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