使用testfsm库提取网络设备信息

PY代码

1
2
3
4
5
6
7
8
9
from textfsm import TextFSM

with open("rule.txt") as rule:
template = TextFSM(rule)
with open("1.log", encoding="utf8") as f:
logfile = f.read()
# ParseTextToDicts转为字典
result = template.ParseTextToDicts(logfile)
print(result)

匹配规则(rule.txt)

FSM规则在线编辑网站:https://textfsm.nornir.tech/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Value SYSNAME (\S+)
Value BRIDGEMAC (\S+)
Value VERSION (\S+ \S+)
Value List SN (\S+)
Value List PRODUCT (\S+)
Value List DEVICE (\S+)
Value IPADDRESS (172.31.6.\d+)
Value List transceiver (\S+)
Value List transceiver_sn (\S+)

Start
^ The bridge MAC of the IRF is[ :]+${BRIDGEMAC}
^\s*DEVICE_NAME[ :]+${DEVICE}
^\s*PRODUCT.ID[ :]+${PRODUCT}
^\s*DEVICE_SERIAL_NUMBER[ :]+${SN}
^H3C Comware Software, Version [0-9,.]+,\s+${VERSION}
^${transceiver} transceiver manufacture information: -> Transceiver
^ sysname ${SYSNAME}
^Vlan\d+\s+[UP ]+\s+${IPADDRESS}

Transceiver
^\s+Manu. Serial Number[ :]+${transceiver_sn} -> Start
^The operation is not ${transceiver_sn} -> Start
^The transceiver is ${transceiver_sn} -> Start

字段说明

Value:定义变量名,语法Value [option[,option...]] name regex

  • option - 选项位
    • Filldown:如果上一轮匹配已结束,变量没有明确指定要清除的话,该变量值会被带入下一轮;下一轮匹配过程中,这个变量如被匹配刷新,则用新的值;如未被匹配刷新,则该变量值会被继续带入再下一轮
    • Key:明确该字段包含唯一的字符串标识
    • Required:一轮匹配过程,该变量必须匹配到,其结果才会被记录
    • List:TextFSM模板默认处理过程是下次匹配会覆盖掉上一轮的匹配值,如果使用了List选项值,则每次匹配会以列表追加元素的形式,往列表尾部逐一追加匹配值。
    • Fillup:与Filldown相似,但往上填充,直到变量值非空。该选项值与Required互斥。
  • name - 变量名
  • regex - 变量正则表达式:一般用()包裹

Start/Transceiver:定义状态名,接下来的每一行就规则(rules)

  • 保留状态:TextFSM模板有3个保留状态
    • Start - 这个状态必须指定,否则模板无法工作。
    • End - 这个状态表明输入的字符串已经完成处理了,但此后不执行”EOF”状态。
    • EOF - 这个状态是隐式指定,”Start”状态开始后,按照规则(rules)处理数据,处理到文末,自动就到达了”EOF”状态。
  • 自定义状态:可以使用自定义状态来实现跨行匹配,如文中的Transceiver状态,用来处理光模块信息,可以使用->进行状态切换。

规则明细:语法 ^regex [-> action]

状态定义后,每个状态都会包含一条或多条规则。

  • TextFSM接收输入字符串后,依据逐条规则处理该字符串。
  • 一旦某条规则匹配,则该规则中的动作(默认是next)将被执行,接着TextFSM接收新的字符串,并从头开始匹配,重复执行。
  • 每条规则必须以两个空格和^脱字符开始,脱字符^必须明确指出来。
  • 正则表达式中可以嵌入变量。
    • 指定变量有两种形式:$ValueName 和 ${ValueName} (推荐用这种)。
    • 在规则中,你放入的变量会被替换成定义它的正则表达式。
    • 如果你想明确指定行尾,则可以使用$$。

在规则后面,仅接着就是动作。

  • 动作与规则之间,必须由 -> 符号进行连接。
  • 动作可以分成三类,简单标记为 L.R.S
    • L - LineAction : 这个动作用来控制待匹配字符串(即输入的字符串)。
    • R - RecordAction : 这个动作用来控制如何收集已定义的变量。
    • S - StateTransition : 这个动作用来控制状态的跳转。
  • 我们啥都不写的话,缺省情况下,动作为 Next.NoRecord。
    • Next 是针对待匹配字符串(输入的字符串),下一行。
    • NoRecord 是针对前面定义的变量,不提取不记录。

动作L.R.S详解

  • Line Actions 这是处理待匹配文本的,待匹配文本是一行一行进来的,所以叫行(Line)动作。

    • Next - 如果规则匹配中了,读取下一行待匹配字符串,整个匹配规则重新回归第一行开始往下匹配。如不明确指定,默认就是这个动作。
    • Continue - 如果规则匹配中了,在“处理待匹配字符串(输入的字符串)”这个维度上,当作没匹配中进行处理,该干嘛还干嘛,该匹配下一行规则就继续匹配。(假装啥事都没发生。)
  • Record Action 这是处理变量的。我们可以在Line动作后指定Record动作,用一个点号进行连接。

    • NoRecord - 啥事不干。这是一个默认动作。
    • Record - 变量结果执行提取记录,之后所有变量重置(不包括带Filldown选项的变量)。
    • Clear - 变量结果不执行提取记录,所有变量重置(不包括带Filldown选项的变量)。
    • Clearall - 变量结果不执行提取记录,所有变量重置。

在一行规则匹配后,倘若需要同时执行Line动作和Record动作,此时就需要用点号连接,如Continue.Record;倘若只需要执行其中一个动作,就不需要点符号了。

  • State Transition 这是处理状态跳转的。
    • 所跳转的状态必须是TextFSM预设或用户自定义的,不能跳转到一个未定义的状态去。
    • 如果待匹配字符串(输入字符串)命中规则
      • 所有动作都执行。
      • 读取下一行待输入文本。
      • 当前状态跳转至指向的状态,并继续执行匹配。

如果规则包含了Continue动作,则这规则内无法进行状态跳转。另外设置跳转规则的时候,需要避免状态间来回跳转,进入死循环。

  • Error Action 这是处理错误的。
    • Error 发生错误时候,停止所有行处理,丢弃所有变量收集,返回一个错误表达式。其语法如下:
1
^regex -> Error [word|"string"]

参考网站:网络工程师 Python TextFSM 模板(第2节,TextFSM,模板语法,略读即可) - 知乎 (zhihu.com)

正则表达式

正则表达式详情链接:正则表达式 – 教程 | 菜鸟教程 (runoob.com)

实例 描述
[Pp]ython 匹配 “Python” 或 “python”
rub[ye] 匹配 “ruby” 或 “rube”
[aeiou] 匹配中括号内的任意一个字母
[0-9] 匹配任何数字。类似于 [0123456789]
[a-z] 匹配任何小写字母
[A-Z] 匹配任何大写字母
[a-zA-Z0-9] 匹配任何字母及数字
[^aeiou] 除了aeiou字母以外的所有字符
[^0-9] 匹配除了数字外的字符
. 匹配除 “\n” 之外的任何单个字符。要匹配包括 ‘\n’ 在内的任何字符,请使用象 [.\n] 的模式。
* 匹配*之前的字符出现0次或任意次数。
+ 匹配+之前的字符出现1次或任意次数。
\d 匹配一个数字字符。等价于 [0-9]。
\D 匹配一个非数字字符。等价于 [^0-9]。
\s 匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]。
\S 匹配任何非空白字符。等价于 [^ \f\n\r\t\v]。
\w 匹配包括下划线的任何单词字符。等价于 [A-Za-z0-9_]。
\W 匹配任何非单词字符。等价于 [^A-Za-z0-9_]。

匹配结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[
{
"BRIDGEMAC": "642f-c702-6328",
"DEVICE": [
"S5130S-28P-EI"
],
"IPADDRESS": "172.31.6.238",
"PRODUCT": [
"LS-5130S-28P-EI-H1"
],
"SN": [
"219801A2SH921BQ007XS"
],
"SYSNAME": "wkl_Ajr_2",
"VERSION": "Release 6328P03",
"transceiver": [
"GigabitEthernet1/0/25",
"GigabitEthernet1/0/26",
"GigabitEthernet1/0/27",
"GigabitEthernet1/0/28"
],
"transceiver_sn": [
"210231A962X21B002C42",
"absent.",
"absent.",
"absent."
]
}
]

原始数据文件(1.log)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364

******************************************************************************
* Copyright (c) 2004-2021 New H3C Technologies Co., Ltd. All rights reserved.*
* Without the owner's prior written consent, *
* no decompiling or reverse-engineering shall be allowed. *
******************************************************************************

<wkl_Ajr_2>
<wkl_Ajr_2>scr dis
<wkl_Ajr_2>dis irf
MemberID Role Priority CPU-Mac Description
*+1 Master 1 f010-90db-7402 ---
--------------------------------------------------
* indicates the device is the master.
+ indicates the device through which the user logs in.

The bridge MAC of the IRF is: 642f-c702-6328
Auto upgrade : yes
Mac persistent : 6 min
Domain ID : 0
<wkl_Ajr_2>dis version
H3C Comware Software, Version 7.1.070, Release 6328P03
Copyright (c) 2004-2021 New H3C Technologies Co., Ltd. All rights reserved.
H3C S5130S-28P-EI uptime is 3 weeks, 2 days, 4 hours, 11 minutes
Last reboot reason : Cold reboot

Boot image: flash:/s5130s_ei-cmw710-boot-r6328p03.bin
Boot image version: 7.1.070, Release 6328P03
Compiled Jul 12 2021 11:00:00
System image: flash:/s5130s_ei-cmw710-system-r6328p03.bin
System image version: 7.1.070, Release 6328P03
Compiled Jul 12 2021 11:00:00


Slot 1:
Uptime is 3 weeks,2 days,4 hours,11 minutes
S5130S-28P-EI with 1 Processor
BOARD TYPE: S5130S-28P-EI
DRAM: 512M bytes
FLASH: 256M bytes
PCB 1 Version: VER.B
Bootrom Version: 141
CPLD 1 Version: 002
Release Version: H3C S5130S-28P-EI-6328P03
Patch Version : None
Reboot Cause : ColdReboot
[SubSlot 0] 24GE+4SFP
<wkl_Ajr_2>dis dev
Slot Type State Subslot Soft Ver Patch Ver
1 S5130S-28P-EI Master 0 S5130S_EI-6328P03 None
<wkl_Ajr_2>dis dev man
Slot 1 CPU 0:
DEVICE_NAME : S5130S-28P-EI
DEVICE_SERIAL_NUMBER : 219801A2SH921BQ007XS
MAC_ADDRESS : 642F-C702-6328
MANUFACTURING_DATE : 2021-11-18
VENDOR_NAME : H3C
PRODUCT_ID : LS-5130S-28P-EI-H1
Power 1:
The operation is not supported on the specified power.
<wkl_Ajr_2>dis lldp nei list
Chassis ID : * -- -- Nearest nontpmr bridge neighbor
# -- -- Nearest customer bridge neighbor
Default -- -- Nearest bridge neighbor
Local Interface Chassis ID Port ID System Name
GE1/0/25 40fe-9510-0c46 GigabitEthernet1/0/3 wkl_a
<wkl_Ajr_2>dis tran man int
GigabitEthernet1/0/25 transceiver manufacture information:
Manu. Serial Number : 210231A962X21B002C42
Manufacturing Date : 2021-12-01
Vendor Name : H3C
GigabitEthernet1/0/26 transceiver manufacture information:
The transceiver is absent.
GigabitEthernet1/0/27 transceiver manufacture information:
The transceiver is absent.
GigabitEthernet1/0/28 transceiver manufacture information:
The transceiver is absent.
<wkl_Ajr_2>dis cur
#
version 7.1.070, Release 6328P03
#
sysname wkl_Ajr_2
#
clock timezone beijing add 08:00:00
#
irf mac-address persistent timer
irf auto-update enable
undo irf link-delay
irf member 1 priority 1
#
lldp global enable
#
password-recovery enable
#
vlan 1
#
vlan 87
#
vlan 3001
description wkl_A
#
vlan 3006
description SW_manage
#
stp global enable
#
interface Bridge-Aggregation1
port link-type trunk
port trunk permit vlan all
dhcp snooping trust
#
interface NULL0
#
interface Vlan-interface1
ip address dhcp-alloc
dhcp client identifier ascii 642fc7026328-VLAN0001
#
interface Vlan-interface3006
description SW_guanli
ip address 172.31.6.238 255.255.255.0
#
interface GigabitEthernet1/0/1
port access vlan 3001
stp edged-port
#
interface GigabitEthernet1/0/2
port access vlan 3001
stp edged-port
#
interface GigabitEthernet1/0/3
port access vlan 3001
stp edged-port
#
interface GigabitEthernet1/0/4
port access vlan 3001
stp edged-port
#
interface GigabitEthernet1/0/5
port access vlan 3001
stp edged-port
#
interface GigabitEthernet1/0/6
port access vlan 3001
stp edged-port
#
interface GigabitEthernet1/0/7
port access vlan 3001
stp edged-port
#
interface GigabitEthernet1/0/8
port access vlan 3001
stp edged-port
#
interface GigabitEthernet1/0/9
port access vlan 3001
stp edged-port
#
interface GigabitEthernet1/0/10
port access vlan 3001
stp edged-port
#
interface GigabitEthernet1/0/11
port access vlan 3001
stp edged-port
#
interface GigabitEthernet1/0/12
port access vlan 3001
stp edged-port
#
interface GigabitEthernet1/0/13
port access vlan 3001
stp edged-port
#
interface GigabitEthernet1/0/14
port access vlan 3001
stp edged-port
#
interface GigabitEthernet1/0/15
port access vlan 3001
stp edged-port
#
interface GigabitEthernet1/0/16
port access vlan 3001
stp edged-port
#
interface GigabitEthernet1/0/17
port access vlan 3001
stp edged-port
#
interface GigabitEthernet1/0/18
port access vlan 3001
stp edged-port
#
interface GigabitEthernet1/0/19
port access vlan 3001
stp edged-port
#
interface GigabitEthernet1/0/20
port access vlan 3001
stp edged-port
#
interface GigabitEthernet1/0/21
port access vlan 3001
stp edged-port
#
interface GigabitEthernet1/0/22
port access vlan 3001
stp edged-port
#
interface GigabitEthernet1/0/23
port access vlan 3001
stp edged-port
#
interface GigabitEthernet1/0/24
port access vlan 3001
stp edged-port
#
interface GigabitEthernet1/0/25
port link-type trunk
port trunk permit vlan all
port link-aggregation group 1
#
interface GigabitEthernet1/0/26
port link-type trunk
port trunk permit vlan all
port link-aggregation group 1
#
interface GigabitEthernet1/0/27
#
interface GigabitEthernet1/0/28
#
scheduler logfile size 16
#
line class aux
user-role network-admin
#
line class vty
user-role network-operator
#
line aux 0
user-role network-admin
#
line vty 0 4
authentication-mode scheme
user-role network-operator
#
line vty 5 63
user-role network-operator
#
ip route-static 0.0.0.0 0 172.31.6.254
#
ssh server enable
ssh user admin service-type stelnet authentication-type password
#
ntp-service enable
ntp-service unicast-server 192.168.85.12
#
radius scheme system
user-name-format without-domain
#
domain system
#
domain default enable system
#
role name level-0
description Predefined level-0 role
#
role name level-1
description Predefined level-1 role
#
role name level-2
description Predefined level-2 role
#
role name level-3
description Predefined level-3 role
#
role name level-4
description Predefined level-4 role
#
role name level-5
description Predefined level-5 role
#
role name level-6
description Predefined level-6 role
#
role name level-7
description Predefined level-7 role
#
role name level-8
description Predefined level-8 role
#
role name level-9
description Predefined level-9 role
#
role name level-10
description Predefined level-10 role
#
role name level-11
description Predefined level-11 role
#
role name level-12
description Predefined level-12 role
#
role name level-13
description Predefined level-13 role
#
role name level-14
description Predefined level-14 role
#
user-group system
#
local-user admin class manage
password hash $h$6$8V0HpjzG/QlEsPfI$LAvLSKJ3CDVPQtfTkcbe9eV+c4Npr3mIXBlZzKlpCzqqhEXIcEOCAZFzN9wjH4LXT49fTjE9yx5LLiztyv/9fg==
service-type telnet ssh
authorization-attribute user-role level-15
authorization-attribute user-role network-admin
authorization-attribute user-role network-operator
#
return
<wkl_Ajr_2>dis int brief
Brief information on interfaces in route mode:
Link: ADM - administratively down; Stby - standby
Protocol: (s) - spoofing
Interface Link Protocol Primary IP Description
InLoop0 UP UP(s) --
NULL0 UP UP(s) --
Vlan1 UP UP 169.254.99.40
Vlan3006 UP UP 172.31.6.238 SW_guanli

Brief information on interfaces in bridge mode:
Link: ADM - administratively down; Stby - standby
Speed: (a) - auto
Duplex: (a)/A - auto; H - half; F - full
Type: A - access; T - trunk; H - hybrid
Interface Link Speed Duplex Type PVID Description
BAGG1 UP 1G(a) F(a) T 1
GE1/0/1 DOWN auto A A 3001
GE1/0/2 DOWN auto A A 3001
GE1/0/3 DOWN auto A A 3001
GE1/0/4 DOWN auto A A 3001
GE1/0/5 DOWN auto A A 3001
GE1/0/6 DOWN auto A A 3001
GE1/0/7 UP 100M(a) F(a) A 3001
GE1/0/8 DOWN auto A A 3001
GE1/0/9 DOWN auto A A 3001
GE1/0/10 DOWN auto A A 3001
GE1/0/11 DOWN auto A A 3001
GE1/0/12 DOWN auto A A 3001
GE1/0/13 UP 100M(a) F(a) A 3001
GE1/0/14 DOWN auto A A 3001
GE1/0/15 UP 100M(a) F(a) A 3001
GE1/0/16 UP 100M(a) F(a) A 3001
GE1/0/17 UP 100M(a) F(a) A 3001
GE1/0/18 UP 100M(a) F(a) A 3001
GE1/0/19 UP 100M(a) F(a) A 3001
GE1/0/20 UP 100M(a) F(a) A 3001
GE1/0/21 UP 100M(a) F(a) A 3001
GE1/0/22 DOWN auto A A 3001
GE1/0/23 DOWN auto A A 3001
GE1/0/24 DOWN auto A A 3001
GE1/0/25 UP 1G(a) F(a) T 1
GE1/0/26 DOWN auto A T 1
GE1/0/27 DOWN auto A A 1
GE1/0/28 DOWN auto A A 1


使用testfsm库提取网络设备信息
https://www.intx.work/posts/41b22435.html
发布于
2024年6月28日
更新于
2025年7月5日
许可协议