May-23-2019, 07:45 AM
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('#'):
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:
I have truncated the output this time:
Now I try my command again and it all comes out:
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.
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.....
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.....