-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaptures.py
More file actions
371 lines (282 loc) · 13.3 KB
/
captures.py
File metadata and controls
371 lines (282 loc) · 13.3 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
359
360
361
362
363
364
365
366
367
368
369
370
371
import netmiko
import time
from netmiko import NetMikoTimeoutException, NetMikoAuthenticationException
from netmiko.ssh_exception import NetMikoTimeoutException, NetMikoAuthenticationException
import threading
class outPutcaptures :
matchLock = threading.Lock()
def CaptureOutput(self,session,cmd,Cap_Cnt,Flag,prmpt):
data = "NF"
showFlag= "N"
if "show running-config" in cmd or 'show run' in cmd or "show inve" in cmd or "show configuration | no-more" in cmd or "show configuration | no-more | display set" in cmd or "show interface terse" in cmd or "show run-config commands" in cmd:
DF = 4
else:
DF = Cap_Cnt + 3
try :
#res = session.send_command(cmd,strip_prompt=False)
#res = session.send_command_timing(cmd,delay_factor=2,max_loops=150,strip_prompt=False,strip_command=True, normalize=True )
#res = session.send_command_expect(cmd,delay_factor=DF,strip_prompt=False, normalize=True )
res = session.send_command_expect(cmd,strip_prompt=False)
time.sleep(0.1)
if "error: error communicating with fpc" in res or "error: error communicating" in res:
time.sleep(5)
#res = session.send_command(cmd,strip_prompt=False)
#res = session.send_command_timing(cmd,delay_factor=2,max_loops=150,strip_prompt=False,strip_command=True, normalize=True )
#res = session.send_command_expect(cmd,delay_factor=DF,strip_prompt=False, normalize=True )
res = session.send_command_expect(cmd,strip_prompt=False)
time.sleep(0.1)
if "error: error communicating with fpc" in res or "error: error communicating" in res:
time.sleep(0.5)
Flag = 'F'
return res,Cap_Cnt,Flag
if prmpt in res:
Flag = 'S'
else:
Flag = 'F'
res = "Bad result"
return res,Cap_Cnt,Flag
except IOError :
Flag= 'F'
Cap_Cnt = Cap_Cnt +1
if Cap_Cnt <= 4:
time.sleep(1.5)
result_tmp,Cap_Cnt_tmp,Flag_tmp= self.CaptureOutput(session,cmd,Cap_Cnt,Flag,prmpt)
return result_tmp,Cap_Cnt_tmp,Flag_tmp
else:
Cap_Cnt = Cap_Cnt +1
return "Bad result",Cap_Cnt,"F"
except NetMikoTimeoutException :
Flag= 'F'
Cap_Cnt = Cap_Cnt +1
if Cap_Cnt <= 4:
time.sleep(2)
result_tmp,Cap_Cnt_tmp,Flag_tmp= self.CaptureOutput(session,cmd,Cap_Cnt,Flag,prmpt)
return result_tmp,Cap_Cnt_tmp,Flag_tmp
else:
Cap_Cnt = Cap_Cnt +1
return "Bad result",Cap_Cnt,"F"
except Exception:
Flag= 'F'
Cap_Cnt = Cap_Cnt +1
if Cap_Cnt <= 4:
time.sleep(1.5)
result_tmp,Cap_Cnt_tmp,Flag_tmp = self.CaptureOutput(session,cmd,Cap_Cnt,Flag,prmpt)
return result_tmp,Cap_Cnt_tmp,Flag_tmp
else :
Flag= 'F'
Cap_Cnt = Cap_Cnt +1
result_tmp = "Bad result"
return result_tmp,Cap_Cnt,Flag
#return res, Cap_Cnt, Flag
# ******************** below is for other than show
def CaptureOutput_WLC(self,session,cmd,Cap_Cnt,Flag,prmpt):
#res = " "
data = "NF"
showFlag= "N"
DF= 1
if cmd == "\n":
DF = 0.3
try :
#session.clear_buffer()
#res = session.send_command_expect(cmd,delay_factor= DF)
res = session.send_command(cmd)
time.sleep(0.2)
if prmpt in res:
Flag = 'S'
else:
Flag = 'F'
return res,Cap_Cnt,Flag
except IOError :
#session.clear_buffer()
Flag= 'F'
Cap_Cnt = Cap_Cnt +1
if Cap_Cnt <= 4:
time.sleep(1.5)
result_tmp,Cap_Cnt_tmp,Flag_tmp= self.CaptureOutput_WLC(session,cmd,Cap_Cnt,Flag,prmpt)
return result_tmp,Cap_Cnt_tmp,Flag_tmp
else:
return " ",Cap_Cnt,Flag
except NetMikoTimeoutException :
# session.clear_buffer()
Flag= 'F'
Cap_Cnt = Cap_Cnt +1
if Cap_Cnt <= 4:
time.sleep(1.5)
result_tmp,Cap_Cnt_tmp,Flag_tmp= self.CaptureOutput_WLC(session,cmd,Cap_Cnt,Flag,prmpt)
return result_tmp,Cap_Cnt_tmp,Flag_tmp
else:
return " ",Cap_Cnt,Flag
except Exception:
# session.clear_buffer()
Flag= 'F'
Cap_Cnt = Cap_Cnt +1
if Cap_Cnt <= 4:
time.sleep(1)
result_tmp,Cap_Cnt_tmp,Flag_tmp = self.CaptureOutput_WLC(session,cmd,Cap_Cnt,Flag,prmpt)
return result_tmp,Cap_Cnt_tmp,Flag_tmp
else :
Flag= 'F'
Cap_Cnt = Cap_Cnt +1
result_tmp = "Error"
return result_tmp,Cap_Cnt_tmp,Flag
def RunOtherCommands(self,session,cmd,Cap_Cnt,Flag,FileHand):
data = "NF"
showFlag= "N"
DF= 3
res = " "
try :
prompt = session.find_prompt()
if cmd.startswith("wr") or cmd.startswith("wri") :
if session.check_config_mode() :
session.exit_config_mode()
res = session.send_command(cmd,delay_factor= DF)
#res = session.send_command("\n")
Flag = 'S'
return res, Cap_Cnt, Flag,prompt
if cmd.startswith("set") or cmd.startswith("delete") :
res = session.send_command(cmd,delay_factor= DF)
#FileHand.write("\n")
#FileHand.write(res)
#res = session.send_command("\n")
elif cmd.startswith("commit"):
res = session.send_command(cmd,delay_factor= DF)
FileHand.write(res)
elif cmd.startswith("reload") or cmd.startswith("reboot"):
if session.check_config_mode() :
session.exit_config_mode()
res = session.send_command(cmd,delay_factor= DF)
FileHand.write("\n")
FileHand.write(res)
time.sleep(0.5)
res = session.send_command("\n\r")
res = session.send_command("\n\r")
elif cmd.startswith("copy"):
if session.check_config_mode() :
session.exit_config_mode()
res = session.send_command(cmd,delay_factor= DF)
#time.sleep(0.2)
#FileHand.write("\n")
#FileHand.write(res)
#res = session.send_command("\n")
#res = session.send_command("\n\n")
elif cmd.startswith("conf"):
CNF ="T"
res = session.config_mode()
Flag = 'S'
res = " "
prompt = session.find_prompt()
return res,Cap_Cnt, Flag,prompt
elif cmd.startswith("exit") or cmd.startswith("exi") :
session.exit_config_mode()
res = " "
Flag = 'S'
tem= session.send_command("\n")
prompt = session.find_prompt()
#return res,Cap_Cnt,Flag,prompt
#res = session.send_command(cmd)
else:
res = session.send_command(cmd)
#print(res)
if "Halt the system ?" in res:
FileHand.write("\n")
FileHand.write(res)
res= " "
#print("I have reached halt section")
res = session.send_command("yes")
res = session.send_command("\n")
FileHand.write("\n")
FileHand.write(res)
time.sleep(0.3)
#res = session.send_command(cmd,delay_factor= DF)
Flag = 'S'
if res == None or res =='':
res = " "
prompt = session.find_prompt()
#return res,Cap_Cnt,Flag,prompt
if "?" in res or "confirm" in res:
FileHand.write("\n")
FileHand.write(res)
res= " "
res = session.send_command("\n")
#if "?" not in res or "confirm" not in res:
# res = session.send_command("\n")
if "?" in res or "confirm" in res:
FileHand.write("\n")
FileHand.write(res)
res = " "
res = session.send_command("\n")
if "?" not in res or "confirm" not in res:
res = session.send_command("\n")
#data = res.encode('utf-8')
if res == None or res =='':
res = " "
Flag = 'S'
return res,Cap_Cnt,Flag,prompt
except IOError :
#session.clear_buffer()
Flag= 'F'
Cap_Cnt = Cap_Cnt +1
if Cap_Cnt <= 4:
time.sleep(1.5)
result_tmp,Cap_Cnt_tmp,Flag_tmp,pmtmp= self.RunOtherCommands(session, cmd, Cap_Cnt, Flag, FileHand)
return result_tmp,Cap_Cnt_tmp,Flag_tmp,pmtmp
else:
return " ",Cap_Cnt,Flag," "
except NetMikoTimeoutException :
#session.clear_buffer()
Flag= 'F'
Cap_Cnt = Cap_Cnt +1
if Cap_Cnt <= 4:
time.sleep(2)
result_tmp,Cap_Cnt_tmp,Flag_tmp,pmtmp= self.RunOtherCommands(session, cmd, Cap_Cnt, Flag, FileHand)
return result_tmp,Cap_Cnt_tmp,Flag_tmp,pmtmp
else:
return " ",Cap_Cnt,Flag," "
except ValueError:
#session.clear_buffer()
Flag= 'F'
Cap_Cnt = Cap_Cnt +1
if Cap_Cnt <= 4:
time.sleep(1)
result_tmp,Cap_Cnt_tmp,Flag_tmp,pmtmp= self.RunOtherCommands(session, cmd, Cap_Cnt, Flag, FileHand)
return result_tmp,Cap_Cnt_tmp,Flag_tmp,pmtmp
else:
return " ",Cap_Cnt,Flag," "
except Exception:
Flag= 'F'
Cap_Cnt = Cap_Cnt +1
if Cap_Cnt <= 4:
time.sleep(1)
result_tmp,Cap_Cnt_tmp,Flag_tmp,pmtmp= self.RunOtherCommands(session, cmd, Cap_Cnt, Flag, FileHand)
return result_tmp,Cap_Cnt_tmp,Flag_tmp,pmtmp
else :
Flag= 'F'
Cap_Cnt = Cap_Cnt +1
result_tmp = "Error"
pmtmp= " "
return result_tmp,Cap_Cnt_tmp,Flag,pmtmp
def writeFlatfile(self,logpath,ipadd,hostname,Final_log_path):
self.matchLock.acquire()
try :
pathhandle = open(logpath,"r")
flathandle = open(Final_log_path + "\\Matches_Found_All.txt","a+")
hostname = hostname.rstrip('#')
hostname = hostname.rstrip('>')
flathandle.write("\n\n")
flathandle.write("===============================================================================\n\n")
flathandle.write("\n\n")
except Exception as uex:
print("There is issue with File operation...", ipadd)
print(uex )
return False
while(1):
line = pathhandle.readline()
if not line:break
flathandle.write(line)
time.sleep(0.1)
if not line : continue
flathandle.write("\n")
flathandle.close()
pathhandle.close()
self.matchLock.release()
return True