forked from geordie12311/nornir-python-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnorscrap-cdp-write-intdescription.py
More file actions
31 lines (29 loc) · 1.63 KB
/
norscrap-cdp-write-intdescription.py
File metadata and controls
31 lines (29 loc) · 1.63 KB
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
#python script using nornir_scrapli to use cdp neighbor info to automatically write descriptions on
#the host interfaces to show what device it connects to and which remote interface
import getpass
from nornir import InitNornir
from nornir_scrapli.tasks import send_command
from nornir_utils.plugins.functions import print_result
from nornir_scrapli.tasks import send_configs
nr = InitNornir(config_file="config.yaml")
password = getpass.getpass()
nr.inventory.defaults.password = password
#above section is going to prompt the user to put in their password
def pull_structured_data(task):
cdp_result = task.run(task=send_command, command="show cdp neighbor")
task.host["facts"] = cdp_result.scrapli_response.genie_parse_output()
cdp_index = task.host["facts"]["cdp"]["index"]
for num in cdp_index:
local_intf = cdp_index[num]["local_interface"]
remote_intf = cdp_index[num]["port_id"]
remote_device = cdp_index[num]["device_id"]
config_commands = [f"interface {local_intf}", f"description Connected to {remote_device} via its interface {remote_intf}"]
task.run(task=send_configs, configs=config_commands)
#above function is going to create an object called pull_structured_data
#it is then going to send the command "show CDP neighor" to the hosts and then cycle
#through the data to find the local, remote interfaces. It is then going to use scrapli
#send_configs to write the description to each interface on the host i.e. Connected to x device
#via its interface y
results = nr.run(task=pull_structured_data)
print_result(results)
#script is then going to output the results to the screen to verify success