Rather than paging through the
show config output I found it easier to enter the
cmdline context and simply copy the
config.json file.
For anyone else using Nornir/Netmiko for config backups, copied below is the Python script to get it done.
- Code: Select all
from nornir import InitNornir
from nornir.plugins.tasks.files import write_file
from nornir.plugins.tasks.networking import netmiko_send_command
from nornir.plugins.functions.text import print_result
import pathlib
#
# Initialize Nornir with config file
#
nr = InitNornir(config_file="config.yaml")
#
# Get list of devices by OS / Platform
#
devices = nr.filter(platform="netonix")
#
# Configure device connection settings
#
def backup_netonix_config(task):
task.host.open_connection(
"netmiko",
configuration=task.nornir.config,
platform="linux",
port="22",
username="yourusername",
password="yourpassword",
)
#
# Create backup directory and enter cmdline context on Netonix
#
backup_dir = "config-backups/"
pathlib.Path(backup_dir).mkdir(exist_ok=True)
task.run(
task=netmiko_send_command,
use_timing="True",
command_string="cmdline",
delay_factor=2,
)
#
# Concatonate config.json file
#
output = task.run(
task=netmiko_send_command,
use_timing="True",
command_string="cat config.json",
delay_factor=2,
)
#
# Write hostname with config output to config-backups folder
#
task.run(
task=write_file,
content=output.result,
filename=str(backup_dir) + task.host.name,
)
#
# Launch script and make it all happen
#
def main():
result = devices.run(task=backup_netonix_config, num_workers=100)
print_result(result, vars=["stdout"])
main()