forked from happynet/happynet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeviceIsolation.py
More file actions
358 lines (296 loc) · 13 KB
/
DeviceIsolation.py
File metadata and controls
358 lines (296 loc) · 13 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
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
'''This script will be used for One Link configuration application on Multiple devices'''
import sys
import os
from netmiko import ConnectHandler
from paramiko.ssh_exception import SSHException
import time
from socket import gethostbyname
import system_snmp
import snmp_if_utils
import getpass
from datetime import datetime
import threading
from multiprocessing import Process
from time import sleep
lock = threading.Lock()
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from subprocess import PIPE, Popen
import zipfile
from python_logging import initialize_logger
from python_logging import default_argparse
from itertools import islice
__author__ = "ashay@microsoft.com"
__copyright__ = "copyrighted by Microsoft"
__contributors__ = ["ashay"]
__version__ = "1.0"
__maintainer__ = "Ashay Phynet- DRI <ashay@microsoft.com>"
__maintainer__ = "Ashay Phynet - DRI <ashay@microsoft.com>"
__team_email__ = "gnsfabmonperf@microsoft.com"
__script_name__ = os.path.basename(sys.argv[0])
logger = initialize_logger(default_argparse(), __script_name__)
username = getpass.getuser()
password = getpass.getpass('Enter GME Password: ').strip()
threadlimiter = threading.BoundedSemaphore(10)
def connection(device):
device_input = {'device_type': __get_netmiko_device_os__(device),
'ip': gethostbyname(device),
'username': username,
'password': password,
'global_delay_factor': 0.7
}
try:
connection = ConnectHandler(**device_input)
return connection
except (EOFError, SSHException):
print('SSH is not enabled for this device {}.'.format(device))
def __get_netmiko_device_os__(device):
'''This function will be used to get vendor type'''
oid=".1.3.6.1.2.1.1.1.0"
try:
snmp_data = system_snmp.snmp_get(device, oid)[1]
except:
text = open("notworkingt3.txt", "w")
text.write('Device is not sending SNMP return {}'.format(device))
logger.error('Device is not sending SNMP return {}'.format(device))
text.close()
if "arista" in snmp_data.lower():
return "arista_eos"
elif "nx-os" in snmp_data.lower():
return "cisco_nxos"
elif "dell" in snmp_data.lower():
return "dell_force10"
else:
raise UnsupportedDeviceType("Unsupported device. Only Arista, NX-OS, and Dell supported.")
def save_config(connection):
'''Save the configuration on a device'''
save_command = "copy run start"
try:
connection.send_command(save_command)
except Exception as e:
raise OperationalCommandFailed("Failed to save configuration.")
def pre_check(device, text_file, local_conn):
'''This function will be used to get data out from devices'''
try:
#local_conn = connection(device)
out1 = local_conn.send_command('{}'.format('show version | grep "is:"')).strip()
out2 = local_conn.send_command('{}'.format('show ip bgp sum')).strip()
out3 = local_conn.send_command('{}'.format('show run | grep "boot"')).strip()
text_file.write('================================={} Pre check ================================\n\n'.format(device))
text_file.write('=================================show version | grep "is:" ================================\n\n')
text_file.write('{}\n\n'.format(out1))
text_file.write('=================================show ip bgp sum ================================\n\n')
text_file.write('{}\n\n'.format(out2))
text_file.write('=================================show run | grep "boot" ================================\n\n')
text_file.write('{}\n\n'.format(out3))
logger.info('Writing Pre Checks for : {}'.format(device))
except:
text_file.write('{} : No able to login \n'.format(device))
logger.error('{} : Not able to login'.format(device))
def post_check(device, text_file, local_conn_again):
'''This function will be used to get data out from devices'''
try:
#local_conn = connection(device)
out1 = local_conn_again.send_command('{}'.format('show version | grep "is:"')).strip()
out2 = local_conn_again.send_command('{}'.format('show ip bgp sum')).strip()
out3 = local_conn_again.send_command('{}'.format('show run | grep "boot"')).strip()
sys = local_conn_again.send_command('show ver | grep "is:"').strip().split(':')[-1].replace('/','')
bootsys = local_conn_again.send_command('show run | grep "boot"').strip().split(':')[-1].replace('/','')
text_file.write('================================={} Post check ================================\n\n'.format(device))
text_file.write('=================================show version | grep "is:" ================================\n\n')
text_file.write('{}\n\n'.format(out1))
text_file.write('=================================show ip bgp sum ================================\n\n')
text_file.write('{}\n\n'.format(out2))
text_file.write('=================================show run | grep "boot" ================================\n\n')
text_file.write('{}\n\n'.format(out3))
text_file.write('{} : PostChecks : Device has been fixed : {} : {}\n'.format(device, sys, bootsys))
logger.info('Writing Post Checks for : {}'.format(device))
except:
text_file.write('{} : No able to login \n'.format(device))
logger.error('{} : Not able to login'.format(device))
def ping(device):
response = os.popen('ping -c 1 {} | grep 64'.format(device), 'r').readline()
return response
def needed_info(device, local_conn, text_file):
try:
sys = local_conn.send_command('show ver | grep "is: "').strip().split(':')[-1].replace('/','')
bootsys = local_conn.send_command('show run | grep "boot"').strip().split(':')[-1].replace('/','')
bootflash = local_conn.send_command('dir bootflash: | grep ".4e"').strip().split(' ')[-1]
except:
logger.error('{} : Not able to login'.format(device))
text_file.write('{} : Not able to Login to Device\n'.format(device))
return sys, bootsys, bootflash
def implementation(device, text_file, local_conn):
'''This function will be used to apply one line configuration on devices'''
sys, bootsys, bootflash = needed_info(device, local_conn, text_file)
#print (sys, bootsys, bootflash)
if 'n9000-dk9.6.1.2.I3.4e.bin' in bootflash:
config_command_1 = list()
config_command_1 = ['boot nxos bootflash:/{}'.format(bootflash)]
config_command_2 = list()
config_command_2 = ['exit', 'cppy run start']
config_command_3 = list()
config_command_3 = ['fast-reload nxos bootflash:///n9000-dk9.6.1.2.I3.4e.bin non-interruptive']
try:
local_conn.send_config_set(config_command_1)
print('Changing boot')
sleep(10)
local_conn.send_config_set(config_command_2)
print('Saving config')
sleep(10)
print('fast-rebooting there will be sleep for 5 minutes. Please wait..........')
local_conn.send_config_set(config_command_3)
except:
sleep(240)
response = ping(device)
if response:
print ('Device is up again')
local_conn_again = connection(device)
sys = local_conn_again.send_command('show ver | grep "is: "').strip().split(':')[-1].replace('/','')
if 'n9000-dk9.6.1.2.I3.4e.bin' in sys:
print ('Device has been upgraded')
logger.info('Device has been upgraded : {}'.format(device))
post_check(device, text_file, local_conn_again)
def run_from_shell(argument):
"""Run a process from the shell and return the output"""
process = Popen(argument, shell=True, stdout=PIPE, stderr=PIPE)
stdout = process.stdout.read()
return stdout
def mail_it(nameoffile, cc_list=None):
'''standard function to send mail with attachements'''
prezip = '{}.txt'.format(nameoffile)
sleep(5)
zip_file = '{}.zip'.format(nameoffile)
zip_write = zipfile.ZipFile(zip_file, 'w', zipfile.ZIP_DEFLATED )
zip_write.write(prezip)
zip_write.close()
file = '{}.zip'.format(nameoffile)
to_address = '{}@microsoft.com'.format(username)
subject = 'scanning report run by {}'.format(username)
body = 'Please find attachments for your scanning job'
cc_string = str()
if cc_list != None:
for name in cc_list: cc_string += "-c {} ".format(name)
if file is None:
argument = """echo "{}" | mutt -s "{}" {} {}""".format(body, subject, to_address, cc_string)
elif type(file) == type(list()):
attachments = ""
for entry in file: attachments += "-a {0} ".format(entry)
argument = """echo "{3}" | mutt -s "{1}" {4} {2} {0}""".format(attachments, subject, to_address, body, cc_string)
else:
argument = """echo "{3}" | mutt -s "{1}" {4} {2} -a{0}""".format(file, subject, to_address, body, cc_string)
return run_from_shell(argument)
def check_info(device, text_file):
local_conn = connection(device)
sys, bootsys, bootflash = needed_info(device, local_conn, text_file)
try:
if sys == bootsys:
print('{}:Device is running in golden state'.format(device))
text_file.write('{} : Device is running in golden state : {} : {}\n'.format(device, sys, bootsys))
else:
print('{}:Device Need to be repaired'.format(device))
text_file.write('{} : Device Need to be repaired : {} : {}\n'.format(device, sys, bootsys))
except:
print('{}:could not test'.format(device))
def application(device, text_file):
local_conn = connection(device)
nameoffile = '{}_{}'.format(device, username)
sys, bootsys, bootflash = needed_info(device, local_conn, text_file)
response = ping(device)
if response:
print ('Device is up Before Change')
if 'n9000-dk9.6.1.2.I3.4e.bin' in sys:
print('Device {} is running in the golden state'.format(device))
text_file.write('{} : Device is running in golden state : {} : {}\n'.format(device, sys, bootsys))
else:
pre_check(device, text_file, local_conn)
sleep(5)
implementation(device, text_file, local_conn)
logger.info('Sending Mail for {} inbox in 2 minutes'.format(device))
print'\n'
mail_it(nameoffile, cc_list=None)
def thread_main_scan(list, text_file):
for host in list:
device = host.strip()
my_thread = threading.Thread(target=check_info, args=(device, text_file))
my_thread.start()
sleep(.5)
my_thread.join()
main_thread = threading.currentThread()
threading.active_count()
for some_thread in threading.enumerate():
if some_thread != main_thread:
print some_thread
some_thread.join()
def multi_threading_scan():
'''Mulit-threading function arg will be command and file name'''
start_time = datetime.now()
getfile = open('devices_{}_scan.txt'.format(username), 'r')
devices = getfile.readlines()
try:
text_file = open("ScanData_{}".format(username), "w")
except Exception as e:
logger.error('Error: {}'.format(e))
sys.exit(1)
list1 = [l.strip() for l in devices]
for i in [list1[x:x + 100] for x in range(0, len(list1), 100)]:
print('MultiThreading currently running for {}'.format(i))
thread_main_scan(i, text_file)
text_file.close()
def thread_main_config(list):
for host in list:
device = host.strip()
text_file = open("ConfigData_3164_{}".format(device), "w")
my_thread = threading.Thread(target=application, args=(device, text_file))
my_thread.start()
sleep(.5)
my_thread.join()
main_thread = threading.currentThread()
threading.active_count()
for some_thread in threading.enumerate():
if some_thread != main_thread:
print some_thread
some_thread.join()
text_file.close()
def multi_threading_config():
'''Mulit-threading function arg will be command and file name'''
start_time = datetime.now()
getfile = open('devices_3164_config.txt'.format(username), 'r')
devices = getfile.readlines()
list1 = [l.strip() for l in devices]
for i in [list1[x:x + 5] for x in range(0, len(list1), 5)]:
print('MultiThreading currently running for {}'.format(i))
thread_main_config(i)
def one_by_one():
getfile = open('devices_3164_config.txt'.format(username), 'r')
devices = getfile.readlines()
for host in devices:
device = host.strip()
logger.info('==================================')
logger.info('Going for {}\n'.format(device))
text_file = open("ConfigData_3164_{}".format(device), "w")
application(device, text_file)
text_file.close()
def main():
'''Main Funcion'''
print '\n'
logger.info('This script will be used for boot var fix only. '
'devices_{}_config.txt for Config devices and devices_{}_scan.txt for scanning devices. '
'output will be saved ScanData_{}.txt and ConfigData_{}.txt respectively' .format(username, username, username, username))
print '\n'
print '\n'
print('\n1) Scanning of Device, checking the bootvar config\n2) Configuring the device, Fixing the bootvar config\n3) Exit')
choice = raw_input('\nPlease provide your choice: ').strip()
# Health check
if choice == '1':
multi_threading_scan()
elif choice == '2':
#multi_threading_config()
one_by_one()
elif choice == '3':
sys.exit()
else:
print('Select some option')
if __name__ == '__main__':
main()