Pagination handling with pexpect

In this article, we will examine the apparently easy task which is a retrieval of the show version command from the Cisco router. What could go wrong there, and how to fix it?

Let’s figure it out!

Workflow overview

Today, we just want to grab a full output of the show version command from the Cisco router.

Seems easy at the start, right? But when we log in and issue the show version, we can observe an obstacle – the –More– at the end of each output portion, which requires us to press space, to get the next part of the result.

Router#show version
Cisco IOS XE Software, Version 17.XX.XX
Cisco IOS Software [XXX], ISR Software (XXX, Version 17.X.XX) RELEASE SOFTWARE (fc3)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2022 by Cisco Systems, Inc.
Compiled Wed XX-Apr-22 XX:XX by XXX


Cisco IOS-XE software, Copyright (c) 2005-2022 by cisco Systems, Inc.
All rights reserved.  Certain components of Cisco IOS-XE software are
licensed under the GNU General Public License ("GPL") Version 2.0.  The
software code licensed under GPL Version 2.0 is free software that comes
with ABSOLUTELY NO WARRANTY.  You can redistribute and/or modify such
GPL code under the terms of GPL Version 2.0.  For more details, see the
documentation or "License Notice" file accompanying the IOS-XE software,
or the applicable URL provided on the flyer accompanying the IOS-XE
software.


 --More--

We’re getting the full output after pressing space several times.

garzum@server:~$ ssh [email protected]
Password:



Router#show version
Cisco IOS XE Software, Version 17.XX.XX
Cisco IOS Software [XXX], ISR Software (XXX, Version 17.X.XX) RELEASE SOFTWARE (fc3)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2022 by Cisco Systems, Inc.
Compiled Wed XX-Apr-22 XX:XX by XXX


Cisco IOS-XE software, Copyright (c) 2005-2022 by cisco Systems, Inc.
All rights reserved.  Certain components of Cisco IOS-XE software are
licensed under the GNU General Public License ("GPL") Version 2.0.  The
software code licensed under GPL Version 2.0 is free software that comes
with ABSOLUTELY NO WARRANTY.  You can redistribute and/or modify such
GPL code under the terms of GPL Version 2.0.  For more details, see the
documentation or "License Notice" file accompanying the IOS-XE software,
or the applicable URL provided on the flyer accompanying the IOS-XE
software.


ROM: 17.X(XX)

Router uptime is 2 weeks, 1 day, 17 hours, 7 minutes
Uptime for this control processor is 2 weeks, 1 day, 17 hours, 8 minutes
System returned to ROM by PowerOn
System image file is "bootflash:packages.conf"
Last reload reason: PowerOn



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.

A summary of U.S. laws governing Cisco cryptographic products may be found at:
http://www.cisco.com/wwl/export/crypto/tool/stqrg.html

If you require further assistance please contact us by sending email to
export@cisco.com.



Suite License Information for Module:'esg'

--------------------------------------------------------------------------------
Suite                 Suite Current         Type           Suite Next reboot
--------------------------------------------------------------------------------
FoundationSuiteK9     None                  Smart License  None
securityk9
appxk9


Technology Package License Information:

-----------------------------------------------------------------
Technology    Technology-package           Technology-package
              Current       Type           Next reboot
------------------------------------------------------------------
appxk9           appxk9           Smart License    appxk9
uck9             uck9             Smart License    uck9
securityk9       securityk9       Smart License    securityk9
ipbase           ipbasek9         Smart License    ipbasek9

The current throughput level is unthrottled


Smart Licensing Status: Smart Licensing Using Policy

cisco C1113-8PMLTEEA (1RU) processor with 1355460K/6147K bytes of memory.
Processor board ID FGL2209945K
Router operating mode: Controller-Managed
1 Ethernet interface
6 Virtual Ethernet interfaces
9 Gigabit Ethernet interfaces
1 ATM interface
2 Cellular interfaces
32768K bytes of non-volatile configuration memory.
4194304K bytes of physical memory.
2945023K bytes of flash memory at bootflash:.
15478848K bytes of USB flash at usb0:.

Configuration register is 0x2102

Router#

That’s the effect that we want to achieve today. Let’s dive in!

Pagination handling

The solution to that problem seems to be pretty easy – write a while loop sending space until it gets a router prompt in the command output.

Let’s check that!

import pexpect

username = "admin"
password = "admin"
ip = "10.0.1.2"
prompt = "Router#"

child = pexpect.spawn(f"ssh {username}@{ip}")
child.expect("Password:")
child.sendline(password)
child.expect(prompt)
child.sendline("show version")

index = 99
pages_counter = 1
command_output = ""
while index != 0:
    index = child.expect([prompt, '--More--'])
    if index == 0:
        command_output += child.before.decode()
    elif index == 1:
        command_output += child.before.decode()
        child.sendline(' ')
    
    pages_counter += 1

print(command_output)
print(f"Number of pages - {pages_counter}")

The execution output is as follows.

show version
Cisco IOS XE Software, Version 17.XX.XX
Cisco IOS Software [XXX], ISR Software (XXX, Version 17.X.XX) RELEASE SOFTWARE (fc3)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2022 by Cisco Systems, Inc.
Compiled Wed XX-Apr-22 XX:XX by XXX


Cisco IOS-XE software, Copyright (c) 2005-2022 by cisco Systems, Inc.
All rights reserved.  Certain components of Cisco IOS-XE software are
licensed under the GNU General Public License ("GPL") Version 2.0.  The
software code licensed under GPL Version 2.0 is free software that comes
with ABSOLUTELY NO WARRANTY.  You can redistribute and/or modify such
GPL code under the terms of GPL Version 2.0.  For more details, see the
documentation or "License Notice" file accompanying the IOS-XE software,
or the applicable URL provided on the flyer accompanying the IOS-XE
software.


ROM: 17.X(XX)

pm3 uptime is 15 weeks, 6 days, 18 hours, 17 minutes
Uptime for this control processor is 15 weeks, 6 days, 18 hours, 18 minutes
System returned to ROM by PowerOn
System restarted at 20:04:05 UTC Mon Feb 13 2023
System image file is "bootflash:packages.conf"
Last reload reason: PowerOn



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.

A summary of U.S. laws governing Cisco cryptographic products may be found at:
http://www.cisco.com/wwl/export/crypto/tool/stqrg.html

If you require further assistance please contact us by sending email to
export@cisco.com.


        

Number of pages - 4

At first glance, it looks good, but if we take a closer look, we don’t have the full output. The response from the CLI ends in the config register section, but we can see that it’s not there.

The problem lies in the sendline command, that’s executed after each output part.

Anatomy of the sendline method

It’s a detail, but a sendline method is not only sending a string that’s passed as its argument but also new line characters. So in our case, it’s sending a space character, followed by the new line.

It’s making a difference, that results in the cropped command output.

There is however another method, that fits perfectly in our scenario. It’s send.

Pexpect’s send

As you can probably guess, the difference between sendline and send it’s the new line characters at the end of a sending command.

Send method sends just our command, without appending anything at the end.

Let’s adjust our script, to use send in the while loop.

import pexpect

username = "admin"
password = "admin"
ip = "10.0.1.2"
prompt = "Router#"

child = pexpect.spawn(f"ssh {username}@{ip}")
child.expect("Password:")
child.sendline(password)
child.expect(prompt)
child.sendline("show version")

index = 99
pages_counter = 1
command_output = ""
while index != 0:
    index = child.expect([prompt, '--More--'])
    if index == 0:
        command_output += child.before.decode()
    elif index == 1:
        command_output += child.before.decode()
        child.send(' ')
    
    pages_counter += 1

print(command_output)
print(f"Number of pages - {pages_counter}")

The code from this article is also available in my GitLab repository.

Now, we’re getting full output.

show version
Cisco IOS XE Software, Version 17.XX.XX
Cisco IOS Software [XXX], ISR Software (XXX, Version 17.X.XX) RELEASE SOFTWARE (fc3)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2022 by Cisco Systems, Inc.
Compiled Wed XX-Apr-22 XX:XX by XXX


Cisco IOS-XE software, Copyright (c) 2005-2022 by cisco Systems, Inc.
All rights reserved.  Certain components of Cisco IOS-XE software are
licensed under the GNU General Public License ("GPL") Version 2.0.  The
software code licensed under GPL Version 2.0 is free software that comes
with ABSOLUTELY NO WARRANTY.  You can redistribute and/or modify such
GPL code under the terms of GPL Version 2.0.  For more details, see the
documentation or "License Notice" file accompanying the IOS-XE software,
or the applicable URL provided on the flyer accompanying the IOS-XE
software.


ROM: 17.X(XX)

pm3 uptime is 15 weeks, 6 days, 18 hours, 44 minutes
Uptime for this control processor is 15 weeks, 6 days, 18 hours, 45 minutes
System returned to ROM by PowerOn
System restarted at 20:04:04 UTC Mon Feb 13 2023
System image file is "bootflash:packages.conf"
Last reload reason: PowerOn



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.

A summary of U.S. laws governing Cisco cryptographic products may be found at:
http://www.cisco.com/wwl/export/crypto/tool/stqrg.html

If you require further assistance please contact us by sending email to
export@cisco.com.


        
Suite License Information for Module:'esg' 

--------------------------------------------------------------------------------
Suite                 Suite Current         Type           Suite Next reboot     
--------------------------------------------------------------------------------
FoundationSuiteK9     None                  Smart License  None                  
securityk9
appxk9


Technology Package License Information: 

-----------------------------------------------------------------
Technology    Technology-package           Technology-package
              Current       Type           Next reboot  
------------------------------------------------------------------
appxk9           appxk9           Smart License    appxk9
uck9             uck9             Smart License    uck9
securityk9       securityk9       Smart License    securityk9
ipbase           ipbasek9         Smart License    ipbasek9
        
The current throughput level is unthrottled 


Smart Licensing Status: Smart Licensing Using Policy

cisco C1113-8PMLTEEA (1RU) processor with 1355460K/6147K bytes of memory.
Processor board ID FGL2209945K
Router operating mode: Controller-Managed
1 Ethernet interface
7 Virtual Ethernet interfaces
9 Gigabit Ethernet interfaces
1 ATM interface
2 Cellular interfaces
32768K bytes of non-volatile configuration memory.
4194304K bytes of physical memory.
2945023K bytes of flash memory at bootflash:.

Configuration register is 0x2102


Number of pages - 5

As you can see, now we have one more page, and of course a complete output.

Summary

Sometimes, seemingly simple tasks can give you a huge headache. On the other hand, the solutions to those can be just around the corner.

In the next Pexpect article, I’ll cover a troubleshooting method, that could help you identify, where lays a problem in such situations.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *