使用正则表达式获取Cisco设备的序列号。

huangapple go评论56阅读模式
英文:

getting the serial number of the Cisco device using regex

问题

各位,
检查是否有更好的建议来使用正则表达式从 Cisco 设备中获取序列号。

“show inventory” 文件看起来会像下面这样:

*************************************************************************

    switch-01#sh inventory
    名称 c93xx 堆叠,描述 c93xx 堆叠
    产品号 C9300-48P         , 版本 V02  , 序列号 **ABC1979G123**
    
    名称 Switch 1, 描述 C9300-48P
    产品号 C9300-48P         , 版本 V02  , 序列号 ABC1979G123
    
    名称 Switch 1 - 电源 A, 描述 Switch 1 - 电源 A
    产品号 PWR-C1-715WAC-P   , 版本 V01  , 序列号 POW1978ST01
    
    名称 Switch 1 - 电源 B, 描述 Switch 1 - 电源 B
    产品号 PWR-C1-715WAC-P   , 版本 V01  , 序列号 POW1978ST02
    
    名称 Switch 1 FRU 上行模块 1, 描述 4x1G 上行模块
    产品号 C9300-NM-4G       , 版本 V01  , 序列号 FUM2023CIOC
    
    名称 Gi111, 描述 1000BaseSX SFP
    产品号 GLC-SX-MMD          , 版本 V01  , 序列号 GBIC2020C01
    
    名称 Gi112, 描述 1000BaseSX SFP
    产品号 GLC-SX-MMD          , 版本 V01  , 序列号 GBIC2020C02
    
    
    switch-01#

*************************************************************************

对于任何 Cisco 设备,高亮显示部分始终是序列号。
(在这里将是 ABC1979G123)

我使用的正则表达式如下:
r'PID: \S+\s+, VID:\s+\S+\s+, SN: (\S+)'

这很有效,但我想知道是否有关于一些简单正则表达式的建议。

谢谢!

我已经提到了我尝试过的方法,它有效。
再次,只是在寻找一些更简单的方法的建议。
英文:

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.

答案1

得分: 1

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

英文:

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

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

答案2

得分: 1

这将匹配在 SN 之后直到行末的所有内容。

英文:

Another solution, without groups (Regex101):

(?<=SN ).*

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

huangapple
  • 本文由 发表于 2023年7月31日 22:44:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804730.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定