fix: attach the original D-Link config.bin as configuration backup

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
MrBlakeandClaude Opus 5.5 committed 2026-11-05 12:50:19 +01:00
1 parent 781ed7d17b
commit 63ed535a37
4 files changed
+43 -8

No files matched your search

+5 -1
View File
@@ -14,6 +14,7 @@ Supported formats:
A plain text ``SNCSR.conf`` is accepted as well.
"""
import base64
import io
import ipaddress
import re
@@ -44,7 +45,10 @@ def parse_backup(data, filename=""):
data = data.encode("utf-8")
dump = _dlink_dump(data)
if dump is not None:
return _dlink_snapshot(dump, data)
switch, snapshot = _dlink_snapshot(dump, data)
# The original file is attached 1:1 so it can be restored on the switch.
snapshot["config_file"] = {"name": filename or "config.bin", "data": base64.b64encode(data).decode("ascii")}
return switch, snapshot
text = _linksys_text(data)
if text is not None:
return _linksys_snapshot(text)
+16 -7
View File
@@ -1,5 +1,6 @@
"""SSH discovery and NetBox synchronization for HPE and Aruba switches."""
import base64
import hashlib
import io
import ipaddress
@@ -1730,11 +1731,19 @@ def _backup_config(nb, device, snapshot, dry_run, report):
A new attachment is only added when the configuration of this device changed
since the last backup; the hash in the description identifies it.
"""
config = snapshot.get("config_backup")
if not config:
report["notes"].append("Konfigurations-Backup: keine Konfiguration ausgelesen")
return
digest = hashlib.sha256(f"{device.id}\n{config}".encode("utf-8")).hexdigest()[:16]
original = snapshot.get("config_file")
if original:
# Backup file from the switch's web UI (e.g. D-Link config.bin), attached unchanged.
content = base64.b64decode(original["data"])
extension = Path(original.get("name") or "config.bin").suffix or ".bin"
content_type = "application/octet-stream"
else:
config = snapshot.get("config_backup")
if not config:
report["notes"].append("Konfigurations-Backup: keine Konfiguration ausgelesen")
return
content, extension, content_type = config.encode("utf-8"), ".txt", "text/plain"
digest = hashlib.sha256(f"{device.id}\n".encode("utf-8") + content).hexdigest()[:16]
try:
app = nb.plugins.netbox_attachments
known = [item for item in app.netbox_attachments.filter(q=digest)
@@ -1754,13 +1763,13 @@ def _backup_config(nb, device, snapshot, dry_run, report):
report["changes"].append(f"Konfigurations-Backup: Anhang „{name}“")
if dry_run:
return
filename = re.sub(r"[^\w.-]+", "_", f"{device.name}_{stamp}") + ".txt"
filename = re.sub(r"[^\w.-]+", "_", f"{device.name}_{stamp}") + extension
try:
attachment = app.netbox_attachments.create({
"name": name, "description": f"Running-Config, SHA-256 {digest}",
# Older plugin versions keep the parent on the attachment itself.
"object_type": "dcim.device", "object_id": device.id,
"file": (filename, io.BytesIO(config.encode("utf-8")), "text/plain"),
"file": (filename, io.BytesIO(content), content_type),
})
if hasattr(attachment, "assignments"):
# Newer plugin versions assign attachments to objects separately.