Takes a fresh backup of each device and shows what changed since the earlier one, section by section.
Takes a fresh backup of the running configuration of each device in devices.csv and compares
it with the earlier backup of the same device in the folder backups. Each changed line is
shown with its section in front (interface Gi1/0/3 / shutdown), so a line that only moved, or
the byte count IOS writes on top, is not shown as a change.
Run it on the saved outputs in saved, with no device:
bogo run --replay saved backup_diff.bogo
bogo test backup_diff.bogo
To use it on your own network:
Put your devices in devices.csv and one earlier backup per device in backups, named
DEVICE.cfg.
Store a login once with bogo login default.
Run it without --replay: then it really connects to each device over SSH.
Add the word save after the file name to keep the fresh backups in place of the old ones:
bogo run backup_diff.bogo save. Without it, nothing is written.
A backup holds secrets of the device. Keep the folder backups as carefully as the devices.
backup_diff.bogo
edition2027"""What changed since the last backup? Take a fresh backup of each device and compare it."""classBackupDiff:
"""Compares the running configuration of each device with its file in the folder backups.
Run with the word save to keep the fresh backups in place of the old ones."""
folder: str = "backups" @staticmethoddef touched(changes: ConfigDiff) -> list[str]:
"""The top sections a diff touches, each once, in order: 'vlan 30', 'ntp server ...'."""
seen: list[str] = []
for line in changes.removed + changes.added:
top: str = line.split(" / ")[0]
if top notin seen:
seen.append(top)
return seen
@staticmethoddef main(args: list[str]) -> None:
devices: list[Device] = inventory("devices.csv")
changed: int = 0for d in devices:
fresh: Config = backup(d)
old: Config = load_config(f"{BackupDiff.folder}/{d.name}.cfg", d.platform)
changes: ConfigDiff = diff(old, fresh)
ifnot changes:
print(f"{d.name}: no change")
continue
changed += 1
print(
f"{d.name}: {len(changes.removed)} lines removed, {len(changes.added)} added, "f"in {len(BackupDiff.touched(changes))} sections"
)
print(" " + str(changes).replace("\n", "\n "))
if"save"in args:
fresh.save(f"{BackupDiff.folder}/{d.name}.cfg")
print(f"\n{changed} of {len(devices)} devices changed since their last backup.")
classTestBackupDiff:
def config(self, text: str) -> Config:
returnConfig(Device(name="sw-test", ip=192.0.2.99), text, "")
def test_same_lines_is_no_change(self) -> None:
a: Config = self.config("hostname sw-test\n!\nvlan 10\n name USERS\n")
b: Config = self.config(
"Current configuration : 99 bytes\nhostname sw-test\n!\nvlan 10\n name USERS\n"
)
assertnot diff(a, b)
def test_touched_lists_each_section_once(self) -> None:
a: Config = self.config("interface Gi1/0/3\n description spare\n shutdown\n")
b: Config = self.config("interface Gi1/0/3\n description cameras\nvlan 30\n name CAMERAS\n")
assertBackupDiff.touched(diff(a, b)) == ["interface Gi1/0/3", "vlan 30"]