diff --git a/netbox_sync/core/lancom.py b/netbox_sync/core/lancom.py index 3a54fa9..9e43a1c 100644 --- a/netbox_sync/core/lancom.py +++ b/netbox_sync/core/lancom.py @@ -239,7 +239,7 @@ def parse_neighbors(output): local = _field(block, "Local Port") if not local: continue - port = re.match(r"(\d+)", local) + port = re.search(r"(\d+)", local) # "Port 2" → "2", the name of the switch port neighbors.append({ "local_port": port.group(1) if port else local, "system_name": _field(block, "System Name"), @@ -304,3 +304,16 @@ def build_snapshot(outputs): "location": _field(system, "Location"), "mac": mac, "serial": _field(system, "Serial Number"), "firmware": _field(system, "Firmware Version")}, } + + +def reparse(snapshot): + """Rebuild a stored snapshot from its raw outputs with the current parsers. + + Exchange files keep all LANCOM outputs, so parser fixes also apply to older exports. + """ + raw = snapshot.get("raw") or {} + if snapshot.get("platform") != PLATFORM or not raw.get("status"): + return snapshot + rebuilt = build_snapshot(dict(raw, lldp_enabled_automatically=snapshot.get("lldp_enabled_automatically"))) + rebuilt["discovered_at"] = snapshot.get("discovered_at", rebuilt["discovered_at"]) + return rebuilt diff --git a/netbox_sync/ui/actions.py b/netbox_sync/ui/actions.py index 5981565..def2cc3 100644 --- a/netbox_sync/ui/actions.py +++ b/netbox_sync/ui/actions.py @@ -9,7 +9,7 @@ from concurrent.futures import ThreadPoolExecutor, as_completed from PySide6.QtCore import QTimer from PySide6.QtWidgets import QFileDialog -from netbox_sync.core import backups, exchange, netbox +from netbox_sync.core import backups, exchange, lancom, netbox from netbox_sync.core.client_sync import ClientSyncEngine from netbox_sync.core.clients import ClientScanError, expand_targets, new_row, query_ad, scan_clients from netbox_sync.core.config import known_hosts_path @@ -320,6 +320,7 @@ class Actions: return snapshots, added = exchange.merge_switches(self.state.switches, data["switches"], lambda: uuid.uuid4().hex) for switch_id, snapshot in snapshots.items(): + snapshot = lancom.reparse(snapshot) self.state.switch_snapshots[switch_id] = snapshot self.state.switch_status[switch_id] = ("ok", "aus Datei · " + describe_snapshot(snapshot)) if added: diff --git a/tests/test_lancom.py b/tests/test_lancom.py index 4e1a33c..81386b0 100644 --- a/tests/test_lancom.py +++ b/tests/test_lancom.py @@ -267,3 +267,31 @@ def test_paging_is_answered_with_space(): assert channel.sent == ["show", ""] assert "RAM Size : 128" in output and "Flash Size : 32" in output assert "quit" not in output and "more" not in output + + +def test_neighbor_on_named_local_port(): + neighbors = """ +Local Port : Port 2 +Chassis ID : 00-a0-57-11-22-33 +Port ID : LAN1 +System Name : AP-005 +Management Address : 10.0.0.50 (IPv4) +SW-08(lldp)#""" + snapshot = build_snapshot(dict(outputs(), neighbors=neighbors), "lancom_sx3") + port = next(item for item in snapshot["interfaces"] if item["name"] == "2") + assert port["neighbor"]["local_port"] == "2" + assert port["neighbor"]["system_name"] == "AP-005" and port["neighbor"]["remote_port"] == "LAN1" + assert port["neighbor"]["management_ip"] == "10.0.0.50" + + +def test_reparse_fixes_older_exports(): + neighbors = "Local Port : Port 2\nPort ID : LAN1\nSystem Name : AP-005\nSW-08(lldp)#" + snapshot = build_snapshot(dict(outputs(), neighbors=neighbors), "lancom_sx3") + snapshot["neighbors"][0]["local_port"] = "Port 2" # as written by 4.16.3 + for item in snapshot["interfaces"]: + item["neighbor"] = None + snapshot["discovered_at"] = 123 + rebuilt = lancom.reparse(snapshot) + assert rebuilt["discovered_at"] == 123 + assert next(item for item in rebuilt["interfaces"] if item["name"] == "2")["neighbor"]["system_name"] == "AP-005" + assert lancom.reparse({"platform": "aruba_cx", "raw": {}}) == {"platform": "aruba_cx", "raw": {}}