-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscover.py
84 lines (71 loc) · 2.44 KB
/
discover.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
#!/usr/bin/env python
"""
purpose:
Discover device information based on list of known devices
use CDP and LLDP do identify devices not in the known lost
author: Denis Pointer
"""
from nornir import InitNornir
from nornir.plugins.tasks.networking import netmiko_send_command
import json
# need to filter information down a bit to just gather information of interest
def gather_info(task):
"""
TODO add description....
"""
# default command list:
command_list = [
"show version",
"show ip interface brief",
"show cdp neighbors detail",
"show running-config",
]
# gather information for each command in the list
output = {}
# for command in command_list:
for command in command_list:
results = task.run(
netmiko_send_command, command_string=command, use_textfsm=True
)
output[command] = results.result
output_json = json.dumps(output, indent=4)
with open(f"outputs/{task.host.name}.txt", "w") as f:
f.write(output_json)
# more flexible, but overly confusing version...
# maybe find a better way
# perhaps a class for the commands instead of weird nested dict
# possibly just turb idea...going down a weird rabbit hole here
def gather_info_dict(task):
"""
TODO add description....
"""
# default command list:
command_dict = {
"show version": {"yaml_name": "version"},
"show ip interface brief": {"yaml_name": "ip_int_brief"},
"show running-config": {"yaml_ext": "config"},
}
# gather information for each command in the list
output = {}
for command in command_dict.keys():
results = task.run(
netmiko_send_command, command_string=command, use_textfsm=True
)
if command_dict[command].get("yaml_name"):
output[command_dict[command].get("yaml_name")] = results.result
if command_dict[command].get("yaml_ext"):
filename = (
f"outputs/{task.host.name}-"
f"{command_dict[command].get('yaml_ext')}.txt"
)
with open(filename, "w") as f:
f.write(results.result)
output_json = json.dumps(output, indent=4)
with open(f"outputs/{task.host.name}.txt", "w") as f:
f.write(output_json)
# Initialize nornir
nr = InitNornir(config_file="nornir.yaml")
# filter inventory based on Site name
targets = nr.filter(site="GNS3")
# run custom task
targets.run(gather_info)