Search code examples
regexcisco

getting the serial number of the Cisco device using regex


Folks, checking if anyone has better suggestions to get a serial number from a Cisco device using a regex.

The "show inventory" file will looks something like the below:


switch-01#sh inventory
NAME c93xx Stack, DESCR c93xx Stack
PID C9300-48P         , VID V02  , SN **ABC1979G123**

NAME Switch 1, DESCR C9300-48P
PID C9300-48P         , VID V02  , SN ABC1979G123

NAME Switch 1 - Power Supply A, DESCR Switch 1 - Power Supply A
PID PWR-C1-715WAC-P   , VID V01  , SN POW1978ST01

NAME Switch 1 - Power Supply B, DESCR Switch 1 - Power Supply B
PID PWR-C1-715WAC-P   , VID V01  , SN POW1978ST02

NAME Switch 1 FRU Uplink Module 1, DESCR 4x1G Uplink Module
PID C9300-NM-4G       , VID V01  , SN FUM2023CIOC

NAME Gi111, DESCR 1000BaseSX SFP
PID GLC-SX-MMD          , VID V01  , SN GBIC2020C01

NAME Gi112, DESCR 1000BaseSX SFP
PID GLC-SX-MMD          , VID V01  , SN GBIC2020C02


switch-01#

For any Cisco device the highlighted is always the serial number. (that will be ABC1979G123)

The regex I have is as below: r'PID: \S+\s+, VID:\s+\S+\s+, SN: (\S+)'

This works well, but I am looking to see if there are any recommendation on some simple regex.

Thanks!!

I have mentioned what I tried, and this works. Again, just looking at suggestions on some simpler approach.


Solution

  • It seems SN is always the last element on the line. If so, perhaps this would be simpler:

     r'.*SN: (\S+)$'