1

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.

2 Answers 2

1

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

 r'.*SN: (\S+)$'
Sign up to request clarification or add additional context in comments.

Comments

1

Another solution, without groups (Regex101):

(?<=SN ).*

This will match everything that is after SN until end of line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.