-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathac_run_v1.py
722 lines (601 loc) · 20.5 KB
/
ac_run_v1.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
import ac_com
import ac_serial
import serial
import time
'''
Main auto-cisco run file
'''
# THIS IS THE OLD SPAGHETTI CODE
def step0():
while True:
name = input("Enter vehicle name (DT201): ")
with open('./lists/master.csv', 'r') as f:
for line in f:
stripped = line.strip()
split = stripped.split(',')
if split[0] == name:
user_input = input('Matched {}\nFleet IP {}\nCommand IP {}\nIs this correct? (y/n)'.format(split[0],split[2],split[1]))
if user_input == 'y':
print('!!! Power up IW3702 now !!!')
return(split)
elif user_input == 'n':
print("Try again...")
continue
else:
pass
print("No Match")
def step1(com):
print('Checking status....')
while True:
try:
ser = ac_serial.initSerial(com)
break
except serial.serialutil.SerialException:
input("Serial port in use, close putty.")
upgraded = False
loaded = False
command = False
fleet = False
sw_ver = 'None'
for x in range(3):
ac_serial.write(ser, b'\r')
time.sleep(0.5)
while True:
try:
line = ac_serial.read(ser)
except:
pass
if line:
print(line)
else:
ac_serial.write(ser, b'\r')
time.sleep(1)
if line == "ap>":
autonomous = True
if upgraded == False:
time.sleep(1)
ac_serial.write(ser, b'sh ver')
time.sleep(1)
line = ac_serial.read(ser)
print(line)
time.sleep(2)
ac_serial.write(ser, b'\r')
sw_ver = ac_serial.read(ser)
print('-----')
print(sw_ver)
print('-----')
if sw_ver[-42:] == "Version 15.3(3)JI1, RELEASE SOFTWARE (fc1)":
upgraded = True
time.sleep(2)
elif sw_ver[-42:] == "Version 15.3(3)JF5, RELEASE SOFTWARE (fc2)":
upgraded = False
time.sleep(2)
break
else:
break
if line == "ap#":
ac_serial.write(ser, b'exit\r')
print(ac_serial.read(ser))
if line == 'Press RETURN to get started.':
ac_serial.write(ser, b'\r')
if line[:9] == ' --More--':
ac_serial.write(ser, b' ')
ac_serial.write(ser, b'\r')
if line[-13:] == 'image version':
ac_serial.write(ser, b'\r')
if len(line) == 17 and line[:2] == 'AP':
autonomous = False
upgraded = False
command = False
fleet = False
break
elif line[-8:] == 'command#':
autonomous = True
upgraded = True
command = True
fleet = False
break
elif line[-6:] == 'fleet#':
autonomous = True
upgraded = True
command = True
fleet = True
break
ser.close()
return(autonomous, upgraded, command, fleet)
# In autonomous mode
def step2(com):
ser = ac_serial.initSerial(com)
logged_in = False
heater_msg = False
autonomous = False
count = 0
for x in range(3):
ac_serial.write(ser, b'\r')
while True:
try:
line = ac_serial.read(ser)
except:
ac_serial.write(ser, b'\r')
time.sleep(1)
if line:
print(line)
if logged_in:
if line == 'ap>':
break
else:
if line[-22:] == 'existing image version':
ac_serial.write(ser, b'\r')
heater_msg = True
if heater_msg:
if line == 'ap>':
autonomous = True
ac_serial.write(ser, b'login\r')
time.sleep(3)
if line[:9] == 'Username:':
ac_serial.write(ser, b'cisco\r')
time.sleep(3)
if line[:9] == 'Password:':
ac_serial.write(ser, b'Cisco\r')
logged_in = True
ser.close()
def step2p5(com):
print('Step 2.5')
ser = ac_serial.initSerial(com)
logged_in = False
enable = False
debug = False
finished_boot = False
for x in range(3):
ac_serial.write(ser, b'\r')
while True:
try:
line = ac_serial.read(ser)
except:
pass
if line:
print(line)
if logged_in:
if enable and not debug:
ac_serial.write(ser, b'debug capwap console cli')
time.sleep(1)
ac_serial.write(ser, b'\n')
debug = True
if enable and debug:
if line[-11:] == '(yes/[no]):':
ac_serial.write(ser, b'yes')
time.sleep(1)
ac_serial.write(ser, b'\n')
elif len(line) == 17 and line[:2] == 'AP':
ac_serial.write(ser, b'capwap ap autonomous')
line = ac_serial.read(ser)
print(line)
time.sleep(1)
ac_serial.write(ser, b'\n')
line = ac_serial.read(ser)
print(line)
ac_serial.write(ser, b'yes')
time.sleep(1)
ac_serial.write(ser, b'\n')
line = ac_serial.read(ser)
print(line)
break
elif line[-1:] == '#':
time.sleep(1)
ac_serial.write(ser, b'\n')
else:
if len(line) == 17 and line[:2] == 'AP':
time.sleep(1)
ac_serial.write(ser, b'en')
line = ac_serial.read(ser)
print(line)
time.sleep(1)
ac_serial.write(ser, b'\n')
line = ac_serial.read(ser)
print(line)
time.sleep(1)
if line[:9] == 'Password:':
time.sleep(1)
ac_serial.write(ser, b'Cisco')
time.sleep(0.5)
ac_serial.write(ser, b'\n')
enable = True
else:
if finished_boot:
if len(line) == 17 and line[:2] == 'AP':
time.sleep(1)
ac_serial.write(ser, b'login')
line = ac_serial.read(ser)
print(line)
time.sleep(1)
ac_serial.write(ser, b'\r')
line = ac_serial.read(ser)
print(line)
time.sleep(3)
line = ac_serial.read(ser)
print(line)
if line[:9] == 'Username:':
ac_serial.write(ser, b'cisco\r')
line = ac_serial.read(ser)
print(line)
time.sleep(3)
if line[:9] == 'Password:':
ac_serial.write(ser, b'Cisco')
time.sleep(3)
line = ac_serial.read(ser)
print(line)
ac_serial.write(ser, b'\r')
line = ac_serial.read(ser)
print(line)
logged_in = True
else:
print("Waiting for the bootup to complete...")
if line[-25:] == 'Invoking capwap discovery':
finished_boot = True
elif line[:2] == 'AP':
finished_boot = True
return(True, False)
# Setting ip
def step3(com):
print('Step 3')
ser = ac_serial.initSerial(com)
enable = False
conft = False
for x in range(3):
ac_serial.write(ser, b'\r')
while True:
try:
line = ac_serial.read(ser)
except:
pass
if line:
print(line)
else:
ac_serial.write(ser, b'\r')
if enable == True:
if conft:
if line[:11] =='ap(config)#':
time.sleep(1)
ac_serial.write(ser, b'int bvi1\r')
elif line[:14] == 'ap(config-if)#':
time.sleep(1)
ac_serial.write(ser, b'ip address 10.0.0.2 255.255.255.0\r')
print(ac_serial.read(ser))
time.sleep(1)
ac_serial.write(ser, b'exit\r')
print(ac_serial.read(ser))
time.sleep(1)
ac_serial.write(ser, b'exit\r')
print(ac_serial.read(ser))
break
elif line == 'ap#':
ac_serial.write(ser, b'conf t\r')
conft = True
else:
if line == 'ap>':
ac_serial.write(ser, b'en')
ac_serial.write(ser, b'\n')
time.sleep(1)
if line[:9] == 'Password:':
time.sleep(1)
ac_serial.write(ser, b'Cisco')
time.sleep(0.5)
ac_serial.write(ser, b'\n')
enable = True
if line[:9] == ' --More--':
ac_serial.write(ser, b' ')
ac_serial.write(ser, b'\r')
def step4(com):
print('Step 4')
ser = ac_serial.initSerial(com)
for x in range(3):
ac_serial.write(ser, b'\r')
while True:
try:
line = ac_serial.read(ser)
except:
ac_serial.write(ser, b'\r')
time.sleep(1)
if line:
print(line)
if line == 'ap#':
time.sleep(1)
ac_serial.write(ser, b'archive download-sw /overwrite /force-reload tftp://10.0.0.5/ap3g2-k9w7-tar.153-3.JI1.tar')
time.sleep(1)
ac_serial.write(ser, b'\r')
elif line == 'ap>':
ac_serial.write(ser, b'en')
ac_serial.write(ser, b'\n')
time.sleep(1)
elif line[:9] == 'Password:':
time.sleep(1)
ac_serial.write(ser, b'Cisco')
time.sleep(0.5)
ac_serial.write(ser, b'\n')
enable = True
elif line[-18:] == 'console by console':
time.sleep(1)
ac_serial.write(ser, b'\r')
elif line[-22:] == 'existing image version':
break
elif line[:9] == ' --More--':
ac_serial.write(ser, b' ')
ac_serial.write(ser, b'\r')
def step5(com):
print('Step 5')
ser = ac_serial.initSerial(com)
for x in range(3):
ac_serial.write(ser, b'\r')
while True:
try:
line = ac_serial.read(ser)
except:
pass
if line:
print(line)
if line == 'ap>':
ac_serial.write(ser, b'en')
ac_serial.write(ser, b'\n')
time.sleep(1)
if line[:9] == 'Password:':
time.sleep(1)
ac_serial.write(ser, b'Cisco')
time.sleep(0.5)
ac_serial.write(ser, b'\n')
if line == 'ap#':
break
def step6(com, vehicle):
print('Step 6')
ser = ac_serial.initSerial(com)
fleet_copy = False
command_copy = False
for x in range(3):
ac_serial.write(ser, b'\r')
while True:
try:
line = ac_serial.read(ser)
except:
pass
if line:
print(line)
else:
ac_serial.write(ser, b'\r')
if line == "ap#":
fleet_file = 'copy tftp://10.0.0.5/cb-{}-fleet.txt flash:'.format(vehicle[0])
fleet_b = str.encode(fleet_file)
command_file = 'copy tftp://10.0.0.5/cb-{}-command.txt flash:'.format(vehicle[0])
command_b = str.encode(command_file)
if not fleet_copy:
ac_serial.write(ser, fleet_b)
line = ac_serial.read(ser)
print(line)
time.sleep(1)
ac_serial.write(ser, b'\r')
line = ac_serial.read(ser)
print(line)
time.sleep(3)
ac_serial.write(ser, b'\r')
line = ac_serial.read(ser)
print(line)
time.sleep(2)
ac_serial.write(ser, b'\r')
line = ac_serial.read(ser)
print(line)
time.sleep(2)
fleet_copy = True
if not command_copy:
ac_serial.write(ser, b'\r')
line = ac_serial.read(ser)
print(line)
time.sleep(1)
ac_serial.write(ser, command_b)
line = ac_serial.read(ser)
print(line)
time.sleep(1)
ac_serial.write(ser, b'\r')
line = ac_serial.read(ser)
print(line)
time.sleep(3)
ac_serial.write(ser, b'\r')
line = ac_serial.read(ser)
print(line)
time.sleep(2)
ac_serial.write(ser, b'\r')
line = ac_serial.read(ser)
print(line)
time.sleep(1)
command_copy = True
if fleet_copy and command_copy:
return(True)
def step7(com, vehicle):
print('Step 7')
ser = ac_serial.initSerial(com)
for x in range(3):
ac_serial.write(ser, b'\r')
while True:
try:
line = ac_serial.read(ser)
except:
pass
if line:
print(line)
else:
ac_serial.write(ser, b'\r')
if line == 'ap#':
load_fleet = 'copy flash:cb-{}-fleet.txt running-config'.format(vehicle[0])
fleet_b = str.encode(load_fleet)
load_command = 'copy flash:cb-{}-command.txt running-config'.format(vehicle[0])
command_b = str.encode(load_command)
ac_serial.write(ser, command_b)
line = ac_serial.read(ser)
print(line)
time.sleep(1)
ac_serial.write(ser, b'\r')
line = ac_serial.read(ser)
print(line)
time.sleep(1)
ac_serial.write(ser, b'\r')
line = ac_serial.read(ser)
print(line)
time.sleep(1)
if line[-8:] == 'command#':
print("Command config loaded")
break
return(True)
def step8(com):
print('Step 8')
ser = ac_serial.initSerial(com)
ping = False
for x in range(3):
ac_serial.write(ser, b'\r')
while True:
try:
line = ac_serial.read(ser)
except:
pass
if line:
print(line)
else:
ac_serial.write(ser, b'\r')
if line[-8:] == 'command#':
if not ping:
ac_serial.write(ser, b'ping 10.221.64.1\r')
for x in range(5):
line = ac_serial.read(ser)
print(line)
if line[:7] == 'Success' or line[:1] == '!':
return(True)
else:
pass
def step9(com, vehicle):
print('Step 7')
ser = ac_serial.initSerial(com)
for x in range(3):
ac_serial.write(ser, b'\r')
while True:
try:
line = ac_serial.read(ser)
except:
pass
if line:
print(line)
else:
ac_serial.write(ser, b'\r')
if line[-8:] == 'command#':
load_fleet = 'copy flash:cb-{}-fleet.txt running-config'.format(vehicle[0])
fleet_b = str.encode(load_fleet)
load_command = 'copy flash:cb-{}-command.txt running-config'.format(vehicle[0])
command_b = str.encode(load_command)
ac_serial.write(ser, fleet_b)
line = ac_serial.read(ser)
print(line)
time.sleep(1)
ac_serial.write(ser, b'\r')
line = ac_serial.read(ser)
print(line)
time.sleep(1)
ac_serial.write(ser, b'\r')
line = ac_serial.read(ser)
print(line)
time.sleep(1)
if line[-6:] == 'fleet#':
print("Fleet config loaded")
return(True)
def step10(com):
print('Step 10')
ser = ac_serial.initSerial(com)
for x in range(3):
ac_serial.write(ser, b'\r')
line = ac_serial.read(ser)
if line[-6:] == 'fleet#':
print("Fleet config loaded")
ac_serial.write(ser, b'wr mem\r')
line = ac_serial.read(ser)
print(line)
time.sleep(1)
ac_serial.write(ser, b'\r')
line = ac_serial.read(ser)
print(line)
time.sleep(1)
return(True)
def main():
while True:
print("WARNING: This script is SUPER buggy, always verify after use")
time.sleep(3)
com = ac_com.get()
configs = False
vehicle = False
ping = False
fleet = False
complete = False
vehicle = step0()
autonomous, upgraded, command, fleet = step1(com)
while True:
if not complete:
if command and not fleet:
configs = True
ping = step8(com)
elif fleet and command:
configs = True
ping = True
complete = step10(com)
elif autonomous and upgraded:
if not command:
print('In autonomous')
print('Upgraded')
step5(com)
elif command:
ping = step8(com)
elif autonomous and not upgraded:
print("Autonomous")
print("Not upgraded")
step3(com)
print("IP Set")
step4(com)
upgraded = True
elif not autonomous and not upgraded:
print("Not autonomous")
print("Not upgraded")
autonomous, upgraded = step2p5(com)
step3(com)
print("IP Set")
step4(com)
upgraded = True
if vehicle and not configs:
if not command and not configs:
step5(com)
configs = step6(com, vehicle)
command = step7(com, vehicle)
elif not vehicle and not configs:
step4(com)
elif vehicle and configs:
if not command:
command = step7(com, vehicle)
if command and not ping:
ping = step8(com)
elif command and ping:
if not fleet and not complete:
fleet = step9(com, vehicle)
complete = step10(com)
if fleet and not complete:
complete = step10(com)
elif fleet and complete:
print('HOLY FUCK made it to the end')
print('----------------------------')
print(' VEHICLE: {}'.format(vehicle[0]))
print(' FLEET IP: {}'.format(vehicle[2]))
print('COMMAND IP: {}'.format(vehicle[1]))
print('----------------------------')
input('Press ENTER to start again...')
break
elif complete:
print('Already Completed')
print('----------------------------')
print(' VEHICLE: {}'.format(vehicle[0]))
print(' FLEET IP: {}'.format(vehicle[2]))
print('COMMAND IP: {}'.format(vehicle[1]))
print('----------------------------')
input('Press ENTER to start again...')
break
if __name__ == "__main__":
main()