-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetmikoutils.py
More file actions
40 lines (35 loc) · 1.54 KB
/
netmikoutils.py
File metadata and controls
40 lines (35 loc) · 1.54 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
32
33
34
35
36
37
38
39
40
#v1.1
from netmiko import ConnectHandler
#Describes a device to be used with netmiko
def def_device(type: str, hostname:str, username:str, password:str):
device = {
'device_type': type,
'host': hostname, #hostname or IP address
'username': username,
'password': password,
}
return device
#Prints results of command(s) sent to device(s)
def print_command_res(device_list: list, command_res_dict: dict):
for device, device_results in command_res_dict.items():
print(f"**********{device}**********")
for command, result in device_results.items():
print(f"{command}:\n{result}")
#Sends a list of commands to a list of devices and returns a dictionary with the results
#Order of hierarchy is device -> command -> result_of_command.
#So you can query any result from any command issued to any device in the list like this: result_var['<device>']['<command>']
def send_commands(devices: list, commands: list):
results = {}
for device in devices:
conn = ConnectHandler(**device)
device_results = {}
for command in commands:
res = conn.send_command(command)
device_results[command] = res
results[device['host']] = device_results
conn.disconnect()
return results #Return a dictionary with results of commands sent to each device
#Sends a list of commands to a list of devices, and prints the output
def send_and_print(devices: list, commands: list):
res = send_commands(devices, commands)
print_command_res(devices, res)