-
Notifications
You must be signed in to change notification settings - Fork 0
/
switchConfigGenerator.py
1451 lines (1298 loc) · 65.2 KB
/
switchConfigGenerator.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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
__author__ = 'Kenneth Buchanan'
import sqlite3
import os
import re
main_directory = r'\some\directory'
# template locations
template_path_legacy = main_directory + r'IDF_Base_Templates\3750X-IDF-BaseTemplate.txt' #3750X Template
template_path = main_directory + r'IDF_Base_Templates\3850-IDF-BaseTemplate.txt' #3850 template
sqlite_file = main_directory + 'SQL_Database\Switches.db'
conn = sqlite3.connect(sqlite_file, check_same_thread=False) #allows different threads to access database. Needed for GUI
c = conn.cursor()
def hostnameSearchAutomatic():
'''search entire hostname column in sql database then run configureHostname function against all hostnames found'''
hostname_column_search = c.execute("SELECT HOSTNAME FROM SWITCHES")
fetch_hostnames = c.fetchall() #variable needs to come after the sql query has been performed
for hostname in fetch_hostnames: #for each hostname found, apply and create configuration
configureHostname(hostname[0]) #index 0 used because fetchall returns tuple per hostname. so 0 is only available. Function is down below
print(len(fetch_hostnames), 'configuration files have been created!', '\n')
#def hostnameSearchDistrict(district):
# '''search for particular district'''
# hostname_column_search = c.execute("SELECT HOSTNAME FROM SWITCHES WHERE DISTRICT=?", (district,))
# fetch_hostnames = c.fetchall() #variable needs to come after the sql query has been performed
# for hostname in fetch_hostnames: #for each hostname found, apply and create configuration
# configureHostname(hostname[0]) #index 0 used because fetchall returns tuple per hostname. so 0 is only available. Function is down below
# print(len(fetch_hostnames), 'configuration files have been created!', '\n')
def convertToStringLower(s):
'''function to convert None type to string, also apply .lower()for consistency'''
return '' if s is None else str(s).lower()
def convertToStringUpper(s):
'''function to convert None type to string, also apply .upper()for consistency'''
return '' if s is None else str(s).upper()
def convertToZeroIfEmpty(s):
'''function to convert empty to 0 for consistency. This won't be needed in future because SQL data validation'''
return 0 if s == '' else s
def convertToNoneLowerStringIfEmpty(s): # used for powerstack
return 'none' if s is None else str(s).lower()
reserved_data_port = \
''' switchport mode access
switchport access vlan 25
no switchport voice vlan
spanning-tree portfast
snmp trap mac-notification change added
snmp trap mac-notification change removed
no logging event link-status
nmsp attachment suppress
'''
reserved_voice_port = \
''' switchport mode access
switchport access vlan 15
no switchport voice vlan
spanning-tree portfast
snmp trap mac-notification change added
snmp trap mac-notification change removed
no logging event link-status
nmsp attachment suppress
'''
#IDFX000 only - branch site's IDFX000 only to have TTD UPS
branchSTTDUpsIDFX000Cmd = \
'''
int gi1/0/5
desc TTD-WAN UPS
''' + reserved_data_port
#IDFX000 only - branch site's IDFX000 only to have server virt
branchServerVirtUpsIDFX000Cmd = \
'''
int gi1/0/6
desc Server Virt;UPS Mgmt
''' + reserved_data_port
#IDFX000 only - branch site's IDFX000 only to have data domain ipmi
branchDatadomainIpmiIDFX000Cmd = \
'''
int gi1/0/7
desc DataDomain 2200 IPMI
''' + reserved_data_port
#IDFX000 only - branch site's IDFX000 only to have svmh21 ilo
branchSvmh21Green1ILOIDFX000Cmd = \
'''
int gi1/0/8
desc SVMH21;Green Cable 1;ILO
''' + reserved_data_port
#IDFX000 only - branch site's IDFX000 only to have panzura ilo
branchPanzuraILOIDFX000Cmd = \
'''
int gi1/0/9
desc Panzura iDRAC (ILO)
''' + reserved_data_port
#---------------------------start analog fax gateway---------------------------------#
hqEvoipFaxGateway2IDF1000Cmd = \
'''
int gi1/0/5
desc E-VOIP FXS ANALOG GATEWAY #2
''' + reserved_voice_port
hqEvoipFaxGateway2IDFX0XXCmd = \
'''
int gi1/0/4
desc E-VOIP FXS ANALOG GATEWAY #2
''' + reserved_voice_port
branchEvoipFaxGateway2IDFX000Cmd = \
'''
int gi1/0/10
desc E-VOIP FXS ANALOG GATEWAY #2
''' + reserved_voice_port
branchEvoipFaxGateway2IDFX0XXCmd = \
'''
int gi1/0/4
desc E-VOIP FXS ANALOG GATEWAY #2
''' + reserved_voice_port
#---------------------------start evoip ups mgmt---------------------------------#
hqEvoipUpsMgmt2IDF1000Cmd = \
'''
int gi1/0/6
desc E-VOIP UPS Mgmt #2
''' + reserved_data_port
hqEvoipUpsMgmt2IDF10XXCmd = \
'''
int gi1/0/5
desc E-VOIP UPS Mgmt #2
''' + reserved_data_port
branchEvoipUpsMgmt2IDFX000Cmd = \
'''
int gi1/0/11
desc E-VOIP UPS Mgmt #2
''' + reserved_data_port
branchEvoipUpsMgmt2IDFX0XXCmd = \
'''
int gi1/0/5
desc E-VOIP UPS Mgmt #2
''' + reserved_data_port
#---------------------------start tape library---------------------------------#
branchOconusMgmtCmdIDFX000 = \
'''
int gi1/0/12
desc OCONUS Tape Library Mgmt
''' + reserved_data_port
#---------------------------start taclanes-------------------------------------#
hqTaclane1IDF1000Cmd = \
'''
int gi1/0/7
desc TACLANE #1
speed 100
duplex full
''' + reserved_data_port
hqTaclane1IDF10XXCmd = \
'''
int gi1/0/6
desc TACLANE #1
speed 100
duplex full
''' + reserved_data_port
hqTaclane2IDF1000Cmd = \
'''
int gi1/0/8
desc TACLANE #2
speed 100
duplex full
''' + reserved_data_port
hqTaclane1IDF10XXCmd = \
'''
int gi1/0/6
desc TACLANE #1
speed 100
duplex full
''' + reserved_data_port
hqTaclane2IDF10XXCmd = \
'''
int gi1/0/7
desc TACLANE #2
speed 100
duplex full
''' + reserved_data_port
branchTaclane1IDFX000Cmd = \
'''
int gi1/0/13
desc TACLANE #1
speed 100
duplex full
''' + reserved_data_port
branchTaclane2IDFX000Cmd = \
'''
int gi1/0/14
desc TACLANE #2
speed 100
duplex full
''' + reserved_data_port
branchTaclane1IDFX0XXCmd = \
'''
int gi1/0/6
desc TACLANE #1
speed 100
duplex full
''' + reserved_data_port
branchTaclane2IDFX0XXCmd = \
'''
int gi1/0/7
desc TACLANE #2
speed 100
duplex full
''' + reserved_data_port
#------------------------packet smart---------------------------------------#
hqPktSmtIDF1000Cmd = \
'''
int gi1/0/4
desc E-VOIP PacketSmart MGMT
''' + reserved_voice_port
branchPktSmtIDFX000Cmd = \
'''
int gi1/0/4
desc E-VOIP PacketSmart MGMT
''' + reserved_voice_port
#---------------------------start tacacs-------------------------------------#
tacacsEastCmd = \
'''aaa group server tacacs+ TACSERVERS
server name ACS_NOC
server name ACS_NOC_2
ip tacacs source-interface vlan 25
tacacs-server directed-request
tacacs server ACS_NOC
address ipv4 192.168.1.2
key somekey
timeout 10
tacacs-server directed-request
tacacs server ACS_SPTC
address ipv4 192.168.1.2
key somekey
timeout 10 '''
tacacsWestCmd = \
'''aaa group server tacacs+ TACSERVERS
server name ACS_SPTC
server name ACS_NOC
ip tacacs source-interface vlan 25
tacacs-server directed-request
tacacs server ACS_SPTC
address ipv4 192.168.1.1
key somekey
timeout 10
tacacs-server directed-request
tacacs server ACS_NOC
address ipv4 192.168.1.1
key somkey
timeout 10 '''
tacacsEast3750XCmd = \
'''
aaa group server tacacs+ TACSERVERS
server 192.168.1.1
server 192.168.1.12
ip tacacs source-interface vlan 25
tacacs-server host 192.168.1.1 timeout 10 key somekey
tacacs-server host 192.168.1.2 timeout 10 key somekey
tacacs-server directed-request '''
tacacsWest3750XCmd = \
'''
aaa group server tacacs+ TACSERVERS
server 10.200.33.36
server 192.168.1.1
ip tacacs source-interface vlan 25
tacacs-server host 192.168.1.1 timeout 10 key somekey
tacacs-server host 192.168.1.2 timeout 10 key somekey
tacacs-server directed-request '''
#---------------------------start ntp-------------------------------------#
ntpEastCmd = \
'''ntp server 192.168.1.5 prefer
ntp server 192.168.1.6
ntp source vlan 25 '''
ntpWestCmd = \
'''ntp server 192.168.1.5 prefer
ntp server 192.168.1.6
ntp source vlan 25 '''
#---------------------------start other port config-----------------------#
rapCfgCmd = \
'''
description ARUBA RAP
no switchport voice vlan
switchport trunk native vlan 25
switchport mode trunk
snmp trap mac-notification change added
snmp trap mac-notification change removed
no logging event link-status
spanning-tree portfast
service-policy input INGRESS-QOS-POLICY
nmsp attachment suppress
no shutdown '''
portCfg = \
'''
desc STANDARD ACCESS PORT
switchport access vlan 25
switchport mode access
switchport voice vlan 15
service-policy input INGRESS-QOS-POLICY
snmp trap mac-notification change added
snmp trap mac-notification change removed
no logging event link-status
nmsp attachment suppress
spanning-tree portfast
no shutdown
'''
splitStackCmd = \
'''
int gi1/1/2
desc TO LEGACY 3750X IDF
'''
#start user port configuration
switch1PortCfg24 = 'int range gi1/0/4-24' + portCfg
switch1PortCfg48 = 'int range gi1/0/4-48' + portCfg
switch13750XPortCfg24 = 'int range gi1/0/2-24' + portCfg #3750X only
switch13750XPortCfg48 = 'int range gi1/0/2-48' + portCfg #3750X only
switch2PortCfg24 = 'int range gi2/0/1-24' + portCfg
switch2PortCfg48 = 'int range gi2/0/1-48' + portCfg
switch3PortCfg24 = 'int range gi3/0/1-24' + portCfg
switch3PortCfg48 = 'int range gi3/0/1-48' + portCfg
switch4PortCfg24 = 'int range gi4/0/1-24' + portCfg
switch4PortCfg48 = 'int range gi4/0/1-48' + portCfg
switch5PortCfg24 = 'int range gi5/0/1-24' + portCfg
switch5PortCfg48 = 'int range gi5/0/1-48' + portCfg
switch6PortCfg24 = 'int range gi6/0/1-24' + portCfg
switch6PortCfg48 = 'int range gi6/0/1-48' + portCfg
switch7PortCfg24 = 'int range gi7/0/1-24' + portCfg
switch7PortCfg48 = 'int range gi7/0/1-48' + portCfg
switch8PortCfg24 = 'int range gi8/0/1-24' + portCfg
switch8PortCfg48 = 'int range gi8/0/1-48' + portCfg
switch9PortCfg24 = 'int range gi9/0/1-24' + portCfg
switch9PortCfg48 = 'int range gi9/0/1-48' + portCfg
#end user port configuration
##end setting some variables##
def configureHostname(hostname):
'''Make an SQL query that selects the entire Switches table and returns all values.
first, you must use the execute statement to execute an SQL command
next, you can treat the cursor as an iterator to get a list of matching rows from the execute statement'''
hostname_row_search = c.execute("SELECT * FROM SWITCHES WHERE Hostname=? COLLATE NOCASE", (hostname,))
fetch_data = c.fetchall() #variable needs to come after the sql query has been performed
if fetch_data != []: # by defualt c.fetchall() will returnan empty list if nothing is found in the database.
for i in fetch_data: #if hostname is not found in the database, run elif and exit program
rowResult = i
## -----------------------------------start Set command variables----------------------------------###
#
site = rowResult[1]
district = rowResult[0].upper()
ipAddress = rowResult[3]
ntpTacacs = convertToStringUpper(rowResult[7])
subnetmask = rowResult[5]
defaultGateway = rowResult[6]
hostname = rowResult[2].upper()
timezone = rowResult[4].upper()
switch1 = convertToStringLower(rowResult[8])
switch2 = convertToStringLower(rowResult[9])
switch3 = convertToStringLower(rowResult[10])
switch4 = convertToStringLower(rowResult[11])
switch5 = convertToStringLower(rowResult[12])
switch6 = convertToStringLower(rowResult[13])
switch7 = convertToStringLower(rowResult[14])
switch8 = convertToStringLower(rowResult[15])
switch9 = convertToStringLower(rowResult[16])
splitStack = convertToZeroIfEmpty(rowResult[20]) #if switch is a split stack then add uplink port desc
branchServerVirtSite = rowResult[18]
panzura_iDRAC_ILO = rowResult[19]
oconusTapeLibrary = rowResult[17]
evoipFaxAnalogGateways = convertToZeroIfEmpty(rowResult[21])
evoipUPS = convertToZeroIfEmpty(rowResult[22])
taclanes = convertToZeroIfEmpty(rowResult[23])
remoteArubaDevices = convertToZeroIfEmpty(rowResult[24])
physicalLocation = convertToStringUpper(rowResult[25])
physicalLocationCmd = 'snmp-server location ' + physicalLocation
physicalLocationCmd2 = 'snmp-server location ' + district + '-' + site
hostnameCmd = 'hostname ' + hostname
domainNameCmd = 'ip domain-name ' + district.lower() + '.usa.doj.gov'
ipAddressCmd = 'ip address ' + ipAddress + ' ' + subnetmask
defaultGatewayCmd = 'ip default-gateway ' + defaultGateway
# time variables below - syntax - timezone clock timezone <usa timezone> <utc offset in hours>
timezoneASTCmd = 'clock timezone AST -4'
timezoneESTCmd = 'clock timezone EST -5'
timezoneCSTCmd = 'clock timezone CST -6'
timezoneMSTCmd = 'clock timezone MST -7'
timezoneAZSTCmd = 'clock timezone AZST -7'
timezonePSTCmd = 'clock timezone PST -8'
timezoneAKSTCmd = 'clock timezone AKST -9'
timezoneHSTCmd = 'clock timezone HST -10'
timezoneCHSTCmd = 'clock timezone CHST +10'
timezoneCDT = 'clock summer-time CDT recurring 2 Sun Mar 2:00 1 Sun Nov 2:00'
timezoneEDT = 'clock summer-time EDT recurring 2 Sun Mar 2:00 1 Sun Nov 2:00'
timezoneMDT = 'clock summer-time MDT recurring 2 Sun Mar 2:00 1 Sun Nov 2:00'
timezonePDT = 'clock summer-time PDT recurring 2 Sun Mar 2:00 1 Sun Nov 2:00'
### ------------------------------------end set command variables ---------------------------------###
# depending on the hostname pattern match, aeither a 3750X config file or a 3850 one
# use regex to determine if switch is hq IDF100, branch IDFX00, hq IDFX0X, or branch IDFXXX for 3750X
# use regex to determine if switch is hq IDF1000, branch IDFX000, hq IDFX0XX, or branch IDFX0XX for 3850
#3750X naming scheme
hqIDF100Pattern = re.compile(r"^.*\D100$") #\D = has to be a non digit character
hqIDF1XXPattern = re.compile(r"^.*\D1..$")
branchIDFX00Pattern = re.compile(r"^.*\D.00$")
branchIDFXXXPattern = re.compile(r"^.*\D...$")
#3850 naming scheme
hqIDF1000Pattern = re.compile(r"^.*\D1000$")
hqIDF10XXPattern = re.compile(r"^.*\D10..$")
branchIDFX000Pattern = re.compile(r"^.*\D.000$")
branchIDFX0XXPattern = re.compile(r"^.*\D.0..$")
#global BaseTemplateFile # python by default will try to search for variable locally in this function
# when you write BaseTemplateFile = BaseTemplateFileLegacy, so need to make it look global
# search name to find HQ, branch and IDF type for port reservations
#3750X
if re.findall(hqIDF100Pattern, hostname):
BaseTemplateFile = template_path_legacy# use 3750X base template instead
site_switch_Type = 'HQ_IDF_100'
elif re.findall(hqIDF1XXPattern, hostname):
BaseTemplateFile = template_path_legacy
site_switch_Type = 'HQ_IDF_1XX'
elif re.findall(branchIDFX00Pattern, hostname):
BaseTemplateFile = template_path_legacy
site_switch_Type = 'BRANCH_IDF_X00'
elif re.findall(branchIDFXXXPattern, hostname):
BaseTemplateFile = template_path_legacy
site_switch_Type = 'BRANCH_IDF_XXX'
#3850
elif re.findall(hqIDF1000Pattern, hostname):
BaseTemplateFile = template_path
site_switch_Type = 'HQ_IDF_1000'
elif re.findall(hqIDF10XXPattern, hostname):
BaseTemplateFile = template_path
site_switch_Type = 'HQ_IDF_10XX'
elif re.findall(branchIDFX000Pattern, hostname):
BaseTemplateFile = template_path
site_switch_Type = 'BRANCH_IDF_X000'
elif re.findall(branchIDFX0XXPattern, hostname):
BaseTemplateFile = template_path
site_switch_Type = 'BRANCH_IDF_X0XX'
else: # catch any other non-standard name.
BaseTemplateFile = template_path
site_switch_Type = 'CANNOT DETERMINE'
### ------------------------------------create config file and folder---------------------------------###
#create path based on district if it doesn't exist
#the r before the string means (raw string literal) so backslashes will not be treated as escape characters
#which mitigates issues with things like regex but this is rarely needed anyway
newpath = main_directory + r'All_District_Configuration_Files\%s\%s' %(district, site)
if not os.path.exists(newpath):
os.makedirs(newpath)
#create config file
ConfigFile = main_directory + r'All_District_Configuration_Files\%s\%s\%s-CONFIG.txt' %(district, site, hostname)
# try to remove the configuration file - if it doesn't exist, create it
try:
os.remove(ConfigFile)
except FileNotFoundError:
pass
#print(" Old Configuration file does not exist. Will now create a new one. Location: " + ConfigFile)
try:
file = open(ConfigFile, 'w')
file.close()
except PermissionError:
print("There seems to be a permissions issue. Ensure you have full permission to " + ConfigFile)
except OSError:
print("Something went wrong when creating the config file!")
### ------------------------------------end create config file and folder---------------------------------###
#find all variables and replace with table data from CSV
#notice the end='', this is new in python 3.x and allows you to specify what you wanted appened to the end of a print
#line. By default it is \n so we use a blank space so it doesn't auto insert a new line
with open(BaseTemplateFile, 'r') as BaseTemplate:
lines = BaseTemplate.readlines()
with open(ConfigFile, 'w') as f:
f.writelines(lines)
# start domain name configuration #
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$domainNameCmd', domainNameCmd)
f.write(filedata)
# end start domain name configuration #
# start hostname configuration #
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$hostnameCmd', hostnameCmd)
f.write(filedata)
# end start hostname configuration #
# start ip address configuration #
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$ipAddressCmd', ipAddressCmd)
f.write(filedata)
# end ip address configuration #
# start default gateway configuration #
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$defaultGatewayCmd', defaultGatewayCmd)
f.write(filedata)
# end default gateway configuration #
# start timezone configuration
# timezones without summer time = AKST, AST, HST, AZST, CHST
# timezones with summertime = CST, EST, MST, PST
with open(ConfigFile, 'r') as f:
filedata = f.read()
if timezone == 'AST': # atlantic time
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$timezoneCmd', timezoneASTCmd)
f.write(filedata)
elif timezone == 'EST': # eastern time
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$timezoneCmd', timezoneESTCmd)
f.write(filedata)
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$timezoneSummerCmd', timezoneEDT)
f.write(filedata)
elif timezone == 'CST': # central time
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$timezoneCmd', timezoneCSTCmd)
f.write(filedata)
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$timezoneSummerCmd', timezoneCDT)
f.write(filedata)
elif timezone == 'MST': # mountain time
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$timezoneCmd', timezoneMSTCmd)
f.write(filedata)
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$timezoneSummerCmd', timezoneMDT)
f.write(filedata)
elif timezone == 'PST': # pacific time
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$timezoneCmd', timezonePSTCmd)
f.write(filedata)
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$timezoneSummerCmd', timezonePDT)
f.write(filedata)
elif timezone == 'AKST': # alaska time
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$timezoneCmd', timezoneAKSTCmd)
f.write(filedata)
elif timezone == 'HST': # hawaii time
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$timezoneCmd', timezoneHSTCmd)
f.write(filedata)
elif timezone == 'CHST': # chamorro standard time (guam)
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$timezoneCmd', timezoneCHSTCmd)
f.write(filedata)
elif timezone == 'AZST': #arizona
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$timezoneCmd', timezoneAZSTCmd)
f.write(filedata)
# end timezone configuration
#start snmp location configuration
if physicalLocation != '':
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$physicalLocationCmd', physicalLocationCmd)
f.write(filedata)
elif physicalLocation == '': #if there's no location, put [District] <Site>
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$physicalLocationCmd', physicalLocationCmd2)
f.write(filedata)
#end snmp location configuration
#start ntp/tacacs configuration
if ntpTacacs == 'EAST':
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$ntpCmd', ntpEastCmd)
f.write(filedata)
if BaseTemplateFile == template_path: # for 3850
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$tacacsCmd', tacacsEastCmd)
f.write(filedata)
elif BaseTemplateFile == template_path_legacy: # for 3750X
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$tacacsCmd', tacacsEast3750XCmd)
f.write(filedata)
elif ntpTacacs == 'WEST':
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$ntpCmd', ntpWestCmd)
f.write(filedata)
if BaseTemplateFile == template_path: # for 3850
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$tacacsCmd', tacacsEastCmd)
f.write(filedata)
elif BaseTemplateFile == template_path_legacy: # for 3750X
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$tacacsCmd', tacacsWest3750XCmd)
f.write(filedata)
#end ntp/tacacs configuraiton
###------------------start Switch1-----------------------###
if switch1 != '':
switch1ProvisionCmd = 'switch 1 provision ' + switch1
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch1ProvisionCmd', switch1ProvisionCmd)
f.write(filedata)
if switch1 == 'ws-c3850-24t' or switch1 == 'ws-c3850-24p':
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch1PortCfg24', switch1PortCfg24)
f.write(filedata)
elif switch1 == 'ws-c3750x-24p': # for 3750X
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch13750XPortCfg24', switch13750XPortCfg24)
f.write(filedata)
elif switch1 == 'ws-c3850-48t' or switch1 == 'ws-c3850-48p':
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch1PortCfg48', switch1PortCfg48)
f.write(filedata)
elif switch1 == 'ws-c3750x-48p': # for 3750X
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch13750XPortCfg48', switch13750XPortCfg48)
f.write(filedata)
###------------------end Switch1-----------------------###
###------------------start Switch2-----------------------###
if switch2 != '':
switch2ProvisionCmd = 'switch 2 provision ' + switch2
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch2ProvisionCmd', switch2ProvisionCmd)
f.write(filedata)
if switch2 == 'ws-c3850-24t' or switch2 == 'ws-c3850-24p' or switch2 == 'ws-c3750x-24p':
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch2PortCfg24', switch2PortCfg24)
f.write(filedata)
elif switch2 == 'ws-c3850-48t' or switch2 == 'ws-c3850-48p' or switch2 == 'ws-c3750x-48p' :
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch2PortCfg48', switch2PortCfg48)
f.write(filedata)
###------------------end Switch2-----------------------###
###------------------start Switch3-----------------------###
if switch3 != '':
switch3ProvisionCmd = 'switch 3 provision ' + switch3
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch3ProvisionCmd', switch3ProvisionCmd)
f.write(filedata)
if switch3 == 'ws-c3850-24t' or switch3 == 'ws-c3850-24p' or switch3 == 'ws-c3750x-24p':
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch3PortCfg24', switch3PortCfg24)
f.write(filedata)
elif switch3 == 'ws-c3850-48t' or switch3 == 'ws-c3850-48p' or switch3 == 'ws-c3750x-48p':
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch3PortCfg48', switch3PortCfg48)
f.write(filedata)
###------------------end Switch3-----------------------###
###------------------start Switch4-----------------------###
if switch4 != '':
switch4ProvisionCmd = 'switch 4 provision ' + switch4
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch4ProvisionCmd', switch4ProvisionCmd)
f.write(filedata)
if switch4 == 'ws-c3850-24t' or switch4 == 'ws-c3850-24p' or switch4 == 'ws-c3750x-24p':
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch4PortCfg24', switch4PortCfg24)
f.write(filedata)
elif switch4 == 'ws-c3850-48t' or switch4 == 'ws-c3850-48p' or switch4 == 'ws-c3750x-48p':
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch4PortCfg48', switch4PortCfg48)
f.write(filedata)
###------------------end Switch4-----------------------###
###------------------start Switch5-----------------------###
if switch5 != '':
switch5ProvisionCmd = 'switch 5 provision ' + switch5 #start putting these in nested if statements?
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch5ProvisionCmd', switch5ProvisionCmd)
f.write(filedata)
if switch5 == 'ws-c3850-24t' or switch5 == 'ws-c3850-24p' or switch5 == 'ws-c3750x-24p' :
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch5PortCfg24', switch5PortCfg24)
f.write(filedata)
elif switch5 == 'ws-c3850-48t' or switch5 == 'ws-c3850-48p' or switch5 == 'ws-c3750x-48p' :
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch5PortCfg48', switch5PortCfg48)
f.write(filedata)
###------------------end Switch5-----------------------###
###------------------start Switch6-----------------------###
if switch6 != '':
switch6ProvisionCmd = 'switch 6 provision ' + switch6
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch6ProvisionCmd', switch6ProvisionCmd)
f.write(filedata)
if switch6 == 'ws-c3850-24t' or switch6.lower() == 'ws-c3850-24p' or switch6 == 'ws-c3750x-24p':
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch6PortCfg24', switch6PortCfg24)
f.write(filedata)
elif switch6 == 'ws-c3850-48t' or switch6.lower() == 'ws-c3850-48p' or switch6 == 'ws-c3750x-48p':
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch6PortCfg48', switch6PortCfg48)
f.write(filedata)
###------------------end Switch6-----------------------###
###------------------start Switch7-----------------------###
if switch7 != '':
switch7ProvisionCmd = 'switch 7 provision ' + switch7
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch7ProvisionCmd', switch7ProvisionCmd)
f.write(filedata)
if switch7 == 'ws-c3850-24t' or switch7 == 'ws-c3850-24p' or switch7 == 'ws-c3750x-24p':
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch7PortCfg24', switch7PortCfg24)
f.write(filedata)
elif switch7 == 'ws-c3850-48t' or switch7 == 'ws-c3850-48p' or switch7 == 'ws-c3750x-48p':
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch7PortCfg48', switch7PortCfg48)
f.write(filedata)
###------------------end Switch7-----------------------###
###------------------start Switch8-----------------------###
if switch8 != '':
switch8ProvisionCmd = 'switch 8 provision ' + switch8
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch8ProvisionCmd', switch8ProvisionCmd)
f.write(filedata)
if switch8 == 'ws-c3850-24t' or switch8 == 'ws-c3850-24p' or switch8 == 'ws-c3750x-24p':
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch8PortCfg24', switch8PortCfg24)
f.write(filedata)
elif switch8 == 'ws-c3850-48t' or switch8 == 'ws-c3850-48p' or switch8 == 'ws-c3750x-48p':
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch8PortCfg48', switch8PortCfg48)
f.write(filedata)
###------------------end Switch8-----------------------###
###------------------start Switch9-----------------------###
if switch9 != '':
switch9ProvisionCmd = 'switch 9 provision ' + switch9
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch9ProvisionCmd', switch9ProvisionCmd)
f.write(filedata)
if switch9 == 'ws-c3850-24t' or switch9 == 'ws-c3850-24p' or switch9 == 'ws-c3750x-24p':
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch9PortCfg24', switch9PortCfg24)
f.write(filedata)
elif switch9 == 'ws-c3850-48t' or switch9 == 'ws-c3850-48p' or switch9 == 'ws-c3750x-48p':
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$switch9PortCfg48', switch9PortCfg48)
f.write(filedata)
###------------------end Switch9-----------------------###
#specific port reservations for sites, hq/branch
if site_switch_Type == 'HQ_IDF_1000': # run this code only if switch is HQ IDF1000
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$hqPktSmtIDF1000Cmd', hqPktSmtIDF1000Cmd)
f.writelines(filedata)
if evoipFaxAnalogGateways == 2: #provision evoip port if needed
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$hqEvoipFaxGateway2IDF1000Cmd', hqEvoipFaxGateway2IDF1000Cmd)
f.writelines(filedata)
if evoipUPS == 2: # provision another UPS mgmt port if needed
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$hqEvoipUpsMgmt2IDF1000Cmd', hqEvoipUpsMgmt2IDF1000Cmd)
f.writelines(filedata)
if taclanes == 1: # provision 1 taclane device
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$hqTaclane1IDF1000Cmd', hqTaclane1IDF1000Cmd)
f.writelines(filedata)
elif taclanes == 2: # provision 2 taclane devices
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$hqTaclane1IDF1000Cmd', hqTaclane1IDF1000Cmd)
f.writelines(filedata)
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$hqTaclane2IDF1000Cmd', hqTaclane2IDF1000Cmd)
f.writelines(filedata)
elif site_switch_Type == 'HQ_IDF_10XX': # run this code only if switch is located at an Branch Site
if evoipUPS == 2: # evoip ups mgmt for branch IDF10XX
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$hqEvoipUpsMgmt2IDF10XXCmd', hqEvoipUpsMgmt2IDF10XXCmd)
f.writelines(filedata)
if taclanes == 1: # provision server virt UPS port, IPMI port, and ILO port
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f: #provision server virt UPS port
filedata = filedata.replace('$hqTaclane1IDF10XXCmd', hqTaclane1IDF10XXCmd)
f.writelines(filedata)
elif taclanes == 2: # provision server virt UPS port, IPMI port, and ILO port
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f: #provision server virt UPS port
filedata = filedata.replace('$hqTaclane1IDF10XXCmd', hqTaclane1IDF10XXCmd)
f.writelines(filedata)
with open(ConfigFile, 'r+') as f: #provision server virt UPS port
filedata = filedata.replace('$hqTaclane2IDF10XXCmd', hqTaclane2IDF10XXCmd)
f.writelines(filedata)
elif site_switch_Type == 'BRANCH_IDF_X000': # run this code only if switch is located at an Branch Site
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$branchPktSmtIDFX000Cmd', branchPktSmtIDFX000Cmd)
f.writelines(filedata)
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f: #always run this code if it's a branch site
filedata = filedata.replace('$branchSTTDUpsIDFX000Cmd', branchSTTDUpsIDFX000Cmd)
f.writelines(filedata)
if branchServerVirtSite == 1: # if server virt exists, provision 3 ports, server virt ups, data domain ilo, scmh21 ilo
with open(ConfigFile, 'r+') as f: # provision a data domain IPMI port
filedata = filedata.replace('$branchServerVirtUpsIDFX000Cmd', branchServerVirtUpsIDFX000Cmd)
f.writelines(filedata)
with open(ConfigFile, 'r+') as f: # provision SVMH ILO port
filedata = filedata.replace('$branchDatadomainIpmiIDFX000Cmd', branchDatadomainIpmiIDFX000Cmd)
f.writelines(filedata)
with open(ConfigFile, 'r+') as f: #provision svmh ilo port
filedata = filedata.replace('$branchSvmh21Green1ILOIDFX000Cmd', branchSvmh21Green1ILOIDFX000Cmd)
f.writelines(filedata)
if panzura_iDRAC_ILO == 1: # panzura for branch IDFX000
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$branchPanzuraILOIDFX000Cmd', branchPanzuraILOIDFX000Cmd)
f.writelines(filedata)
if evoipFaxAnalogGateways == 2: # # analog fax gateway for branch IDFX000
with open(ConfigFile, 'r') as f:
filedata = f.read()
with open(ConfigFile, 'r+') as f:
filedata = filedata.replace('$branchEvoipFaxGateway2IDFX000Cmd', branchEvoipFaxGateway2IDFX000Cmd)
f.writelines(filedata)