Search code examples
regextclexpectciscocisco-ios

Regex to retrieve firmware version number from Cisco IOS `show version`


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


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…