-
Notifications
You must be signed in to change notification settings - Fork 1
/
nornir-version-check.py
34 lines (29 loc) · 1.59 KB
/
nornir-version-check.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#python script to check for a specific IOS version on hosts
import getpass
from nornir import InitNornir
from nornir_scrapli.tasks import send_command
from nornir_utils.plugins.functions import print_result
from rich import print as rprint
nr = InitNornir(config_file="config.yaml")
#The above line is telling nornir where the config file is located
user = input("Enter your username: ")
password = getpass.getpass(prompt="Enter your password: ")
nr.inventory.defaults.username = user
nr.inventory.defaults.password = password
#The above lines will prompt the user to enter their username and password and use that input to connect to the devices.
def pull_structured_data(task):
version_result = task.run(task=send_command, command="show version")
task.host["facts"] = version_result.scrapli_response.genie_parse_output()
version = task.host["facts"]["version"]["version_short"]
if version =="15.2":
rprint(f"{task.host}:[green]Version check passed")
else:
rprint(f"{task.host}: [red]Version check failed")
#above function is going to create an object called pull_structured_data
#it is then going to create a 2nd object called facts and parse it through genie
#to provide structured data output. Then the object version is going to go down through
#the structure and find "version_short" and then the if statement is going to check for
#version 15.7, if it finds it then it will print "version check" passed along with hostname
#if it finds a different version it will be picked up by the else statement and print out
#"version check failed"
results = nr.run(task=pull_structured_data)