-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_intermediate.py
72 lines (58 loc) · 2.31 KB
/
2_intermediate.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
# This example show how to execute a command in ALL the inventory
# The list of devices and credentials is specified in an external yaml file,
# imported and looped.
# It will also print how long it took to execute the command
# Change import settings
import yaml
import sys
import time
from netmiko import ConnectHandler, NetmikoTimeoutException, NetmikoAuthenticationException
# Reusable send command function
def send_command(dev: dict, cmd: str) -> str:
"""
Send command to device using Netmiko
:param dev: device info
:param cmd: command to execute
:return: Command output from device
"""
hostname = device['hostname']
# remove key hostname from dictionary since it is not expected/valid for netmiko
del dev['hostname']
try:
# Use context manager to open and close the SSH session
with ConnectHandler(**dev) as ssh:
ssh.enable()
output = ssh.send_command(cmd)
output = output.strip()
except NetmikoTimeoutException:
output = 'Connection timed out'
except NetmikoAuthenticationException:
output = 'Authentication failed'
# re add key for future use
dev['hostname'] = hostname
return output
if __name__ == "__main__":
# get parameters from CLI
# command = ' '.join(sys.argv[2:])
command = input('\nCommand to run: ')
# Load devices from file
with open('inventory.yml') as f:
inventory = yaml.safe_load(f.read())
# get the common variables for all devices
credentials = inventory['common_vars']
devices_counter = len(inventory['hosts'])
print(f'\nExecuting command: {command}\n')
# Start timer variable
execution_start_timer = time.perf_counter()
# Loop to repeat the command in all the inventory
for device in inventory['hosts']:
# update the device dictionary with the credentials and send command
device.update(credentials)
print('*** host: {} - ip: {}'.format(device['hostname'], device['host']))
# send command and store results
result = send_command(device, command)
# show result
print(f'{result}\n')
# Get and print finishing time
elapsed_time = time.perf_counter() - execution_start_timer
print(f"\n\"{command}\" executed in {devices_counter} devices in {elapsed_time:0.2f} seconds.\n")