Question

I need help getting the firmware version from the output of the Cisco "show version" command

The following is the first line of the show version output (where "12.4(21a)JA1" is the firmware version):

Cisco IOS Software, C1240 Software (C1240-K9W7-M), Version 12.4(21a)JA1, RELEASE
SOFTWARE (fc1)

The below code gives me the error: couldn't compile regular expression pattern: quantifier operand invalid

expect "*#" {send "show version\n"}
expect -re "(?<=Version/s)(.*)(?=/sRELEASE)" {set var1 $expect_out(1,string)}
puts "Firmware Version: $var1"

Thanks for the help

Was it helpful?

Solution

Your problem is two-fold. Firstly, Expect doesn't support look-behind constraints (well, not under normal builds). Secondly, your regular expression mixes up what sort of slashes it is using. It's always a good idea to put regular expresions in braces in Tcl and (by extension) Expect. I also recommend using the multi-line version of expect (as shown below) when things get longer, as that's much easier on the eye.

expect "*#" {send "show version\n"}
expect {
    -re {Version\s+(.*),\s+RELEASE} {
        set firmwareVersion $expect_out(1,string)
    }
}
puts "Firmware Version: $firmwareVersion"

The only downside of putting things in braces is that SO formats them wrongly. We can survive such hardship, I think…

OTHER TIPS

Original:

expect "*#" {send "show version\n"}
expect -re "(?<=Version/s)(.*)(?=/sRELEASE)" {set var1 $expect_out(1,string)}
puts "Firmware Version: $var1"

First, as Donal mentioned expect doesn't support look-behind regexps...

Also, I think you'll find it a little challenging to match a string and perform variable substitution while you are interacting. It's quite possible, but it's easier to do this...

Suggested:

expect "*#" {send "show version\n"}
expect "*#" {send "# something else here"}
regexp {Version\s(\d.+?),\sRELEASE\sSOFTWARE} $expect_out(buffer) matched var1
puts "Firmware Version: $var1"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top