From 3f166ce666b214bfd0db2b093d8783db17fee8d7 Mon Sep 17 00:00:00 2001 From: Sharky Date: Wed, 27 Sep 2023 14:55:15 +0200 Subject: [PATCH 1/4] Make get_vlans name whitespace aware , see #1789 --- napalm/ios/ios.py | 6 +++--- .../mocked_data/test_get_vlans/normal/expected_result.json | 7 ++++++- .../test_get_vlans/normal/show_vlan_all_ports.txt | 4 +++- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/napalm/ios/ios.py b/napalm/ios/ios.py index df7a78400..048b3abdb 100644 --- a/napalm/ios/ios.py +++ b/napalm/ios/ios.py @@ -3722,7 +3722,7 @@ def get_vlans(self): return self._get_vlan_all_ports(output) def _get_vlan_all_ports(self, output): - find_regexp = re.compile(r"^(\d+)\s+(\S+)\s+\S+(\s+[A-Z][a-z].*)?$") + find_regexp = re.compile(r"^(\d+)\s+(.*?(?=active|act\/[isl]{1}shut|act\/unsup))\w+(?:\/\w+)?\S+(\s+[A-Z][a-z].*)?$") continuation_regexp = re.compile(r"^\s+([A-Z][a-z].*)$") output = output.splitlines() vlans = {} @@ -3736,7 +3736,7 @@ def _get_vlan_all_ports(self, output): if vlan_m: was_vlan_or_cont = True vlan_id = vlan_m.group(1) - vlan_name = vlan_m.group(2) + vlan_name = vlan_m.group(2).strip() interfaces = vlan_m.group(3) or "" vlans[vlan_id] = {"name": vlan_name, "interfaces": []} @@ -3763,7 +3763,7 @@ def _get_vlan_all_ports(self, output): def _get_vlan_from_id(self): command = "show vlan brief" output = self._send_command(command) - vlan_regexp = r"^(\d+)\s+(\S+)\s+\S+.*$" + vlan_regexp = r"^(\d+)\W+(.*?(?=active|act\/[isl]{1}shut|act\/unsup))" find_vlan = re.findall(vlan_regexp, output, re.MULTILINE) vlans = {} for vlan_id, vlan_name in find_vlan: diff --git a/test/ios/mocked_data/test_get_vlans/normal/expected_result.json b/test/ios/mocked_data/test_get_vlans/normal/expected_result.json index 9724b801b..96c41b948 100644 --- a/test/ios/mocked_data/test_get_vlans/normal/expected_result.json +++ b/test/ios/mocked_data/test_get_vlans/normal/expected_result.json @@ -1112,7 +1112,7 @@ ] }, "795": { - "name": "Vlan795", + "name": "Vlan 795", "interfaces": [ "Port-channel1", "Port-channel5", @@ -1194,5 +1194,10 @@ "1275": { "name": "Vlan1275", "interfaces": [] + }, + "1276": { + "name": "A1 - VIDEO", + "interfaces": ["Port-channel1"] } + } diff --git a/test/ios/mocked_data/test_get_vlans/normal/show_vlan_all_ports.txt b/test/ios/mocked_data/test_get_vlans/normal/show_vlan_all_ports.txt index 5f9850dd7..dd44f4549 100644 --- a/test/ios/mocked_data/test_get_vlans/normal/show_vlan_all_ports.txt +++ b/test/ios/mocked_data/test_get_vlans/normal/show_vlan_all_ports.txt @@ -64,12 +64,13 @@ VLAN Name Status Ports 790 Vlan790 active Po1, Po5, Po6, Po21, Po22, Po23, Po24, Po25, Po26, Po27, Po28, Po30 792 Vlan792 active Po1, Po2, Po5, Po21, Po22, Po23, Po24, Po25, Po26, Po27, Po28, Po30 794 Vlan794 active Po1, Po5, Po6, Po21, Po22, Po23, Po24, Po25, Po26, Po27, Po28, Po30 -795 Vlan795 active Po1, Po5, Po21, Po22, Po23, Po24, Po25, Po26, Po27, Po28, Po30 +795 Vlan 795 active Po1, Po5, Po21, Po22, Po23, Po24, Po25, Po26, Po27, Po28, Po30 1002 Vlan1002 act/unsup Po1, Po5, Po21, Po22, Po23, Po24, Po25, Po26, Po27, Po28, Po30 1003 Vlan1003 act/unsup Po1, Po5, Po21, Po22, Po23, Po24, Po25, Po26, Po27, Po28, Po30 1004 Vlan1004 act/unsup Po1, Po5, Po21, Po22, Po23, Po24, Po25, Po26, Po27, Po28, Po30 1005 Vlan1005 act/unsup Po1, Po5, Po21, Po22, Po23, Po24, Po25, Po26, Po27, Po28, Po30 1275 Vlan1275 active +1276 A1 - VIDEO active Po1 VLAN Type SAID MTU Parent RingNo BridgeNo Stp BrdgMode Trans1 Trans2 ---- ----- ---------- ----- ------ ------ -------- ---- -------- ------ ------ @@ -141,6 +142,7 @@ VLAN Type SAID MTU Parent RingNo BridgeNo Stp BrdgMode Trans1 Trans2 1004 fdnet 101004 1500 - - - ieee - 0 0 1005 trnet 101005 1500 - - - ibm - 0 0 1275 enet 101275 1500 - - - - - 0 0 +1276 enet 101275 1500 - - - - - 0 0 Primary Secondary Type Ports ------- --------- ----------------- ------------------------------------------ From 638bbbeeedb805691bef25b7f9d8eeec5eb9338a Mon Sep 17 00:00:00 2001 From: Sharky Date: Wed, 27 Sep 2023 15:13:55 +0200 Subject: [PATCH 2/4] lets make black happy --- napalm/ios/ios.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/napalm/ios/ios.py b/napalm/ios/ios.py index 048b3abdb..22f17ff22 100644 --- a/napalm/ios/ios.py +++ b/napalm/ios/ios.py @@ -3722,7 +3722,9 @@ def get_vlans(self): return self._get_vlan_all_ports(output) def _get_vlan_all_ports(self, output): - find_regexp = re.compile(r"^(\d+)\s+(.*?(?=active|act\/[isl]{1}shut|act\/unsup))\w+(?:\/\w+)?\S+(\s+[A-Z][a-z].*)?$") + find_regexp = re.compile( + r"^(\d+)\s+(.*?(?=active|act\/[isl]{1}shut|act\/unsup))\w+(?:\/\w+)?\S+(\s+[A-Z][a-z].*)?$" + ) continuation_regexp = re.compile(r"^\s+([A-Z][a-z].*)$") output = output.splitlines() vlans = {} From a81ef592c0eb109d251474909fc15eecef72e5a3 Mon Sep 17 00:00:00 2001 From: Sharky Date: Wed, 27 Sep 2023 15:25:03 +0200 Subject: [PATCH 3/4] writing regex on multiple lines .... intersting , thanks liner --- napalm/ios/ios.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/napalm/ios/ios.py b/napalm/ios/ios.py index 22f17ff22..d68cc82f4 100644 --- a/napalm/ios/ios.py +++ b/napalm/ios/ios.py @@ -3723,7 +3723,9 @@ def get_vlans(self): def _get_vlan_all_ports(self, output): find_regexp = re.compile( - r"^(\d+)\s+(.*?(?=active|act\/[isl]{1}shut|act\/unsup))\w+(?:\/\w+)?\S+(\s+[A-Z][a-z].*)?$" + r"^(\d+)\s+" # vlan id + "(.*?(?=active|act\/[isl]{1}shut|act\/unsup))" # vlan name + "\w+(?:\/\w+)?\S+(\s+[A-Z][a-z].*)?$" # ports ) continuation_regexp = re.compile(r"^\s+([A-Z][a-z].*)$") output = output.splitlines() From 56c1b80aa864401015c08968b04ed1f4bf6c4e64 Mon Sep 17 00:00:00 2001 From: Sharky Date: Wed, 27 Sep 2023 15:28:18 +0200 Subject: [PATCH 4/4] fix pylama w605 --- napalm/ios/ios.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/napalm/ios/ios.py b/napalm/ios/ios.py index d68cc82f4..83ecf751a 100644 --- a/napalm/ios/ios.py +++ b/napalm/ios/ios.py @@ -3724,8 +3724,8 @@ def get_vlans(self): def _get_vlan_all_ports(self, output): find_regexp = re.compile( r"^(\d+)\s+" # vlan id - "(.*?(?=active|act\/[isl]{1}shut|act\/unsup))" # vlan name - "\w+(?:\/\w+)?\S+(\s+[A-Z][a-z].*)?$" # ports + r"(.*?(?=active|act\/[isl]{1}shut|act\/unsup))" # vlan name + r"\w+(?:\/\w+)?\S+(\s+[A-Z][a-z].*)?$" # ports ) continuation_regexp = re.compile(r"^\s+([A-Z][a-z].*)$") output = output.splitlines()