Search This Blog

Saturday 16 January 2021

Nornir get ntp associations status

 The below script will connect to Cisco network devices and collect "show ntp associations" using Nornir and will parse the data using genie.

The result will look like this:

R1 uses NTP Server: 10.0.0.1 which is: synchronized
R2 uses NTP Server: 10.0.0.2 which is: synchronized


from nornir import InitNornir
from nornir.plugins.functions.text import print_result
from nornir.plugins.tasks.networking import netmiko_send_command
from nornir.plugins.tasks.files import write_file
from nornir.core.exceptions import NornirExecutionError
import csv, pprint
from datetime import datetime

def dev_info(task):
    try:
        r = task.run(netmiko_send_command, command_string="show ntp associations"use_genie=True)
        task.host["facts"] = r.result
        ntp_server = task.host["facts"]["clock_state"]["system_status"]["associations_address"]
        ntp_status = task.host["facts"]["clock_state"]["system_status"]["clock_state"]
        print(f"{task.host} uses NTP Server: " + ntp_server + " which is: " + ntp_status)
    except:
        print(f"{task.host} had some kind on unknown issue")

def main() -> None:
    nr = InitNornir(config_file="config.yml")
    result = nr.run(task=dev_info)
    #print_result(result)

if __name__ == "__main__":
    main()

No comments:

Post a Comment

Nornir Compliance Check