-
Notifications
You must be signed in to change notification settings - Fork 7
/
ndb_nxos_install.py
416 lines (383 loc) · 15 KB
/
ndb_nxos_install.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import re
import cli
import sys
import logging
import optparse
COMMAND_LIST = []
STRICT_LOG_FILE = True
if '--debug' in sys.argv:
STRICT_LOG_FILE = False
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
CON_LOG_HANDLER = logging.StreamHandler()
FILE_LOG_HANDGLER = logging.FileHandler('/bootflash/ndb_nxos_install.log')
FILE_LOG_HANDGLER.setLevel(logging.DEBUG)
CON_LOG_HANDLER.setLevel(logging.DEBUG)
FORMATTER = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
FILE_LOG_HANDGLER.setFormatter(FORMATTER)
CON_LOG_HANDLER.setFormatter(FORMATTER)
logger.addHandler(FILE_LOG_HANDGLER)
logger.addHandler(CON_LOG_HANDLER)
elif len(sys.argv) == 1:
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
FILE_LOG_HANDGLER = logging.FileHandler('/bootflash/ndb_nxos_install.log')
FILE_LOG_HANDGLER.setLevel(logging.DEBUG)
FORMATTER = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
FILE_LOG_HANDGLER.setFormatter(FORMATTER)
logger.addHandler(FILE_LOG_HANDGLER)
else:
usage = 'usage: %prog [options]'
PARSER = optparse.OptionParser(usage=usage)
PARSER.add_option(
'--debug',
action='store_true',
help='Optional Arg: Specify the debug logs'+\
' in both console and log file ')
(options, args) = PARSER.parse_args()
print'Invalid argument. Plesae use -h option for more details.'
sys.exit(0)
try:
NXOS = cli.cli('show version | inc NXOS | inc version')
PLATFORM = cli.cli('show version | inc ignore-case Chassis')
except BaseException:
logger.error(
'Something went wrong while fetching switch platform and nxos')
sys.exit(0)
def enter_mode():
mode = raw_input('Enter mode(NXAPI):')
if STRICT_LOG_FILE:
print'Validating mode.Please wait..'
if mode == '':
mode = 'NXAPI'
logger.info('Mode: ' + ' ' + mode)
else:
validate_mode(mode)
logger.info('Mode:' + ' ' + mode)
def validate_mode(mode):
if mode.lower() == 'nxapi' or mode.lower() == 'nx':
pass
else:
logger.error('Enter valid mode')
sys.exit(0)
def add_span():
try:
cli.cli('configure terminal ; spanning-tree mode mst')
logger.info('spanning-tree mode mst' +
' command executed successfully')
cli.cli('configure terminal ; vlan 1-3967')
logger.info('vlan 1-3967' +
' command configured successfully')
cli.cli(
'configure terminal ; no spanning-tree vlan 1-3967')
logger.info(
'no spanning-tree vlan 1-3967' +
' command configured successfully')
except BaseException:
logger.error('Something went wrong while' +
'executing spanning tree and vlan configuration')
sys.exit(0)
def install():
if '3548' in PLATFORM or '3048' in PLATFORM:
logger.error(
'Current Switch platform' +
' ' +
'does not support NXAPI')
sys.exit(0)
elif '9000' not in PLATFORM:
platform = validate_platform()
if platform:
get_device_ip()
get_version()
enter_mode()
add_span()
add_banner()
add_hardware_config()
else:
logger.error('Device not supported')
sys.exit(0)
else:
get_device_ip()
get_version()
enter_mode()
add_span()
add_banner()
add_hardware_config()
def validate_platform():
try:
if 'N3K' in cli.cli('sh module'):
return True
else:
return False
except BaseException:
logger.error('Caught exception whle validting switch platform')
sys.exit(0)
def add_banner():
banner_name = raw_input('Enter banner message:')
result = validate_banner(banner_name)
try:
if result:
cli.cli('configure terminal ; banner motd' + ' ' + banner_name)
logger.info('banner added successfully')
else:
banner_name = banner_name[0:] + banner_name[0]
cli.cli('configure terminal ; banner motd' + ' ' + banner_name)
logger.info('banner added successfully')
except BaseException:
logger.error('Something went wrong while adding banner message')
sys.exit(0)
def validate_banner(banner):
if banner == '':
logger.error('Enter valid banner message')
sys.exit(0)
for i in banner[1:]:
if banner[0] == i:
return True
return False
def add_hardware_config():
if '9000' in PLATFORM or '3164' in PLATFORM or '32' in PLATFORM:
if '92' in PLATFORM or ('93' in PLATFORM and 'EX' in PLATFORM):
ing_tcam = raw_input(
'Enter hardware access-list tcam region ing-ifacl:')
validate_tcam(ing_tcam)
if 'double-wide' in ing_tcam:
logger.error('double-wide not supported for platform')
sys.exit(0)
if STRICT_LOG_FILE:
print'Configuration is being added. Please wait..'
try:
cli.cli('configure terminal ; feature nxapi')
logger.info('feature nxapi command executed successfully')
except BaseException:
logger.error('feature nxapi command execution failed')
sys.exit(0)
try:
cli.cli(
'configure terminal' +
' ; hardware access-list tcam region ing-ifacl' +
' ' +
ing_tcam)
logger.info(
'ing-ifacl tcam region' +
' configured successfully')
except BaseException:
logger.error(
'Failed to configure ing-ifacl' +
' Tcam region exceeded')
sys.exit(0)
cli.cli('copy running-config startup-config')
logger.info('Configuration copied to startup')
logger.info('Reload the switch' +
' for configuration to take effect')
else:
ifacl_tcam = raw_input(
'Enter hardware access-list tcam region ifacl:')
validate_tcam(ifacl_tcam)
ipv6_tcam = raw_input(
'Enter hardware access-list tcam region ipv6-ifacl:')
validate_tcam(ipv6_tcam)
mac_tcam = raw_input(
'Enter hardware access-list tcam region mac-ifacl:')
validate_tcam(mac_tcam)
if STRICT_LOG_FILE:
print'Configuration is being added. Please wait..'
try:
cli.cli('configure terminal ; feature nxapi')
logger.info('feature nxapi command executed successfully')
except BaseException:
logger.error('feature nxapi command execution failed')
sys.exit(0)
try:
cli.cli(
'configure terminal' +
' ; hardware access-list tcam region ifacl' +
' ' +
ifacl_tcam)
COMMAND_LIST.append(
'hardware access-list tcam region ifacl')
logger.info(
'ifacl tcam region' +
' configured successfully')
except BaseException:
logger.error(
'Failed to configure ifacl ' +
' .Tcam region exceeded')
sys.exit(0)
try:
cli.cli(
'configure terminal' +
' ; hardware access-list tcam region ipv6-ifacl' +
' ' +
ipv6_tcam)
COMMAND_LIST.append(
'hardware access-list tcam region ipv6-ifacl')
logger.info(
'ipv6-ifacl tcam region' +
' configured successfully')
except BaseException:
remove_config(COMMAND_LIST[0], ifacl_tcam)
logger.error(
'Failed to configure ipv6-ifacl' +
' .Tcam region exceeded')
sys.exit(0)
try:
cli.cli(
'configure terminal' +
' ; hardware access-list tcam region mac-ifacl' +
' ' +
mac_tcam)
COMMAND_LIST.append(
'hardware access-list tcam region mac-ifacl')
logger.info(
'mac-ifacl tcam region' +
' configured successfully')
except BaseException:
remove_config(COMMAND_LIST[0], ifacl_tcam)
remove_config(COMMAND_LIST[1], ipv6_tcam)
logger.error(
'Failed to configure mac-ifacl' +
' .Tcam region exceeded')
sys.exit(0)
cli.cli('copy running-config startup-config')
logger.info('Configuration copied to startup')
logger.info('Reload the switch' +
' for configuration to take effect')
else:
ifacl_tcam = raw_input(
'Enter hardware profile tcam region ifacl:')
validate_tcam(ifacl_tcam)
if STRICT_LOG_FILE:
print'Configuration is being added. Please wait..'
try:
cli.cli('configure terminal ; feature nxapi')
logger.info('feature nxapi command executed successfully')
except BaseException:
logger.error('feature nxapi command executed failed')
sys.exit(0)
try:
cli.cli('configure terminal' +
' ; hardware profile tap-aggregation')
logger.info(
'hardware profile tap-aggregation' +
'command executed successfully')
COMMAND_LIST.append('hardware profile tap-aggregation')
except BaseException:
logger.error('hardware profile tap-aggregation' +
' command execution failed')
sys.exit(0)
try:
cli.cli(
'configure terminal' +
' ; hardware profile tcam region ifacl' +
' ' +
ifacl_tcam)
COMMAND_LIST.append('hardware profile tcam region ifacl')
logger.info(
'ifacl tcam region' +
'configured successfully')
except BaseException:
remove_config(COMMAND_LIST[0], '')
logger.error(
'Failed to configure ifacl' +
' .Tcam region exceeded')
sys.exit(0)
cli.cli('copy running-config startup-config')
logger.info('Configuration copied to startup')
logger.info('Reload the switch' +
' for configuration to take effect')
def remove_config(command, size):
if size == '':
try:
cli.cli('configure terminal ; no' + ' ' + command)
except BaseException:
logger.error('Failed to remove configuration')
sys.exit(0)
else:
try:
cli.cli('configure terminal ; no' + ' ' + command + ' ' + size)
except BaseException:
logger.error('Failed to remove configuration')
sys.exit(0)
def validate_tcam(tcam_value):
if 'double-wide' in tcam_value:
if tcam_value.split(' ')[0].isdigit():
pass
else:
logger.error('Enter valid number for tcam')
sys.exit(0)
if int(tcam_value.split(' ')[0]) % 256 == 0:
pass
else:
logger.error('TCAM size should be in multiple of 256 entries')
sys.exit(0)
elif tcam_value.isdigit():
if int(tcam_value.split(' ')[0]) % 256 == 0:
pass
else:
logger.error('TCAM size should be in multiple of 256 entries')
sys.exit(0)
else:
logger.error('Enter valid number for tcam')
sys.exit(0)
def get_device_ip():
ip_command = cli.cli('sh run interface mgmt0 | grep ip').split(' ')
ip_address = ip_command[4].split('/')[0]
logger.info('Device IP -' + ' ' + ip_address)
def get_version():
list1 = cli.cli('show version | inc NXOS | inc version').split(' ')
version = list1[4].split('\n')[0]
logger.info('NXOS Version -' + ' ' + version)
def interface_parser(out):
interfaces = {'Ethernet' : [], 'port-channel': [] }
lines = out.replace('\\r', '')
lines = lines.replace('\r','')
lines = lines.replace('\\n','\n')
lines = lines.split("\n\n")
for line in lines:
line = line.strip(" ")
if re.search(r'(interface)\s+(.*)',line,re.IGNORECASE):
value = re.search(r'(interface)\s+(.*)',line,re.IGNORECASE)
interface_name = value.group(2)
if 'Ethernet' in interface_name:
interfaces['Ethernet'].append(interface_name)
elif 'port-channel' in interface_name:
interfaces['port-channel'].append(interface_name)
return interfaces
def main():
try:
while True:
operation = raw_input("Do you want to shut/unshut all interfaces [shut(S)/unshut(U)/no_action(N)] - ")
if operation in ['S','U']:
interfaces = cli.cli('sh run int | no-more')
interface_response = interface_parser(interfaces)
interface_names = ''
if len(interface_response['port-channel']) != 0:
interface_names = ','.join(interface_response['port-channel'])
if operation == 'S':
cli.cli('configure terminal ; interface '+ interface_names + ' ; shutdown ; end')
logger.info("Successfully shutted down the port-channel interfaces")
elif operation == 'U':
cli.cli('configure terminal ; interface '+ interface_names + ' ; no shutdown ; end')
logger.info("Successfully unshutted the port-channel interfaces")
if len(interface_response['Ethernet']) != 0:
interface_names = ','.join(interface_response['Ethernet'])
if operation == 'S':
cli.cli('configure terminal ; interface '+ interface_names + ' ; shutdown ; end')
logger.info("Successfully shutted down the ethernet interfaces")
elif operation == 'U':
cli.cli('configure terminal ; interface '+ interface_names + ' ; no shutdown ; end')
logger.info("Successfully unshutted the ethernet interfaces")
break
elif operation == 'N':
break
else:
print "Invalid input"
except BaseException:
logger.error(
'Something went wrong while fetching switch platform and nxos')
sys.exit(0)
install()
if __name__ == '__main__':
main()