Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b89a364bf0 | ||
|
|
97ee72e610 | ||
|
|
c85df7402d | ||
|
|
5427cc015e |
No files matched your search
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
#define AppName "NetBox Synchronizer"
|
#define AppName "NetBox Synchronizer"
|
||||||
#ifndef AppVersion
|
#ifndef AppVersion
|
||||||
#define AppVersion "4.16.2"
|
#define AppVersion "4.16.4"
|
||||||
#endif
|
#endif
|
||||||
#define AppPublisher "Louis-Alexander Kerst"
|
#define AppPublisher "Louis-Alexander Kerst"
|
||||||
#define AppExeName "NetBoxSynchronizer.exe"
|
#define AppExeName "NetBoxSynchronizer.exe"
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
"""NetBox Synchronizer – VMs, switches and clients from the field into NetBox."""
|
"""NetBox Synchronizer – VMs, switches and clients from the field into NetBox."""
|
||||||
|
|
||||||
APP_NAME = "NetBox Synchronizer"
|
APP_NAME = "NetBox Synchronizer"
|
||||||
APP_VERSION = "4.16.2"
|
APP_VERSION = "4.16.4"
|
||||||
APP_PUBLISHER = "Louis-Alexander Kerst"
|
APP_PUBLISHER = "Louis-Alexander Kerst"
|
||||||
APP_USER_MODEL_ID = "Louis-AlexanderKerst.NetBoxSynchronizer"
|
APP_USER_MODEL_ID = "Louis-AlexanderKerst.NetBoxSynchronizer"
|
||||||
|
|
||||||
|
|||||||
@@ -11,10 +11,10 @@ import time
|
|||||||
|
|
||||||
from netbox_sync.core.switches import (
|
from netbox_sync.core.switches import (
|
||||||
CLI_ERROR,
|
CLI_ERROR,
|
||||||
_interface,
|
CLI_PROMPT,
|
||||||
_compress_vlans,
|
_compress_vlans,
|
||||||
|
_interface,
|
||||||
_membership,
|
_membership,
|
||||||
_run_cli_command as _run_with_newline,
|
|
||||||
clean_output,
|
clean_output,
|
||||||
parse_vlan_list,
|
parse_vlan_list,
|
||||||
)
|
)
|
||||||
@@ -35,9 +35,33 @@ COMMANDS = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# The CLI pages long outputs ("-- more --, next page: Space, ..., q to quit") and cannot be told not to.
|
||||||
|
PAGER = re.compile(r"(?im)^[^\n]*(?:q to quit|-+\s*more\s*-+)[^\n]*\Z")
|
||||||
|
|
||||||
|
|
||||||
def _run_cli_command(channel, command, timeout=20):
|
def _run_cli_command(channel, command, timeout=20):
|
||||||
"""This CLI executes a command only on "\r"; "\n" is just echoed."""
|
"""Run a command; this CLI executes it only on "\r" ("\n" is just echoed).
|
||||||
return _run_with_newline(channel, command, timeout=timeout, newline="\r")
|
|
||||||
|
Paging prompts are answered with Space and removed from the output.
|
||||||
|
"""
|
||||||
|
channel.send(command + "\r")
|
||||||
|
chunks, started = [], time.monotonic()
|
||||||
|
while time.monotonic() - started < timeout:
|
||||||
|
if channel.recv_ready():
|
||||||
|
chunks.append(channel.recv(65535).decode("utf-8", errors="replace"))
|
||||||
|
text = clean_output("".join(chunks))
|
||||||
|
if PAGER.search(text):
|
||||||
|
chunks = [PAGER.sub("", text)]
|
||||||
|
channel.send(" ")
|
||||||
|
started = time.monotonic() # every page gets the full timeout
|
||||||
|
continue
|
||||||
|
if CLI_PROMPT.search(text):
|
||||||
|
return text
|
||||||
|
time.sleep(0.05)
|
||||||
|
text = clean_output("".join(chunks))
|
||||||
|
tail = " | ".join(line.strip() for line in text.splitlines()[-3:] if line.strip())
|
||||||
|
raise TimeoutError(f"CLI-Befehl nicht vollständig abgeschlossen: {command}. Der Switch hat die CLI-Ausgabe nicht "
|
||||||
|
f"mit einem vollständigen Prompt abgeschlossen. Letzte Ausgabe: {tail[-300:] or '–'}")
|
||||||
|
|
||||||
|
|
||||||
def probe(channel):
|
def probe(channel):
|
||||||
@@ -215,7 +239,7 @@ def parse_neighbors(output):
|
|||||||
local = _field(block, "Local Port")
|
local = _field(block, "Local Port")
|
||||||
if not local:
|
if not local:
|
||||||
continue
|
continue
|
||||||
port = re.match(r"(\d+)", local)
|
port = re.search(r"(\d+)", local) # "Port 2" → "2", the name of the switch port
|
||||||
neighbors.append({
|
neighbors.append({
|
||||||
"local_port": port.group(1) if port else local,
|
"local_port": port.group(1) if port else local,
|
||||||
"system_name": _field(block, "System Name"),
|
"system_name": _field(block, "System Name"),
|
||||||
@@ -280,3 +304,16 @@ def build_snapshot(outputs):
|
|||||||
"location": _field(system, "Location"), "mac": mac,
|
"location": _field(system, "Location"), "mac": mac,
|
||||||
"serial": _field(system, "Serial Number"), "firmware": _field(system, "Firmware Version")},
|
"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
|
||||||
@@ -9,7 +9,7 @@ from concurrent.futures import ThreadPoolExecutor, as_completed
|
|||||||
from PySide6.QtCore import QTimer
|
from PySide6.QtCore import QTimer
|
||||||
from PySide6.QtWidgets import QFileDialog
|
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.client_sync import ClientSyncEngine
|
||||||
from netbox_sync.core.clients import ClientScanError, expand_targets, new_row, query_ad, scan_clients
|
from netbox_sync.core.clients import ClientScanError, expand_targets, new_row, query_ad, scan_clients
|
||||||
from netbox_sync.core.config import known_hosts_path
|
from netbox_sync.core.config import known_hosts_path
|
||||||
@@ -320,6 +320,7 @@ class Actions:
|
|||||||
return
|
return
|
||||||
snapshots, added = exchange.merge_switches(self.state.switches, data["switches"], lambda: uuid.uuid4().hex)
|
snapshots, added = exchange.merge_switches(self.state.switches, data["switches"], lambda: uuid.uuid4().hex)
|
||||||
for switch_id, snapshot in snapshots.items():
|
for switch_id, snapshot in snapshots.items():
|
||||||
|
snapshot = lancom.reparse(snapshot)
|
||||||
self.state.switch_snapshots[switch_id] = snapshot
|
self.state.switch_snapshots[switch_id] = snapshot
|
||||||
self.state.switch_status[switch_id] = ("ok", "aus Datei · " + describe_snapshot(snapshot))
|
self.state.switch_status[switch_id] = ("ok", "aus Datei · " + describe_snapshot(snapshot))
|
||||||
if added:
|
if added:
|
||||||
|
|||||||
@@ -214,6 +214,9 @@ def _quick_reads(monkeypatch):
|
|||||||
real = switches._read_available
|
real = switches._read_available
|
||||||
monkeypatch.setattr(switches, "_read_available", lambda channel, timeout=20, initialize_session=False:
|
monkeypatch.setattr(switches, "_read_available", lambda channel, timeout=20, initialize_session=False:
|
||||||
real(channel, timeout=min(timeout, 0.3), initialize_session=initialize_session))
|
real(channel, timeout=min(timeout, 0.3), initialize_session=initialize_session))
|
||||||
|
command = lancom._run_cli_command
|
||||||
|
monkeypatch.setattr(lancom, "_run_cli_command", lambda channel, text, timeout=20:
|
||||||
|
command(channel, text, timeout=min(timeout, 0.3)))
|
||||||
return switches
|
return switches
|
||||||
|
|
||||||
|
|
||||||
@@ -244,3 +247,51 @@ def test_session_is_attached_to_errors(monkeypatch):
|
|||||||
with pytest.raises(TimeoutError) as caught:
|
with pytest.raises(TimeoutError) as caught:
|
||||||
switches.run_switch_commands({"host": "x", "platform": "lancom_sx3"})
|
switches.run_switch_commands({"host": "x", "platform": "lancom_sx3"})
|
||||||
assert "SW-08#" in caught.value.transcript and "»» system" in caught.value.transcript
|
assert "SW-08#" in caught.value.transcript and "»» system" in caught.value.transcript
|
||||||
|
|
||||||
|
|
||||||
|
def test_paging_is_answered_with_space():
|
||||||
|
class Paging(FakeChannel):
|
||||||
|
def send(self, data):
|
||||||
|
if data == " ":
|
||||||
|
self.sent.append("<space>")
|
||||||
|
self.buffer += "\r \rFlash Size : 32\nSW-08(system)# "
|
||||||
|
elif data.strip() == "show" and self.mode == "system":
|
||||||
|
self.sent.append("show")
|
||||||
|
self.buffer += "show\nModel Name : LANCOM GS-2326P+\nRAM Size : 128\n -- more --, next page: Space, q to quit"
|
||||||
|
else:
|
||||||
|
super().send(data)
|
||||||
|
|
||||||
|
channel = Paging()
|
||||||
|
channel.mode = "system"
|
||||||
|
output = lancom._run_cli_command(channel, "show", timeout=2)
|
||||||
|
assert channel.sent == ["show", "<space>"]
|
||||||
|
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": {}}
|
||||||
Reference in new issue
Block a user