This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-devices.py
executable file
·179 lines (159 loc) · 5.84 KB
/
check-devices.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python3
"""
This script run tests on devices
"""
from argparse import ArgumentParser
from getpass import getpass
import sys
from datetime import datetime
import ssl
from math import ceil
from socket import setdefaulttimeout
from json import dumps
from jsonrpclib import Server,jsonrpc
from prettytable import PrettyTable
from yaml import safe_load
from colorama import Fore
import anta.tests
# pylint: disable=protected-access
ssl._create_default_https_context = ssl._create_unverified_context
def create_connections_dict(text_file, device_username, device_password, output_file):
connections = {}
try:
with open(text_file, 'r', encoding='utf8') as file:
for device in file:
device = device.strip()
connections[device] = Server(
f"https://{device_username}:{device_password}@{device}/command-api"
)
except FileNotFoundError:
print('Error opening ' + text_file)
sys.exit(1)
# Delete unreachable devices from connections dict
unreachable = []
print('Testing devices .... please be patient ... ')
for device, connection in connections.items():
try:
setdefaulttimeout(5)
connection.runCmds(1, ['show version'])
except jsonrpc.TransportError:
print('wrong credentials for ' + device)
unreachable.append(device)
except OSError:
print(device + ' is not reachable using eAPI')
unreachable.append(device)
for key in unreachable:
connections.pop(key)
with open(output_file, 'w', encoding="utf8") as outfile:
now = datetime.now()
outfile.write(now.strftime("%c"))
outfile.write('\n')
outfile.write('devices inventory file was ' + text_file)
outfile.write('\n')
outfile.write('devices username was ' + device_username)
outfile.write('\n')
outfile.write('list of unreachable devices is \n')
for element in unreachable:
outfile.write(element + "\n")
return connections
def main():
parser = ArgumentParser(
description='EOS devices health checks'
)
parser.add_argument(
'-i',
help='Text file containing a list of switches, one per line',
dest='inventory_file',
required=True
)
parser.add_argument(
'-u',
help='Devices username',
dest='username',
required=True
)
parser.add_argument(
'-t',
help='Text file containing the tests',
dest='test_catalog',
required=True
)
parser.add_argument(
'-o',
help='Output file',
dest='output_file',
required=True
)
args = parser.parse_args()
args.password = getpass(prompt='Device password: ')
args.enable_pass = getpass(prompt='Enable password (if any): ')
# Create connections dict
connections = create_connections_dict\
(args.inventory_file, args.username, args.password, args.output_file)
try:
with open(args.test_catalog, 'r', encoding='utf8') as file:
test_catalog = safe_load(file)
except FileNotFoundError:
print('Error opening ' + args.test_catalog)
sys.exit(1)
# Create the dictionnary test_summary for test results and run the tests
test_summary = {}
for device, connection in connections.items():
print('Running tests on device ' + device + ' ...')
test_summary[device] = {}
for test, test_def in test_catalog.items():
# Run test cases
test_kwargs = {}
if 'name' in test_def:
func_name = test_def['name']
test_kwargs = {k:v for k,v in test_def.items() if k != 'name'}
else:
func_name = test_def
test_summary[device][test] = getattr(anta.tests, func_name)\
(connection, args.enable_pass, **test_kwargs)
# Replace True/False/None with Pass/Fail/Skip in the test_summary dictionnary
for device in sorted(test_summary):
for test in sorted(test_catalog):
if test_summary[device][test] is True:
test_summary[device][test] = Fore.GREEN + 'Pass' + Fore.RESET
elif test_summary[device][test] is False:
test_summary[device][test] = Fore.RED + 'Fail' + Fore.RESET
elif test_summary[device][test] is None:
test_summary[device][test] = Fore.BLUE + 'Skip' + Fore.RESET
# Use prettytable so we will display data in a visually appealing format
x = PrettyTable(padding_width=2)
fields = ['devices']
for test in sorted(test_catalog):
fields.append(test)
x.field_names = fields
for device in sorted(test_summary):
row = [device]
for test in sorted(test_catalog):
row.append(test_summary[device][test])
x.add_row(row)
# Split the table into t tables of c columns each
c = 12
lenx = len(fields)
t = ceil(lenx/c)
print("Test results are saved on " + args.output_file)
# Write tests result to an external file
with open(args.output_file, 'a', encoding="utf8") as outfile:
outfile.write('tests file was ' + args.test_catalog)
outfile.write('\n\n')
outfile.write('***** Results *****\n')
outfile.write('\n')
for i in range(t):
start = (i * (c -1) + 1)
stop = ((c - 1) * (i + 1)) + 1
y = ["devices"] + fields[start:stop]
outfile.write(x.get_string(fields=y))
outfile.write('\n')
outfile.write('\n')
outfile.write('***** Tests *****\n')
outfile.write('\n')
for test in sorted(test_catalog):
outfile.write(test + '\t '+ dumps(test_catalog[test]))
outfile.write('\n')
outfile.write('\n')
if __name__ == '__main__':
main()