0

When I run 'show version' on a Cisco switch I get the following output:

Cisco IOS Software, C3750E Software (C3750E-UNIVERSALK9-M), Version 12.2(58)SE2, RELEASE SOFTWARE (fc1) Technical Support: http://www.cisco.com/techsupport Copyright (c) 1986-2011 by Cisco Systems, Inc.

<--output truncated-->

#

I am using Expect to login to the switch, run the show version command, and expect the complete output of that command and the exact version, which I can then output to the screen using the code below:

send "show version\n"
expect -re "show version.*Version (.*), REL.*#$"
send_user "Command Output:\n$expect_out(0,string)\n\n"
send_user "Version:\n$expect_out(1,string)\n\n"

This all works OK, however I am now trying to replicate this using Python and Pexpect. I can get the equivalent of $expect_out(0,string) using child.before:

child.sendline(show version')
child.expect('#')
print("\r\n","Command Output:","\r\n",child.before, sep = '')

How do I replicate $expect_out(1,string) in Pexpect to get the exact version?

Many thanks in advance

farrier
  • 3
  • 3

1 Answers1

1

The Pexpect package does rather less than Expect, and this is one of the areas where it is critically different as there is no exposed access to the relevant match objects.

You'll need to use a separate RE to extract the part that you care about.

import re

child.sendline(show version')
child.expect('#')
print("\r\n","Command Output:","\r\n",child.before, sep = '')

m = re.search("show version.*Version (.*), REL.", child.before)
if m:
    print("Version:\n" + m.group(1) + "\n\n")
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215