Skip to content

Commit 341f434

Browse files
committed
Got TextFSM to properly parse Huawei CLI output for both Classifier and Behavior. Woohoo!
1 parent 8041b3b commit 341f434

File tree

7 files changed

+91
-103
lines changed

7 files changed

+91
-103
lines changed

_pyparsing_play.py

Lines changed: 0 additions & 77 deletions
This file was deleted.

parse_behavior.templ

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
Value Filldown POLICY (\S+)
2-
Value CLASSIFIER (\S+)
3-
Value BEHAVIOR (\S+)
1+
Value Filldown policy (\S+)
2+
Value Required classifier (\S+)
3+
Value Required behavior (\S+)
44

55
Start
6-
^\s+Policy:\s${POLICY}
7-
^\s+Classifier:\s${CLASSIFIER}
8-
^\s+Behavior:\s${BEHAVIOR} -> Record
6+
^\s+Policy: ${policy}
7+
^\s+Classifier: ${classifier}
8+
^\s+Behavior: ${behavior} -> Record

parse_behavior.txt

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@
77
CIR 10500 (Kbps), CBS 775000 (Byte)
88
Conform Action : pass
99
Exceed Action : discard
10-
Classifier: vlan181_udp
11-
Operator: AND
12-
Behavior: agg_40M_udp
13-
Committed Access Rate:
14-
CIR 42000 (Kbps), CBS 3100000 (Byte)
15-
Conform Action : pass
16-
Exceed Action : discard
17-
car aggregation
1810

1911
Policy: Test_agg_40M
2012
Classifier: vlan181_tcp
@@ -33,12 +25,5 @@
3325
Conform Action : pass
3426
Exceed Action : discard
3527
car aggregation
36-
Classifier: 123456
37-
Operator: AND
38-
Behavior: 123456
39-
Committed Access Rate:
40-
CIR 10500 (Kbps), CBS 775000 (Byte)
41-
Conform Action : pass
42-
Exceed Action : discard
4328

4429
Total policy number is 2

parse_classifier.templ

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
Value CLASSIFIER (\S+)
2-
Value VLAN (\d+)
3-
1+
Value Required classifier (\S+)
2+
Value Required vlan (\d+)
43

54
Start
6-
^\s+Classifier:\s${CLASSIFIER}
7-
^\s+Rule\(s\)\s:\sif\-match\svlan\-id\s${VLAN} -> Record
5+
^\s+Classifier:\s${classifier}
6+
^\s+Rule\(s\) : if-match vlan-id ${vlan} -> Record
87

show_inventory.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import jtextfsm as textfsm
2+
3+
# Load the input file to a variable
4+
input_file = open("show_inventory.txt", encoding='utf-8')
5+
raw_text_data = input_file.read()
6+
input_file.close()
7+
8+
# Run the text through the FSM.
9+
# The argument 'template' is a file handle and 'raw_text_data' is a
10+
# string with the content from the show_inventory.txt file
11+
template = open("show_inventory_multiple.templ")
12+
re_table = textfsm.TextFSM(template)
13+
fsm_results = re_table.ParseText(raw_text_data)
14+
15+
# ...now all rows which were parsed by TextFSM
16+
for row in fsm_results:
17+
print(row)
18+
print("Write %d records" % len(fsm_results))

show_inventory.txt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
switch-1001#sh inventory
2+
NAME: "1", DESCR: "WS-C2960-24TT-L"
3+
PID: WS-C2960-24TT-L , VID: V09 , SN: 12345ABCD
4+
---------------------------------------------------------------------------------------------------
5+
switch-1002#sh inventory
6+
NAME: "1", DESCR: "WS-C2960-48TT-L"
7+
PID: WS-C2960-48TT-L , VID: V02 , SN: 12345ABCD
8+
---------------------------------------------------------------------------------------------------
9+
switch-2001#sh inventory
10+
NAME: "1", DESCR: "WS-C3750X-24"
11+
PID: WS-C3750X-24T-S , VID: V04 , SN: 12345ABCD
12+
NAME: "Switch 1 - Power Supply 0", DESCR: "FRU Power Supply"
13+
PID: C3KX-PWR-350WAC , VID: V02 , SN: 12345ABCD
14+
NAME: "Switch 1 - FRULink Slot 1 - FRULink Module", DESCR: "FRULink 1G Module"
15+
PID: C3KX-NM-1G , VID: V01 , SN: 12345ABCD
16+
NAME: "GigabitEthernet1/1/1", DESCR: "1000BaseLX SFP"
17+
PID: Unspecified , VID: , SN: 12345ABCD
18+
NAME: "2", DESCR: "WS-C3750X-24"
19+
PID: WS-C3750X-24T-S , VID: V04 , SN: 12345ABCD
20+
NAME: "Switch 2 - Power Supply 0", DESCR: "FRU Power Supply"
21+
PID: C3KX-PWR-350WAC , VID: V02 , SN: 12345ABCD
22+
NAME: "Switch 2 - FRULink Slot 1 - FRULink Module", DESCR: "FRULink 1G Module"
23+
PID: C3KX-NM-1G , VID: V01 , SN: 12345ABCD
24+
NAME: "GigabitEthernet2/1/1", DESCR: "1000BaseLX SFP"
25+
PID: Unspecified , VID: , SN: 12345ABCD
26+
NAME: "3", DESCR: "WS-C3750X-24"
27+
PID: WS-C3750X-24T-S , VID: V04 , SN: 12345ABCD
28+
NAME: "Switch 3 - Power Supply 0", DESCR: "FRU Power Supply"
29+
PID: C3KX-PWR-350WAC , VID: V02 , SN: 12345ABCD
30+
NAME: "Switch 3 - FRULink Slot 1 - FRULink Module", DESCR: "FRULink 1G Module"
31+
PID: C3KX-NM-1G , VID: V01 , SN: 12345ABCD
32+
NAME: "GigabitEthernet3/1/1", DESCR: "1000BaseLX SFP"
33+
PID: Unspecified , VID: , SN: 12345ABCD
34+
NAME: "4", DESCR: "WS-C3750X-24"
35+
PID: WS-C3750X-24T-S , VID: V04 , SN: 12345ABCD
36+
NAME: "Switch 4 - Power Supply 0", DESCR: "FRU Power Supply"
37+
PID: C3KX-PWR-350WAC , VID: V02 , SN: 12345ABCD
38+
NAME: "Switch 4 - FRULink Slot 1 - FRULink Module", DESCR: "FRULink 1G Module"
39+
PID: C3KX-NM-1G , VID: V01 , SN: 12345ABCD
40+
NAME: "GigabitEthernet4/1/1", DESCR: "1000BaseLX SFP"
41+
PID: Unspecified , VID: , SN: 12345ABCD
42+
---------------------------------------------------------------------------------------------------
43+
switch-2002#sh inventory
44+
NAME: "1", DESCR: "WS-C3560G-24TS"
45+
PID: WS-C3560G-24TS-S , VID: V03, SN: 12345ABCD
46+
NAME: "GigabitEthernet0/25", DESCR: "1000BaseSX SFP"
47+
PID: , VID: , SN: 12345ABCD
48+
NAME: "GigabitEthernet0/27", DESCR: "1000BaseSX SFP"
49+
PID: , VID: , SN: 12345ABCD
50+
NAME: "GigabitEthernet0/28", DESCR: "1000BaseSX SFP"
51+
PID: , VID: , SN: 12345ABCD
52+
---------------------------------------------------------------------------------------------------

show_inventory_multiple.templ

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Value Filldown hostname (\S+)
2+
Value name (.+)
3+
Value description (.*)
4+
Value productid (\S*)
5+
Value vid (\S*)
6+
Value Required serialnumber (\S+)
7+
8+
Start
9+
^${hostname}[>#].*
10+
^NAME: "${name}", DESCR: "${description}"
11+
^PID: ${productid}.*VID: ${vid}.*SN: ${serialnumber} -> Record

0 commit comments

Comments
 (0)