fix: LANCOM LLDP fields with empty values, indented lines after paging and prompt as port description; explain skipped cables

Every skipped cable is reported as a note with its reason (local port,
neighbor device or far-end interface not found, far end already cabled).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
MrBlakeandClaude Opus 5.5 committed 2026-11-05 16:38:23 +01:00
1 parent b89a364bf0
commit 0241509845
4 files changed
+94 -14

No files matched your search

+15 -8
View File
@@ -12,6 +12,7 @@ import time
from netbox_sync.core.switches import (
CLI_ERROR,
CLI_PROMPT,
MAC_PATTERN,
_compress_vlans,
_interface,
_membership,
@@ -120,7 +121,9 @@ def run_commands(channel, log=lambda *_args: None, name="", enable_lldp=False, w
def _field(text, label):
match = re.search(rf"(?im)^{re.escape(label)}\s*:\s*(.*?)\s*$", text or "")
# Only spaces/tabs around the colon: an empty value must not swallow the next line. Lines after a
# paging prompt are indented, hence the optional leading blanks.
match = re.search(rf"(?im)^[ \t]*{re.escape(label)}[ \t]*:[ \t]*(.*?)[ \t]*$", text or "")
return match.group(1) if match else ""
@@ -148,17 +151,17 @@ def parse_status(output):
def parse_port_config(output):
"""``show configuration``: a port line followed by its description line."""
"""``show configuration``: a port line, directly followed by its (possibly empty) description line."""
ports, current = {}, None
for line in clean_output(output).splitlines():
match = re.match(r"^\s*(\d+)\s+(Enabled|Disabled)\s", line, re.I)
if match:
current = int(match.group(1))
ports[current] = {"enabled": match.group(2).lower() == "enabled", "description": ""}
elif current is not None and line.strip() and not set(line.strip()) <= {"-"}:
ports[current]["description"] = line.strip()
current = None
elif current is not None and set(line.strip()) <= {"-"} and line.strip():
elif current is not None:
text = line.strip()
if text and not set(text) <= {"-"} and not CLI_PROMPT.search(text):
ports[current]["description"] = text
current = None
return ports
@@ -240,14 +243,18 @@ def parse_neighbors(output):
if not local:
continue
port = re.search(r"(\d+)", local) # "Port 2" → "2", the name of the switch port
chassis = _field(block, "Chassis ID")
name = _field(block, "System Name")
if not name and chassis and not MAC_PATTERN.fullmatch(chassis) and not re.fullmatch(r"[\d.]+", chassis):
name = chassis # e.g. Windows hosts send their name only as chassis ID
neighbors.append({
"local_port": port.group(1) if port else local,
"system_name": _field(block, "System Name"),
"system_name": name,
"remote_port": _field(block, "Port ID") or _field(block, "Port Description"),
"port_description": _field(block, "Port Description"),
"management_ip": (re.search(r"\b\d{1,3}(?:\.\d{1,3}){3}\b", _field(block, "Management Address"))
or [""])[0],
"chassis_id": _field(block, "Chassis ID"),
"chassis_id": chassis,
"protocol": "LLDP",
})
return neighbors
+25 -6
View File
@@ -2031,21 +2031,35 @@ def sync_snapshot_to_netbox(nb, config, snapshot, create_cables=True, dry_run=Fa
lookup = _RemoteLookup(nb)
manual_devices = config.get("neighbor_devices") or {}
for neighbor in snapshot["neighbors"]:
label = f"{neighbor.get('local_port')} → {neighbor.get('system_name')} {neighbor.get('remote_port')}"
name = neighbor.get("system_name") or neighbor.get("management_ip") or neighbor.get("chassis_id") or "?"
label = f"{neighbor.get('local_port')} → {name} {neighbor.get('remote_port') or ''}".strip()
def skip(reason):
report["cables_skipped"] += 1
report["notes"].append(f"Kabel {device.name} {label} übersprungen: {reason}")
try:
key = interface_key(neighbor["local_port"])
if key not in records:
skip(f"lokaler Port „{neighbor['local_port']}“ gehört zu keiner Schnittstelle")
continue
local = records[key]
if local is None: # interface only created by this (dry) run
if not dry_run:
skip("lokale Schnittstelle wurde nicht angelegt")
continue
existing = getattr(local, "cable", None) if local is not None else None
if neighbor["local_port"] in manual_devices:
# 0 means "no device" was chosen in the review dialog.
chosen = int(manual_devices[neighbor["local_port"]] or 0)
remote_device = nb.dcim.devices.get(chosen) if chosen else None
if not chosen:
report["cables_skipped"] += 1 # deliberately "no cable"
continue
remote_device = nb.dcim.devices.get(chosen)
else:
remote_device = lookup.device(neighbor)
if not remote_device:
report["cables_skipped"] += 1
skip("Nachbargerät nicht in NetBox gefunden – bei Bedarf im Reiter „Nachbarn“ zuordnen")
continue
if remote_device.id in own_devices:
continue
@@ -2054,10 +2068,15 @@ def sync_snapshot_to_netbox(nb, config, snapshot, create_cables=True, dry_run=Fa
if remote:
_check_cable(nb, local, remote, f"{device.name} {label}", update_cables, dry_run, report)
else:
report["cables_skipped"] += 1
skip(f"lokaler Port hat bereits ein Kabel, Schnittstelle „{neighbor.get('remote_port')}“ "
f"auf {remote_device.name} nicht gefunden")
continue
if not remote or getattr(remote, "cable", None):
report["cables_skipped"] += 1
if not remote:
skip(f"keine passende Schnittstelle „{neighbor.get('remote_port')}“ auf {remote_device.name} "
"(Name, Portnummer, MAC oder einziger physischer Port)")
continue
if getattr(remote, "cable", None):
skip(f"{remote_device.name} {remote.name} hat bereits ein Kabel")
continue
report["cables_created"] += 1
report["changes"].append(f"Kabel {device.name} {label}")