Search This Blog

Thursday 14 January 2021

Nornir uptime and version check

This script uses Nornir 2.5 and genie to return structured data and check for devices uptime and validate software versions.

from nornir import InitNornir

from nornir.plugins.tasks.networking import netmiko_send_command, netmiko_send_config

from nornir.plugins.functions.text import print_result

import os

from rich import print


nr = InitNornir(config_file="config.yml")


nr.inventory.defaults.username = os.environ["NORNIR_USER"]

nr.inventory.defaults.password = os.environ["NORNIR_PASS"]


def structured_test(task):

    r = task.run(task=netmiko_send_command, command_string="show version", use_genie=True)

    task.host["facts"] = r.result

    uptime = task.host["facts"]["version"]["uptime"]

    version = task.host["facts"]["version"]["version"]

    hostname = task.host["facts"]["version"]["hostname"]

    #print("The uptime of " + hostname + " is " + uptime)

    print(f"{task.host} running version " + version + " has been up for " + uptime)

    if version > "15.4":

        print(f"{task.host}: [green]PASSED[/green]")

    else:

        print(f"{task.host}: [red]FAILED VERSION CHECK[/red]")


result = nr.run(task=structured_test)

No comments:

Post a Comment

Nornir Compliance Check