This repository has been archived by the owner on Mar 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collect_eos_commands.py
80 lines (75 loc) · 3.12 KB
/
collect_eos_commands.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
from netmiko import ConnectHandler
import os
import yaml
from audit.functions import device_directories
input_f = open('input.yml', 'r')
input_s = input_f.read()
input_f.close()
input = yaml.load(input_s, Loader=yaml.FullLoader)
devices = input['devices']
output_directory = input['output_directory']
username = input['username']
password = input['password']
text_cmds = input['text_cmds']
json_cmds = input['json_cmds']
text_and_json_cmds = input['text_and_json_cmds']
for device in devices:
# Create directories to save the commands output
directories = device_directories(device, output_directory)
device_directory = directories[0]
eos_commands_directory = directories[1]
json_directory = directories[2]
text_directory = directories[3]
# collect show commands
print("opening connection to " + device)
switch = {'device_type': 'arista_eos', 'host': device, 'username': username, 'password': password, 'port': '22', 'timeout': 180}
connection = ConnectHandler(**switch)
print("collecting show commands on device " + device)
# collect text commands
if text_cmds is not None:
for cmd in text_cmds:
print("collecting " + cmd)
cmd_output = connection.send_command(cmd)
f=open(text_directory + "/" + cmd + ".txt", "w")
f.write(cmd_output)
f.closed
if (text_and_json_cmds is not None) and (text_cmds is not None) :
for cmd in text_and_json_cmds:
if cmd not in text_cmds:
print("collecting " + cmd)
cmd_output = connection.send_command(cmd)
f=open(text_directory + "/" + cmd + ".txt", "w")
f.write(cmd_output)
f.closed
elif (text_and_json_cmds is not None) and (text_cmds is None):
for cmd in text_and_json_cmds:
print("collecting " + cmd)
cmd_output = connection.send_command(cmd)
f=open(text_directory + "/" + cmd + ".txt", "w")
f.write(cmd_output)
f.closed
# collect json commands
if json_cmds is not None:
for cmd in json_cmds:
print("collecting " + cmd + "| json")
cmd_output = connection.send_command(cmd + "| json")
f=open(json_directory + "/" + cmd + ".json", "w")
f.write(cmd_output)
f.closed
if (text_and_json_cmds is not None) and (json_cmds is not None) :
for cmd in text_and_json_cmds:
if cmd not in json_cmds:
print("collecting " + cmd + "| json")
cmd_output = connection.send_command(cmd + "| json")
f=open(json_directory + "/" + cmd + ".json", "w")
f.write(cmd_output)
f.closed
elif (text_and_json_cmds is not None) and (json_cmds is None):
for cmd in text_and_json_cmds:
print("collecting " + cmd + "| json")
cmd_output = connection.send_command(cmd + "| json")
f=open(json_directory + "/" + cmd + ".json", "w")
f.write(cmd_output)
f.closed
print("closing connection to " + device)
connection.disconnect()