-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfo_grabber_asa.py
121 lines (98 loc) · 3.13 KB
/
info_grabber_asa.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/local/bin/python3
"""
This script uses the Nornir framework to collect discovery information from
Cisco network devices and save the output to file. Devices and parameters are
provided by the SimpleInventory plugin for Nornir using YAML files.
"""
import sys
from getpass import getpass
from datetime import datetime
from nornir import InitNornir
from nornir.core.filter import F
from nornir.plugins.tasks import text, files
from nornir.plugins.functions.text import print_result
from nornir.plugins.tasks.networking import netmiko_send_command
# print formatting function
def c_print(printme):
# Print centered text with newline before and after
print(f"\n" + printme.center(80, " ") + "\n")
# Nornir kickoff
def kickoff():
# print banner
print()
print("~" * 80)
c_print("This script will gather discovery information from Cisco devices")
if len(sys.argv) < 2:
site = ""
else:
site = sys.argv[1] + "_"
# initialize The Norn
nr = InitNornir(
inventory={
"plugin": "nornir.plugins.inventory.simple.SimpleInventory",
"options": {
"host_file": f"inventory/{site}hosts.yaml",
"group_file": f"inventory/{site}groups.yaml",
"defaults_file": "inventory/defaults.yaml",
},
}
)
# filter The Norn
nr = nr.filter(platform="ios")
c_print("Checking inventory for credentials")
# check for existing credentials in inventory
if nr.inventory.defaults.username == None or nr.inventory.defaults.password == None:
c_print("Please enter device credentials:")
if nr.inventory.defaults.username == None:
nr.inventory.defaults.username = input("Username: ")
if nr.inventory.defaults.password == None:
nr.inventory.defaults.password = getpass()
print()
print("~" * 80)
return nr
def grab_info(task):
# show commands to be run
commands = [
"show version",
"show inventory",
"show run",
"show interface ip brief",
"show route",
"show arp",
]
c_print(f"*** Collecting data from {task.host} ***")
# set time stamp for output
time_stamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
# loop over commands
for cmd in commands:
# send command to device
output = task.run(task=netmiko_send_command, command_string=cmd)
# save results with timestamp to aggregate result
task.host["info"] = (
"\n" * 2
+ "#" * 40
+ "\n"
+ cmd
+ " : "
+ time_stamp
+ "\n"
+ "#" * 40
+ "\n" * 2
+ output.result
)
# write output files with time stamp
task.run(
task=files.write_file,
filename=f"output/{task.host}_info_{time_stamp}.txt",
content=task.host["info"],
append=True,
)
def main():
# kickoff The Norn
nr = kickoff()
# run The Norn
nr.run(task=grab_info)
c_print(f"Failed hosts: {nr.data.failed_hosts}")
print("~" * 80)
if __name__ == "__main__":
main()