-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5drss-0.0.600.py
2524 lines (1995 loc) · 96.6 KB
/
5drss-0.0.600.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
#!/usr/bin/python3
import copy
import ipaddress
import os
from os import name, tcgetpgrp
import re
import sys
from prettytable import PrettyTable, ALL
import weakref #weak ref, only way to avoid issues with circular reference
import enum
import argparse
from collections import deque
import pathlib
from pathlib import Path
import xlsxwriter
#batch use: ls ../5health/ | grep .conf | cut -d "_" -f1 | uniq | while read i; do ./5drss-0.0.404.py -b ../5health/${i}_bigip_base.conf -t ../5health/${i}_bigip.conf ; done
global bigipbaseList
bigipbaseList = []
global bigipList
bigipList = []
global xlsxExport
xlsxExport=False
global targetXlsxFile
targetXlsxFile = ''
#########################
#File Operations
#########################
def checkBigipFile(bigipName):
#Make sure the file can be opened
try:
bigipFile = open(bigipName, 'r')
except OSError:
print ('Could not open/read orignal config file:', bigipName)
sys.exit()
#Make sure the file is readable
try:
line = bigipFile.readline()
except:
print ('This file doesn\'t appear to be readable:', bigipName)
sys.exit()
else:
if not "#TMSH-VERSION:" in line:
print("This file doesn't appear to be a bigip configuration file.", bigipName)
sys.exit()
return bigipFile
def buildBigipFileListFromPath(directory):
global bigipbaseList, bigipList
bigipbaseList=[]
for filepath in pathlib.Path(directory).glob('**/*bigip_base.conf'):
#print(filepath.absolute())
bigipbaseList.append(filepath.absolute())
bigipList=[]
for filepath in pathlib.Path(directory).glob('**/*bigip.conf'):
#print(filepath.absolute())
bigipList.append(filepath.absolute())
def reorderBigipFileList():
global bigipbaseList, bigipList
baseCommonFileCounter=0
commonFileCounter=0
for f in bigipbaseList:
baseCommonFile=checkBigipFile(f)
for line in baseCommonFile:
if "net route-domain /Common/0 {" in line:
#print('found common bigipbase.conf',f)
baseCommonFileCounter+=1
ff=f
break
baseCommonFile.close()
for g in bigipList:
commonFile=checkBigipFile(g)
for line in commonFile:
if "ltm default-node-monitor {" in line:
#print('found common bigip.conf',g)
commonFileCounter+=1
gg=g
break
commonFile.close()
if baseCommonFileCounter>1 or baseCommonFileCounter==0:
print('More or less than 1 bigip_base.conf found, aborting')
sys.exit()
if commonFileCounter>1 or commonFileCounter==0:
print('More or less than 1 bigip.conf found, aborting')
sys.exit()
bigipbaseList.remove(ff)
bigipbaseList.insert(0, ff)
bigipList.remove(gg)
bigipList.insert(0, gg)
#########################
#Execution parameters
#########################
class view(enum.IntEnum):
literal = 0
insights = 1
reverse = 2
# wide = 3
class mode(enum.IntEnum):
brief = 1
full = 2
extended = 3
# debug = 4
#########################
#Comments
#########################
class criticality(enum.IntEnum):
#based on syslog
error = 3
warning = 4
info = 6
normal = 100
#########################
#Helpers
#########################
def isolate(config):
return re.sub('%[0-9]*', '', config.replace('{', '').replace('\n', '').replace(' ', '').lstrip().rstrip())
def determineIpType(address):
try:
if type(ipaddress.ip_address((re.split('/',str(address))[0]))) is ipaddress.IPv4Address:
return(4)
elif type(ipaddress.ip_address((re.split('/',str(address))[0]))) is ipaddress.IPv6Address:
return(6)
except:
#print('not an ip address, probably an fqdn')
return(0)
def objectTypeToConfigurationArray(ltmObjectType, objectArray):
n=ltmObjectType.__name__
n += 's'
if n=='rds':n='routeDomains' #FIX THIS
return getattribute(objectArray, n )
def getattribute(obj, attribute):
if attribute==None:
return None
if type(obj)==ltmObject:
return None
else:
if "weakref" in str(obj):
try:
return getattr(obj(), attribute)
except AttributeError:
#print("There is no such attribute")
return None
else:
try:
return getattr(obj, attribute)
except AttributeError:
#print("There is no such attribute")
return None
def flag(priority):
if (priority==criticality.error):
f = "\033[1m\033[91m[Error]\033[0m\033[0m"
if (priority==criticality.warning):
f ="\033[1m\033[93m[Warning]\033[0m\033[0m"
if (priority==criticality.info):
f = "\033[1m\033[92m[Info]\033[0m\033[0m"
return f
def unformat(description):
return description.replace("%s ","").replace(": %s","")
def chunks(objectArray, chunkSize):
"""Yield successive n-sized chunks from lst."""
for i in range(0, len(objectArray), chunkSize):
yield objectArray[i:i + chunkSize]
def colorize(string,p=criticality.normal):
tmp = string.split('\n')
tmp2 = ''
for t in tmp:
if(p==criticality.error):
tmp2+=('\033[91m'+str(t)+'\033[0m')+'\n'
elif(p==criticality.warning):
tmp2+=('\033[93m'+str(t)+'\033[0m')+'\n'
elif(p==criticality.info):
tmp2+=('\033[92m'+str(t)+'\033[0m')+'\n'
elif(p==94): #tofix
tmp2+=('\033[94m'+str(t)+'\033[0m')+'\n'
elif(p==33): #tofix
tmp2+=('\033[35m'+str(t)+'\033[0m')+'\n'
else:
tmp2+=('\033[2m'+str(t)+'\033[0m')+'\n'
return tmp2.rstrip('\n')
def underlinize(string):
return(('\033[4m'+str(string)+'\033[0m').strip())
def bolderize(string):
return(('\033[1m'+str(string)+'\033[0m').strip())
def extractRD(address):
if '%' in address:
r = re.search('%[0-9]*', address)
r=r.group(0)
return r.replace('%','')
else:
return '0'
def pause():
#input("\nPress Enter to continue...\n")
pass
def noInfo():
print('\n no information to display in this view \n')
#########################
#Config operations
#########################
def removeConfigSegment(configSegment, pattern):
queue = deque([])
patternMatched=False
patternStart = re.compile(pattern)
patternOpenBracket = re.compile('.*{.*')
patternCloseBracket = re.compile('.*}.*')
for line in configSegment.split('\n'):
queue.append(line+'\n')
if patternStart.search(line):
patternMatched=True
if patternCloseBracket.search(line):
if(patternMatched==True):
while patternOpenBracket.search(queue[-1])==None:
queue.pop()
if(patternStart.search(queue[-1])!=None):
patternMatched=False
queue.pop()
resultConfigSegment=''.join(queue)
return resultConfigSegment
def extractConfigSegment(configSegment, pattern):
patternMatched=False
patternStart = re.compile(pattern)
patternOpenBracket = re.compile('.*{.*')
patternCloseBracket = re.compile('.*}.*')
patternNestedInline = re.compile('[\s]*.*{.*}.*')
counterNest=0
resultConfigSegment =''
for line in configSegment.split('\n'):
if patternStart.search(line):
patternMatched=True
counterNest+=1
resultConfigSegment+=line+'\n'
elif patternNestedInline.search(line):
if(patternMatched and counterNest>0):
resultConfigSegment+=line+'\n'
elif patternCloseBracket.search(line):
if(patternMatched and counterNest>0):
resultConfigSegment+=line+'\n'
counterNest-=1
if(counterNest==0):
patternMatched=False
elif patternOpenBracket.search(line):
if(patternMatched and counterNest>0):
resultConfigSegment+=line+'\n'
counterNest+=1
else:
if(patternMatched and counterNest>0):
resultConfigSegment+=line+'\n'
return resultConfigSegment
######################
# Reporting
######################
class comment:
def __init__(self='none', description=None, priority=None): #Check None here
self.description: str = description
self.priority: int = priority
self.objects = []
def populate():
print("\n\r")
print('[*] Initializing Comments Table - ', end='')
result.comments[0] = (comment('Dummy',0))
result.comments[10] = (comment('VLAN %s has no IPv4 network attached.',criticality.error)) #Orphan
result.comments[11] = (comment('VLAN %s has more than one IPv4 network attached.',criticality.error)) #Orphan
result.comments[12] = (comment('VLAN %s has no IPv6 network attached.',criticality.error)) #Orphan
result.comments[13] = (comment('VLAN %s has more than one IPv6 network attached.',criticality.error)) #Orphan
result.comments[14] = (comment('VLAN %s is used for a route next-hop.',criticality.info))
result.comments[101] = (comment('VLAN %s has no IPv4 Self-IP.',criticality.error)) #Orphan
result.comments[102] = (comment('VLAN %s has no Static IPv4 Self-IP.',criticality.error))
result.comments[103] = (comment('VLAN %s has no Floating IPv4 Self-IP.',criticality.error))
result.comments[104] = (comment('VLAN %s has multiple Static IPv4 Self-IP.',criticality.error))
result.comments[105] = (comment('VLAN %s has multiple Floating IPv4 Self-IP.',criticality.error))
result.comments[106] = (comment('VLAN %s is Client-Side only (Only VS) for IPv4.',criticality.info))
result.comments[107] = (comment('VLAN %s is Server-Side only (Only Nodes) for IPv4.',criticality.info))
result.comments[108] = (comment('VLAN %s is shared Client/Server-Side (VS and Nodes) for IPv4.',criticality.info))
result.comments[109] = (comment('VLAN %s has no LTM objects (No VS and no Nodes) for IPv4.',criticality.warning))#Orphan
result.comments[110] = (comment('VLAN %s has IPv4 self-IP addresses on multiple subnets.',criticality.warning))#Orphan
result.comments[121] = (comment('VLAN %s has no IPv6 Self-IP.',criticality.error)) #Orphan
result.comments[122] = (comment('VLAN %s has no Static IPv6 Self-IP.',criticality.error))
result.comments[123] = (comment('VLAN %s has no Floating IPv6 Self-IP.',criticality.error))
result.comments[124] = (comment('VLAN %s has multiple Static IPv6 Self-IP.',criticality.error))
result.comments[125] = (comment('VLAN %s has multiple Floating IPv6 Self-IP.',criticality.error))
result.comments[126] = (comment('VLAN %s is Client-Side only (Only VS) for IPv6.',criticality.info))
result.comments[127] = (comment('VLAN %s is Server-Side only (Only Nodes) for IPv6.',criticality.info))
result.comments[128] = (comment('VLAN %s is shared Client/Server-Side (VS and Nodes) for IPv6.',criticality.info))
result.comments[129] = (comment('VLAN %s has no LTM objects (No VS and no Nodes) for IPv6.',criticality.warning))#Orphan
result.comments[130] = (comment('VLAN %s has IPv6 self-IP addresses on multiple subnets.',criticality.warning))#Orphan
result.comments[201] = (comment('Static IPv4 Self-IP %s belongs to a VLAN that has no Floating IPv4 Self-IP.',criticality.error))
result.comments[202] = (comment('Static IPv4 Self-IP %s belongs to a VLAN that has too many Floating IPv4 Self-IP.',criticality.error))
result.comments[203] = (comment('Static IPv4 Self-IP %s belongs to a VLAN that has too many Static IPv4 Self-IP.',criticality.error))
result.comments[204] = (comment('Floating IPv4 Self-IP %s belongs to a VLAN that has no Static IPv4 Self-IP.',criticality.error))
result.comments[205] = (comment('Floating IPv4 Self-IP %s belongs to a VLAN that has too many Static IPv4 Self-IP.',criticality.error))
result.comments[206] = (comment('Floating IPv4 Self-IP %s belongs to a VLAN that has too many Floating IPv4 Self-IP.',criticality.error))
result.comments[207] = (comment('IPv4 Self-IP %s belongs to a VLAN that has Self-IP addresses on other IPv4 subnets.',criticality.error))
result.comments[221] = (comment('Static IPv6 Self-IP %s belongs to a VLAN that has no Floating IPv6 Self-IP.',criticality.error))
result.comments[222] = (comment('Static IPv6 Self-IP %s belongs to a VLAN that has too many Floating IPv6 Self-IP.',criticality.error))
result.comments[223] = (comment('Static IPv6 Self-IP %s belongs to a VLAN that has too many Static IPv6 Self-IP.',criticality.error))
result.comments[224] = (comment('Floating IPv6 Self-IP %s belongs to a VLAN that has no Static IPv6 Self-IP.',criticality.error))
result.comments[225] = (comment('Floating IPv6 Self-IP %s belongs to a VLAN that has too many Static IPv6 Self-IP.',criticality.error))
result.comments[226] = (comment('Floating IPv6 Self-IP %s belongs to a VLAN that has too many Floating IPv6 Self-IP.',criticality.error))
result.comments[227] = (comment('IPv6 Self-IP %s belongs to a VLAN that has Self-IP addresses on other IPv6 subnets.',criticality.error))
result.comments[301] = (comment('Route %s is unnecessary, network is already directly connected via VLAN.',criticality.warning))
result.comments[302] = (comment('Route %s is unnecessary, no objects are accessible via this route.',criticality.warning)) #Not Done
result.comments[303] = (comment('There are no default IPv4 route configured.',criticality.warning))
result.comments[304] = (comment('There are no default IPv6 route configured.',criticality.warning))
result.comments[401] = (comment('LTM Node %s is not used in any of the LTM pool.',criticality.warning))
result.comments[402] = (comment('LTM Node %s is not reachable via any of the configured routes or vlans.',criticality.error))
result.comments[403] = (comment('LTM Node %s is only reachable via the default route.',criticality.info))
result.comments[501] = (comment('LTM Pool %s is empty.',criticality.warning))
result.comments[502] = (comment('LTM Pool %s has LTM nodes on different vlan.',criticality.warning))
result.comments[503] = (comment('LTM Pool %s has a mix of directly connected and routed LTM nodes.',criticality.warning))
result.comments[504] = (comment('LTM Pool %s is not attached to any LTM virtual server (could be used within irules, use 5bulator.py to find out which pools are used from which irules).',criticality.warning))
result.comments[601] = (comment('LTM Virtual server %s is on the same vlan as all of its LTM pool members (one-arm).',criticality.info))
result.comments[602] = (comment('LTM Virtual server %s is on the same vlan as some of its LTM pool members.',criticality.warning))
result.comments[603] = (comment('LTM Virtual server %s is on a different vlan than all its LTM pool members (inline).',criticality.info))
result.comments[604] = (comment('LTM Virtual server %s has no LTM pool attached.',criticality.warning))
print(len(result.comments))
print("\r")
######################
# Data Structures NW
######################
class ltmObject:
def __init__(self, name='none', comments=None, orphan=False ):
self.name: str = name
self.comments = []
self.orphan = False
class globalSettings(ltmObject):
def __init__(self, name='none'):
ltmObject.__init__(self, name, None)
self.hostname = None
def process(config):
global bigipconfiguration
gs= globalSettings()
#What does this do again ?
settings =isolate(re.search('(^sys global-settings(.*))',config).group(0).replace('sys global-settings', ''))
if settings:
gs.name=settings.strip()
hostname = re.search('([\s]+hostname .*)',config)
if hostname:
hostname =isolate(hostname.group(0).replace('hostname', ''))
gs.hostname=hostname.strip()
bigipconfiguration.hostname=gs.hostname
class deviceGroup(ltmObject):
def __init__(self, name='none'):
ltmObject.__init__(self, name, None)
self.devices = []
self.type = None
self.clusterSize=0
def process(config):
global bigipconfiguration
dg= deviceGroup()
name =isolate(re.search('(^cm device-group (.*))',config).group(0).replace('cm device-group', ''))
if name:
dg.name=name.strip()
type = re.search('([\s]+type .*)',config)
if type:
type =isolate(type.group(0).replace('type', ''))
dg.type=type.strip()
if dg.type=='sync-failover':
devicesList = extractConfigSegment(config,'([\s]*devices\s{)')
devicesList = re.sub('[\s]*devices\s{','',devicesList, flags=re.M)
devicesList = re.sub('(^[\s]*})','',devicesList, flags=re.M)
devicesList = re.sub('(^[\s]*)','',devicesList, flags=re.M)
devicesList = devicesList.strip()
nlines = len(devicesList.splitlines())
if nlines>=2:
bigipconfiguration.isHA=True
class rd(ltmObject):
def __init__(self, name='none', id=0):
ltmObject.__init__(self, name, None)
self.vlanConfList = []
self.id: str = id
self.vlans = []
def process(config):
global bigipconfiguration
r = rd()
if not config==None:
name =isolate(re.search('(^net route-domain (.*))',config).group(0).replace('net route-domain', ''))
if name:
r.name=name.strip()
id =isolate(re.search('([\s]+id .*)',config).group(0).replace('id', ''))
if id:
r.id=id.strip()
vlans =extractConfigSegment(config,'([\s]*vlans\s{)')
vlans = re.sub('(^[\s]*vlans([\n]|[\s].*))','',vlans, flags=re.M)
vlans = re.sub('(^[\s]*})','',vlans, flags=re.M)
vlans = re.sub('(^[\s]+)','',vlans, flags=re.M)
if vlans:
vlans=vlans.strip().split()
r.vlanConfList=vlans
for v1 in vlans:
for v2 in bigipconfiguration.vlans:
if v1 == v2.name:
r.vlans.append(v2)
v2.rd=r
bigipconfiguration.routeDomains.append(r)
def isVlanInRDVlanList(self,name):
for vname in self.vlanConfList:
if str(vname)==str(name):
return True
return False
def audit():
nRD=len(bigipconfiguration.routeDomains)
if nRD>1:
bigipconfiguration.hasRouteDomains=True
elif nRD==1:
if bigipconfiguration.routeDomains[0].id=='0':
bigipconfiguration.hasRouteDomains=False
else:
bigipconfiguration.hasRouteDomains=True
def getVlanByName(self, name):
for v in self.vlans:
if v.name==name:
return v
return None
def getVlanByAddress(self, address):
t=determineIpType(address)
if t==4:
for v in self.vlans:
for n in v.network4:
if ipaddress.IPv4Network(address).subnet_of(n.prefix):
return v
return None
if t==6:
for v in self.vlans:
for n in v.network6:
if ipaddress.IPv6Network(address).subnet_of(n.prefix):
return v
return None
class network(ltmObject):
def __init__(self, name='none', counterNull = 0, comments=None):
ltmObject.__init__(self, name, comments)
self.prefix = None
self.version = 4
self.selfips = []
self.virtuals = []
self.nodes = []
self.vlan = None
self.counterSelfStatic = counterNull
self.counterSelfFloating = counterNull
self.counterVirtuals = counterNull
self.counterNodes = counterNull
def audit4(self):
self.counterVirtuals=len(self.virtuals)
self.counterNodes=len(self.nodes)
self.counterSelfStatic=0
self.counterSelfFloating=0
############################################
# Conflict with audit at the self level
############################################
for s in self.selfips:
if s.kind=="static":
self.counterSelfStatic+=1
if s.kind=="floating":
self.counterSelfFloating+=1
#No Self-IP
if self.counterSelfFloating+self.counterSelfStatic==0:
if len( getattr(self.vlan,'network4'))==1:
bigipconfiguration.attachObjectToComment(self.vlan, 101, True)
else:
bigipconfiguration.attachObjectToComment(self, 101, True)
else:
#No Static
if self.counterSelfStatic==0:
if len( getattr(self.vlan,'network4'))==1:
bigipconfiguration.attachObjectToComment(self.vlan, 102)
else:
bigipconfiguration.attachObjectToComment(self, 102)
#No Floating and no float detected at all (i.e standalone unit)
elif (bigipconfiguration.isHA and self.counterSelfFloating==0):
if len( getattr(self.vlan,'network4'))==1:
bigipconfiguration.attachObjectToComment(self.vlan, 103)
else:
bigipconfiguration.attachObjectToComment(self, 103)
#Too many static:
if self.counterSelfStatic>1:
if len( getattr(self.vlan,'network4'))==1:
bigipconfiguration.attachObjectToComment(self.vlan, 104)
else:
bigipconfiguration.attachObjectToComment(self, 104)
#Too many float:
if self.counterSelfFloating>1:
if len( getattr(self.vlan,'network4'))==1:
bigipconfiguration.attachObjectToComment(self.vlan, 105)
else:
bigipconfiguration.attachObjectToComment(self, 105)
#No LTM Objects on the VLAN:
if self.counterNodes==0 and self.counterVirtuals==0:
if len( getattr(self.vlan,'network4'))==1:
bigipconfiguration.attachObjectToComment(self.vlan, 109)
else:
bigipconfiguration.attachObjectToComment(self, 109)
else:
#No LTM nodes on the VLAN:
if self.counterNodes==0:
if len( getattr(self.vlan,'network4'))==1:
bigipconfiguration.attachObjectToComment(self.vlan, 106)
else:
bigipconfiguration.attachObjectToComment(self, 106)
#No LTM nodes on the VLAN:
if self.counterVirtuals==0:
if len( getattr(self.vlan,'network4'))==1:
bigipconfiguration.attachObjectToComment(self.vlan, 107)
else:
bigipconfiguration.attachObjectToComment(self, 107)
#Both Virtual and nodes on the VLAN:
if not self.counterNodes==0 and not self.counterVirtuals==0:
if len( getattr(self.vlan,'network4'))==1:
bigipconfiguration.attachObjectToComment(self.vlan, 108)
else:
bigipconfiguration.attachObjectToComment(self, 108)
def audit6(self):
self.counterVirtuals=len(self.virtuals)
self.counterNodes=len(self.nodes)
self.counterSelfStatic=0
self.counterSelfFloating=0
############################################
# Conflict with audit at the self level
############################################
for s in self.selfips:
if s.kind=="static":
self.counterSelfStatic+=1
if s.kind=="floating":
self.counterSelfFloating+=1
#No Self-IP
if self.counterSelfFloating+self.counterSelfStatic==0:
if len( getattr(self.vlan,'network6'))==1:
bigipconfiguration.attachObjectToComment(self.vlan, 121, True)
else:
bigipconfiguration.attachObjectToComment(self, 121, True)
else:
#No Static
if self.counterSelfStatic==0:
if len( getattr(self.vlan,'network6'))==1:
bigipconfiguration.attachObjectToComment(self.vlan, 122)
else:
bigipconfiguration.attachObjectToComment(self, 122)
#No Floating and no float detected at all (i.e standalone unit)
elif (bigipconfiguration.isHA and self.counterSelfFloating==0):
if len( getattr(self.vlan,'network6'))==1:
bigipconfiguration.attachObjectToComment(self.vlan, 123)
else:
bigipconfiguration.attachObjectToComment(self, 123)
#Too many static:
if self.counterSelfStatic>1:
if len( getattr(self.vlan,'network6'))==1:
bigipconfiguration.attachObjectToComment(self.vlan, 124)
else:
bigipconfiguration.attachObjectToComment(self, 124)
#Too many float:
if self.counterSelfFloating>1:
if len( getattr(self.vlan,'network6'))==1:
bigipconfiguration.attachObjectToComment(self.vlan, 125)
else:
bigipconfiguration.attachObjectToComment(self, 125)
#No LTM Objects on the VLAN:
if self.counterNodes==0 and self.counterVirtuals==0:
if len( getattr(self.vlan,'network6'))==1:
bigipconfiguration.attachObjectToComment(self.vlan, 129)
else:
bigipconfiguration.attachObjectToComment(self, 129)
else:
#No LTM nodes on the VLAN:
if self.counterNodes==0:
if len( getattr(self.vlan,'network6'))==1:
bigipconfiguration.attachObjectToComment(self.vlan, 126)
else:
bigipconfiguration.attachObjectToComment(self, 126)
#No LTM nodes on the VLAN:
if self.counterVirtuals==0:
if len( getattr(self.vlan,'network6'))==1:
bigipconfiguration.attachObjectToComment(self.vlan, 127)
else:
bigipconfiguration.attachObjectToComment(self, 127)
#Both Virtual and nodes on the VLAN:
if not self.counterNodes==0 and not self.counterVirtuals==0:
if len( getattr(self.vlan,'network6'))==1:
bigipconfiguration.attachObjectToComment(self.vlan, 128)
else:
bigipconfiguration.attachObjectToComment(self, 128)
def getNumberOfSelf(self, kind):
counter=0
for s in self.selfips:
if s.kind==kind:
counter+=1
return int(counter)
class vlan(ltmObject):
def __init__(self, name='none', tag='none', counterNull = 0, comments=None ):
ltmObject.__init__(self, name, comments)
self.tag: str = tag
self.network4 = []
self.network6 = []
self.rd = None
self.routes = []
self.counterVirtuals4=0
self.counterNodes4=0
self.counterSelfStatic4=0
self.counterSelfFloating4=0
self.counterVirtuals6=0
self.counterNodes6=0
self.counterSelfStatic6=0
self.counterSelfFloating6=0
self.counterVirtualsTotal=0
self.counterNodesTotal=0
self.counterSelfStaticTotal=0
self.counterSelfFloatingTotal=0
def process(config):
global bigipconfiguration
v = vlan()
name =isolate(re.search('(^net vlan (.*))',config).group(0).replace('net vlan', ''))
if name:
v.name=name.strip()
#else return
tag =isolate(re.search('([\s]+tag .*)',config).group(0).replace('\n', '').replace('tag', ''))
if tag==None:
tag = 0
v.tag=tag
#####################################
# Attach VLAN to their Configuration
#####################################
if bigipconfiguration.getVlanByName(v.name)==None:
bigipconfiguration.vlans.append(v)
def audit():
print('[*] Auditing configuration - Vlans.')
if bigipconfiguration.isIPv4:
for v in bigipconfiguration.vlans:
if len(v.network4)==0:
bigipconfiguration.attachObjectToComment(v, 10)
elif len(v.network4)>1:
bigipconfiguration.attachObjectToComment(v, 11)
for n4 in v.network4:
n4.audit4()
pass
if bigipconfiguration.isIPv6:
for v in bigipconfiguration.vlans:
if len(v.network6)==0:
bigipconfiguration.attachObjectToComment(v, 12)
elif len(v.network6)>1:
bigipconfiguration.attachObjectToComment(v, 13)
for n6 in v.network6:
n6.audit6()
pass
def getVlanNetworkFromAddress(self, address, version):
if version ==4:
for w in self.network4:
if ipaddress.IPv4Network(address,False).subnet_of(w.prefix):
return w
return None
elif version ==6:
for w in self.network6:
if ipaddress.IPv6Network(address,False).subnet_of(w.prefix):
return w
return None
else:
return None
def postProcess(self):
for n in self.network4:
self.counterVirtuals4+=n.counterVirtuals
self.counterNodes4+=n.counterNodes
self.counterSelfStatic4=n.counterSelfStatic
self.counterSelfFloating4=n.counterSelfFloating
for n in self.network6:
self.counterVirtuals6+=n.counterVirtuals
self.counterNodes6+=n.counterNodes
self.counterSelfStatic6=n.counterSelfStatic
self.counterSelfFloating6=n.counterSelfFloating
self.counterVirtualsTotal = self.counterVirtuals4+self.counterVirtuals6
self.counterNodesTotal = self.counterNodes4+self.counterNodes6
self.counterSelfStaticTotal = self.counterSelfStatic4+self.counterSelfStatic6
self.counterSelfFloatingTotal = self.counterSelfFloating4+self.counterSelfFloating6
# if self.counterSelfStatic4+self.counterSelfFloating4+self.counterSelfStatic6+self.counterSelfFloating6==0:
# bigipconfiguration.attachOrphanObjectToConfiguration(self)
# pass#orphan
# elif self.counterVirtuals4+self.counterNodes4+self.counterVirtuals6+self.counterNodes6==0:
# bigipconfiguration.attachOrphanObjectToConfiguration(self)
# pass#orphan
if len(self.routes)==0:
if self.counterSelfStatic4+self.counterSelfFloating4+self.counterSelfStatic6+self.counterSelfFloating6==0:
bigipconfiguration.attachOrphanObjectToConfiguration(self)
pass#orphan
elif self.counterVirtuals4+self.counterNodes4+self.counterVirtuals6+self.counterNodes6==0:
bigipconfiguration.attachOrphanObjectToConfiguration(self)
pass#orphan
else:
bigipconfiguration.attachObjectToComment(self, 14)
class selfip(ltmObject):
def __init__(self, name='none', kind='none', address='none', comments=None):
ltmObject.__init__(self, name, comments)
self.kind: str = kind
self.address: str = address
self.vlan = None
def process(config):
global bigipconfiguration
s = selfip()
v = None
r=''
#####################################
# Extract info from self from config
#####################################
config = re.sub('(^[\s]*inherited-traffic-group([\n]|[\s].*))','',config, flags=re.M)
name =re.search('(^net self (.*))',config)
if name:
s.name=isolate(name.group(0).replace('net self', ''))
address =re.search('([\s]*address (.*))',config)
if address:
r=extractRD(address.group(0))
s.address=isolate(address.group(0).replace('address', ''))
kind =re.search('([\s]*traffic-group (.*))',config)
if kind:
kind=isolate(kind.group(0).replace('traffic-group ', '')).split('/')[2]
if kind=='traffic-group-local-only': #ambiguity on possible other traffic groups, to be reviewed
s.kind='static'
else:
s.kind='floating'
vl =re.search('([\s]+vlan (.*))',config) # dont seem to be able to use ^ in the regex.
if vl:
vl=isolate(vl.group(0).replace('vlan ', ''))
##########################################
# Attach Self to config and to their VLANs
##########################################
rd1=bigipconfiguration.getRdByID(r)
if not rd1==None:
v=rd1.getVlanByName(vl)
if not v == None:
if determineIpType(s.address)==4:
if (len(v.network4)==0): #No network stored yet
w=network()
w.prefix=ipaddress.ip_network(s.address, strict=False)
w.version=4
#w.selfips.append(s)
w.vlan=v
w.name=str(v.name)+'('+str(w.prefix)+')'
v.network4.append(w)
bigipconfiguration.isIPv4=True
else: #Network list is not empty
n = v.getVlanNetworkFromAddress( ipaddress.ip_network(s.address, strict=False), 4)
#network exists already:
if not n==None:
#n.selfips.append(s)
pass
#network doesnt exist yet:
else:
w=network()
w.prefix=ipaddress.ip_network(s.address, strict=False)
w.version=4
#w.selfips.append(s)
w.vlan=v
w.name=str(v.name)+'('+str(w.prefix)+')'
v.network4.append(w)
s.vlan=v #remove?
bigipconfiguration.attachObjectToConfiguration(s,(v,))
return
elif determineIpType(s.address)==6:
if (len(v.network6)==0): #No network stored yet
w=network()
w.prefix=ipaddress.ip_network(s.address, strict=False)
w.version=6
w.selfips.append(s)
w.vlan=v
v.network6.append(w)
bigipconfiguration.isIPv6=True
else: #Network list is not empty
n = v.getVlanNetworkFromAddress( ipaddress.ip_network(s.address, strict=False),6)
#network exist already:
if not n==None:
n.selfips.append(s)
#network doesnt exist yet:
else:
w=network()
w.prefix=ipaddress.ip_network(s.address, strict=False)
w.version=6
w.selfips.append(s)
w.vlan=v
v.network6.append(w)
s.vlan=v
bigipconfiguration.attachObjectToConfiguration(s,(v,))
return
else:
print('Error - invalid ip address type')
s.vlan=v
bigipconfiguration.attachObjectToConfiguration(s)
return
else:
#add case where there could be a tunnel instead of a vlan
print('Error - no vlan found on this route domain for this selfip : ', s.name)
bigipconfiguration.attachObjectToConfiguration(s)
return
def audit():
print('[*] Auditing configuration - Selfips.')
#check if there are too many self on the VLAN where this self belongs
for s in bigipconfiguration.selfips:
if (s.vlan!=None):
n = s.vlan.getVlanNetworkFromAddress( ipaddress.ip_network(s.address, strict=False), determineIpType(ipaddress.ip_network(s.address, strict=False)))
staticCounter = n.getNumberOfSelf('static')
floatCounter = n.getNumberOfSelf('floating')
if (s.kind=='static'):
if ( int(floatCounter) == 0 ) :
if determineIpType(s.address)==4:
bigipconfiguration.attachObjectToComment(s, 201)
elif determineIpType(s.address)==6:
bigipconfiguration.attachObjectToComment(s, 221)
elif ( int(floatCounter) > 1 ) :