forked from GlynisBarberBrandon/F5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigipParser
1376 lines (1020 loc) · 45.8 KB
/
bigipParser
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
#!/bin/bash
#
# bigipParser
# Author: Fatih Çelik (9.2020)
#
# This script provides Virtual Servers and it's dependent objects as divided by files with objects as much as possible
# It takes single argument which points of bigip.conf file.
# Usage:
# bigipParser path/to/bigip.conf
#
#
# Copyright (C) Fatih Çelik, hereby disclaims all copyright
# interest in the program written by Fatih CELIK
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
#########################################################################
# #
# ver. 0.10 #
# #
#########################################################################
if [ $# -ne 1 ]; then
echo -e "\n\tUsage: bigipParser /path/to/bigip.conf\n"
exit 9
fi
er=0
for command in awk grep cat tac tail head sed base64
do
if [ -x "/bin/${command}" ] || [ -x "/usr/bin/${command}" ]; then
printf "%-24s [ OK ]\n" "${command}"
else
printf "%-24s [ MISSING ]\n" "${command}"
(( er += 1 ))
fi
done
if [ $er -ne 0 ]; then
echo -e "\nSorry, Some of the tools ( ${er} pieces ) listed above are missing.\nPlease fullfill the requirements before using this script\n"
exit 40
fi
confFile="$1"
DEBUG=1 # Need more details ? Set any thing !
# Prepare bigip.conf file
# Since iApp created Virtual Servers not important for us, erase those lines 'app.service' and '*.app' in where they stands
iAppFreeConfFile=/tmp/bigip.conf_$(cat /dev/urandom | tr -cd 'a-zA-Z0-9' | head -c 10)
#~ sed -e 's/[a-zA-Z0-9\_\-\.]\+app\///g' -e '/app-service/d' "${confFile}" > ${iAppFreeConfFile}
sed -e '/app-service/d' "${confFile}" > ${iAppFreeConfFile}
# lets find out how many conf object in bigip.conf file
declare -A virtServerArray #OK
declare -A poolArray #OK
declare -A monitorArray #OK
declare -A policyArray #OK
declare -A iruleArray #OK
declare -A snatTranslationArray #OK
declare -A snatPoolArray #OK
declare -A dataGroupArray #OK
declare -A persistArray #OK
declare -A profileArray #OK
declare -A virtAddrArray #OK
declare -A sysiFileArray #OK
declare -A ltmiFileArray #OK
declare -A sysSslCertArray #OK
declare -A sysSslKeyArray #OK
# declare -a netRoureArray # Should I worry about route definitions and some other objects like ssl certs & keys ?
# Yes, we support iFiles and we are going to implement a support for SSL certs & Keys soon
vsRange=0
policyRange=0
poolRange=0
ruleRange=0
virtAddrRange=0
sysiFileRange=0
ltmiFileRange=0
[ $DEBUG ] && echo -e "#################################################\n## $(date)\n#################################################\n"
findFinish(){
#!/bin/bash
# count every opening and closing curly brace and find out where it ends...
if [ $# -ne 2 ]; then
i=1
openCurly=0
while read -r line
do
isCurly=$(echo $line | grep -E "{|}" )
if [ "$isCurly" ]; then
openCurly=$(( openCurly + $(echo $line | sed 's/[^{]//g' | tr -d '\n' | wc -m) ))
openCurly=$(( openCurly - $(echo $line | sed 's/[^}]//g' | tr -d '\n' | wc -m) ))
if [ $openCurly -eq 0 ]; then
endingLine=$i
echo $endingLine
exit 0
fi
fi
(( i+=1 ))
done < <( cat - )
else
i=1
openCurly=0
file=$1
start=$2
while read -r line
do
isCurly=$(echo $line | grep -E "{|}" )
if [ "$isCurly" ]; then
openCurly=$(( openCurly + $(echo $line | sed 's/[^{]//g' | tr -d '\n' | wc -m) ))
openCurly=$(( openCurly - $(echo $line | sed 's/[^}]//g' | tr -d '\n' | wc -m) ))
if [ $openCurly -eq 0 ]; then
endingLine=$i
echo $endingLine
exit 0
fi
fi
(( i+=1 ))
done < <( /usr/bin/tail -n +${start} $file )
fi
}
get_virtualServer(){
# lets find how many Virtual Servers in that config file and then populate our Array (virtServerArray) with them.
# Array Conttains Virtual Server names, starting and ending points
# Before; tempArray[index]=StartLine:EndingLine:Name
# After; Array[Name]=StartLine,EndingLine
declare -a tempArray
local name start end res
read -a tempArray <<< $( /bin/grep -nE "ltm virtual " ${iAppFreeConfFile} | sed -e 's/ltm virtual \/Common\//\:/g' -e 's/{ \|{//g' | tr '\n' ' ' )
[ $DEBUG ] && declare -p tempArray | tr " " "\n" # debug
if [ ${#tempArray[@]} -ne 0 ]; then
for (( i = 0; i < ${#tempArray[@]}; i++)){
start=$( echo ${tempArray[i]} | awk -F':' '{ print $1 }' )
end=$( echo ${tempArray[i+1]} | awk -F':' '{ print $1 -1 }' )
name=$( echo ${tempArray[i]} | awk -F':' '{ print $3 }' )
if [ $( echo "$name" | grep -E "\.app\/") ]; then
name=$( echo $name | awk -F "/" '{ print $2"__APP"}')
fi
if [ $(( ${#tempArray[@]} -1)) -eq ${#virtServerArray[@]} ]; then
res=$(findFinish ${iAppFreeConfFile} $start)
end=$(( $res + $start -1 ))
vsRange=$( echo "${vsRange},${end}" )
fi
if [ $i -eq 0 ]; then
vsRange=$start
fi
virtServerArray["${name}"]="${start},${end}"
}
[ $DEBUG ] && declare -p virtServerArray | tr ' ' '\n' # debug
else
echo -e "\tNo Virtual Server found\n...so we quit"
exit 11
fi
unset tempArray
}
get_virtAddr(){
# Populate Virtual IP Address list with below format
# virtAddrArray[virtIPAddr]="StartingLine,EndingLine
declare -a tempArray
local name start end res
read -a tempArray <<< $( grep -nE "^ltm virtual-address " ${iAppFreeConfFile} | awk -F '/' '{ print $1" "$3 }' | sed -e 's,:, ,g' -e 's, {,,g' -e s'/ ltm virtual-address //g' -e 's/ /::/g' | tr '\n' ' ' )
[ $DEBUG ] && declare -p tempArray | tr ' ' '\n' # debug
if [ ${#tempArray[@]} -ne 0 ]; then
for (( addr=0; addr < ${#tempArray[@]}; addr++ )){
name=$( echo ${tempArray[$addr]} | awk -F ':' '{ print $3 }' )
start=$( echo ${tempArray[$addr]} | awk -F ':' '{ print $1 }' )
end=$( echo ${tempArray[$addr + 1]} | awk -F ':' '{ print $1 -1 }' )
if [ $(( ${#tempArray[@]} -1 )) -eq ${#virtAddrArray[@]} ]; then
res=$( findFinish ${iAppFreeConfFile} $start )
end=$(( $start + $res -1 ))
virtAddrRange=$(echo "${virtAddrRange},${end}" )
fi
if [ $addr -eq 0 ]; then
virtAddrRange=$start
fi
virtAddrArray["${name}"]="${start},${end}"
}
[ $DEBUG ] && declare -p virtAddrArray | tr ' ' '\n' # debug
fi
unset tempArray
}
get_pools(){
# populate array with pool names and add the starting, ending points
# poolArray[poolName]=Number_of_StartingLine,Number_of_EndingLine
declare -a tempArray
declare -a poolUsedInRules
local name start end res
read -a tempArray <<< $( /bin/grep -nE "ltm pool " ${iAppFreeConfFile} | sed -e 's/ltm pool \/Common\//\:/g' -e 's/{ \|{//g' -e 's/{ \|{//g' -e 's,}$,,g' | tr '\n' ' ' )
[ $DEBUG ] && declare -p tempArray | tr ' ' '\n' # debug
if [ ${#tempArray[@]} -ne 0 ]; then
for (( i = 0; i < ${#tempArray[@]}; i++ )){
start=$( echo ${tempArray[i]} | awk -F: '{ print $1 }' )
end=$( echo ${tempArray[i+1]} | awk -F: '{ print $1 -1 }' )
name=$( echo ${tempArray[i]} | awk -F: '{ print $3 }' )
if [ $( echo "$name" | grep -E "\.app\/") ]; then
name=$( echo $name | awk -F "/" '{ print $2"__APP"}')
fi
if [ $(( ${#tempArray[@]} -1)) -eq ${#poolArray[@]} ]; then
res=$(findFinish ${iAppFreeConfFile} $start)
end=$(( $res + $start -1 ))
poolRange=$( echo "${poolRange},${end}" )
fi
if [ $i -eq 0 ]; then
poolRange=$start
fi
poolArray["${name}"]="${start},${end}"
if [ ! -z $ruleRange ]; then # if there is no iRule, no need to go further then...
read -a poolUsedInRules <<< $( sed -n ${ruleRange}p ${iAppFreeConfFile} | grep -nE "$name" | grep -v "#" | cut -d ":" -f 1 | tr '\n' ' ' )
[ $DEBUG ] && echo "Pool Used IN Rules first populate" # debug
[ $DEBUG ] && echo "DEBUG - IF the Pool ${name}, -> ${poolArray["${name}"]} Used IN anywhere of whole iRules list" && declare -p poolUsedInRules | tr " " "\n" # debug
if [ ${#poolUsedInRules[@]} -ne 0 ]; then
#~ extractPools "$name"
extract_poolsFromiRules "$name"
fi
fi
}
else
echo -e "\tNo Pool Found"
fi
[ $DEBUG ] && echo "Status of poolArray after Pool Array Populated:" # debug
[ $DEBUG ] && declare -p poolArray | tr " " "\n" # debug
[ $DEBUG ] && echo "Status of iruleArray after Pool Array Populated:" # debug
[ $DEBUG ] && declare -p iruleArray | tr " " "\n" # debug
unset tempArray poolUsedInRules
}
get_policy(){
# Populate Array (policyArray) below formats
# policyArray[PolicyName]=StartLine,EndLine
declare -a tempArray
local name start end res
read -a tempArray <<< $( /bin/grep -nE "ltm policy " ${iAppFreeConfFile} | sed -e 's/ltm policy \/Common\//\:/g' -e 's/{ \|{//g' | tr '\n' ' ' )
[ $DEBUG ] && echo -e "DEBUG - policies temp array;\n" && declare -p tempArray | tr ' ' '\n' # DEBUG
if [ ${#tempArray[@]} -ne 0 ]; then
for (( i = 0; i < ${#tempArray[@]}; i++ )){
start=$( echo ${tempArray[i]} | awk -F: '{ print $1 }' )
end=$( echo ${tempArray[i+1]} | awk -F: '{ print $1 -1 }' )
name=$( echo ${tempArray[i]} | awk -F: '{ print $3 }' )
if [ $( echo "$name" | grep -E "\.app\/") ]; then
name=$( echo $name | awk -F "/" '{ print $2"__APP"}')
fi
if [ $(( ${#tempArray[@]} -1)) -eq ${#policyArray[@]} ]; then
res=$(findFinish ${iAppFreeConfFile} $start)
end=$(( $res + $start -1 ))
policyRange=$( echo "${policyRange},${end}" )
fi
if [ $i -eq 0 ]; then
policyRange=$start
fi
policyArray["${name}"]="${start},${end}"
}
#~ debug
[ $DEBUG ] && declare -p policyArray | tr ' ' '\n'
else
echo -e "\tNo Policy found"
policyRange=""
fi
unset tempArray
}
get_iRules(){
declare -a tempArray
local start end name res
read -a tempArray <<< $( /bin/grep -nE "ltm rule " ${iAppFreeConfFile} | sed -e 's/ltm rule \/Common\//\:/g' -e 's/{ \|{//g' | tr '\n' ' ' )
[ $DEBUG ] && echo "IRULE first"
[ $DEBUG ] && declare -p tempArray | tr " " "\n" # debug
if [ ${#tempArray[@]} -ne 0 ]; then
for (( i = 0; i < ${#tempArray[@]}; i++ )){
start=$( echo ${tempArray[i]} | awk -F: '{ print $1 }' )
end=$( echo ${tempArray[i+1]} | awk -F: '{ print $1 -1 }' )
name=$( echo ${tempArray[i]} | awk -F: '{ print $3 }' )
if [ $( echo "$name" | grep -E "\.app\/") ]; then
name=$( echo $name | awk -F "/" '{ print $2"__APP"}')
fi
if [ $(( ${#tempArray[@]} -1)) -eq ${#iruleArray[@]} ]; then
res=$( findFinish ${iAppFreeConfFile} $start )
end=$(( $res + $start -1 ))
ruleRange="$( echo ${ruleRange},${end} )"
fi
if [ $i -eq 0 ]; then
ruleRange=$start
fi
iruleArray["${name}"]="${start},${end}"
}
[ $DEBUG ] && echo " iRule ARRAY " # debug
[ $DEBUG ] && echo " iRule Range ( ruleRange ) is $ruleRange" # debug
[ $DEBUG ] && declare -p iruleArray | tr " " "\n" # debug
else
echo -e "\tNo iRule found"
ruleRange=""
fi
unset tempArray
}
get_iFiles(){
# this function detect if there is any "ltm ifile" definitions exist
# then fullfill their start and end line numbers like others. But
# this time we 'll use this array store more than one tuples like "Start,End:Start,End:Start,End:(...),(...)"
# As you guess, the first tuple is point "ltm ifile" lines and second tuples points its corresponding sub "sys file ifile" line ranges.
declare -a tempArray # for "sys file ifile"
declare -a tmpArray # for "ltm ifile"
local start end name res
read -a tmpArray <<< $( grep -nE "^ltm ifile " ${iAppFreeConfFile} | sed -e 's,:[a-z]\+\ ifile\ /[a-zA-Z0-9\-\_]\+/,::,g' -e 's, {$,,g' | tr '\n' ' ' )
[ $DEBUG ] && echo "DEBUG - ltm ifile temp array " && declare -p tmpArray | tr ' ' '\n' #debug
if [ ${#tmpArray[@]} -ne 0 ]; then #ltm ifile lines exist so we can go further and find "sys file ifile" lines
for (( k = 0; k < ${#tmpArray[@]}; k++ )){
start=$( echo ${tmpArray[k]} | awk -F: '{ print $1 }' )
end=$( echo ${tmpArray[k+1]} | awk -F: '{ print $1 -1 }' )
name=$( echo ${tmpArray[k]} | awk -F: '{ print $3 }' )
if [ $( echo "$name" | grep -E "\.app\/") ]; then
name=$( echo $name | awk -F "/" '{ print $2"__APP"}' )
fi
if [ $(( ${#tmpArray[@]} -1 )) -eq ${#ltmiFileArray[@]} ]; then
res=$( findFinish ${iAppFreeConfFile} $start )
end=$(( $res + $start -1 ))
ltmiFileRange=$( echo "${ltmiFileRange},${end}" )
fi
if [ $k -eq 0 ]; then
ltmiFileRange=$start
fi
ltmiFileArray["${name}"]="${start},${end}"
}
[ $DEBUG ] && echo "DEBUG - ltmiFileArray " && declare -p ltmiFileArray | tr ' ' '\n' # debug
[ $DEBUG ] && echo -e "DEBUG - ltmiFileRange is ${ltmiFileRange}\n\n" # debug
read -a tempArray <<< $( grep -nE "^sys file ifile " ${iAppFreeConfFile} | sed -e 's,:, ,g' -e 's, {$,,g' | awk '{ print $1"::"$NF }' | sed -e 's,/[a-zA-Z0-9\-\_]\+/,,g' | tr '\n' ' ')
[ $DEBUG ] && echo "DEBUG - sys file ifile temp array " && declare -p tempArray | tr ' ' '\n' #debug
if [ ${#tempArray[@]} -ne 0 ]; then
for (( i = 0; i < ${#tempArray[@]}; i++ )){
start=$( echo ${tempArray[i]} | awk -F: '{ print $1 }' )
end=$( echo ${tempArray[i+1]} | awk -F: '{ print $1 -1 }' )
name=$( echo ${tempArray[i]} | awk -F: '{ print $3 }' )
if [ $( echo "$name" | grep -E "\.app\/") ]; then
name=$( echo $name | awk -F "/" '{ print $2"__APP"}' )
fi
if [ $(( ${#tempArray[@]} -1)) -eq ${#sysiFileArray[@]} ]; then
res=$( findFinish ${iAppFreeConfFile} $start )
end=$(( $res + $start -1 ))
sysiFileRange=$( echo "${sysiFileRange},${end}" )
fi
if [ $i -eq 0 ]; then
sysiFileRange=$start
fi
sysiFileArray["${name}"]="${start},${end}"
}
[ $DEBUG ] && echo "DEBUG - sysiFileArray " && declare -p sysiFileArray | tr ' ' '\n' # debug
[ $DEBUG ] && echo "DEBUG - sysiFileRange is ${sysiFileRange}" # debug
fi
for m in "${!ltmiFileArray[@]}"
do
if [ $( echo ${ltmiFileArray[$m]} | grep -E "[0-9]+,[0-9]+" ) ]; then
ifileName=$( sed -n "${ltmiFileArray[$m]}p" ${iAppFreeConfFile} | grep "file-name " | awk -F '/' '{ print $3 }' )
if [ $( echo "${sysiFileArray[${ifileName}]}" | grep -E "[0-9]+,[0-9]+" ) ]; then
ltmiFileArray["${m}"]="${ltmiFileArray[${m}]};${sysiFileArray[${ifileName}]}"
[ $DEBUG ] && echo "ltm iFile - ${ltmiFileArray[${m}]};${sysiFileArray[${ifileName}]}"
fileName=$( sed -n "${sysiFileArray[${ifileName}]}p" ${iAppFreeConfFile} | grep -E "^\s{4}cache-path " | awk -F '/' '{ print $NF }' )
res=$( find . -type f -name "${fileName}" )
if [ -a "${res}" ]; then
ltmiFileArray["${m}"]="${ltmiFileArray[${m}]};${res}"
else
echo "Sorry Can not find the file - ${res}"
ltmiFileArray["${m}"]="${ltmiFileArray[${m}]};NOFILE"
fi
else
echo "These ifile and sys ifile tuples are missing --> ${ltmiFileArray[${m}]};${sysiFileArray[${ifileName}]} <-- "
ltmiFileArray["${m}"]="${ltmiFileArray[${m}]};N"
fi
else
echo "Sorry, There is no iFile - ${ltmiFileArray[$m]}"
fi
done
[ $DEBUG ] && echo -e "DEBUG - After the sys file ifile \ninformation has been \nadded ltmiFileArray " && declare -p ltmiFileArray | tr ' ' '\n' # debug
fi
unset tempArray
unset tmpArray
}
get_snatTranslation(){
declare -a tempArray
local start end name res
read -a tempArray <<< $( /bin/grep -nE "ltm snat-translation " ${iAppFreeConfFile} | sed -e 's/ltm snat-translation \/Common\//\:/g' -e 's/{ \|{//g' | tr '\n' ' ' )
if [ ${#tempArray[@]} -ne 0 ]; then
for (( i = 0; i < ${#tempArray[@]}; i++ )){
start=$( echo ${tempArray[i]} | awk -F: '{ print $1 }' )
end=$( echo ${tempArray[i+1]} | awk -F: '{ print $1 -1 }' )
name=$( echo ${tempArray[i]} | awk -F: '{ print $3 }' )
if [ $( echo "$name" | grep -E "\.app\/") ]; then
name=$( echo $name | awk -F "/" '{ print $2"__APP"}')
fi
if [ $(( ${#tempArray[@]} -1)) -eq $i ]; then
res=$( findFinish ${iAppFreeConfFile} $start )
end=$(( $res + $start -1 ))
fi
snatTranslationArray["${name}"]="${start},${end}"
}
[ $DEBUG ] && declare -p snatTranslationArray | tr ' ' '\n' # debug
else
echo -e "\tNo Snat Translation found"
fi
unset tempArray
}
get_snatPool(){
declare -a tempArray
local start end name res
read -a tempArray <<< $( /bin/grep -nE "ltm snatpool " ${iAppFreeConfFile} | sed -e 's/ltm snatpool \/Common\//\:/g' -e 's/{ \|{//g' | tr '\n' ' ' )
if [ ${#tempArray[@]} -ne 0 ]; then
for (( i = 0; i < ${#tempArray[@]}; i++ )){
start=$( echo ${tempArray[i]} | awk -F: '{ print $1 }' )
end=$( echo ${tempArray[i+1]} | awk -F: '{ print $1 -1 }' )
name=$( echo ${tempArray[i]} | awk -F: '{ print $3 }' )
if [ $( echo "$name" | grep -E "\.app\/") ]; then
name=$( echo $name | awk -F "/" '{ print $2"__APP"}')
fi
if [ $(( ${#tempArray[@]} -1)) -eq ${#snatPoolArray[@]} ]; then
res=$( findFinish ${iAppFreeConfFile} $start )
end=$(( $res + $start -1 ))
fi
snatPoolArray["${name}"]="${start},${end}"
}
[ $DEBUG ] && declare -p snatPoolArray | tr ' ' '\n' # debug
else
echo -e "\tNo Snat Pool found"
fi
unset tempArray
}
get_monitors(){
declare -a tempArray
local start end name res
read -a tempArray <<< $( /bin/grep -nE "ltm monitor " ${iAppFreeConfFile} | sed -e 's/ltm monitor [a-zA-Z0-9\_\-]\+\ \/Common\//:/g' -e 's/ {\|{//g' | tr '\n' ' ' )
if [ ${#tempArray[@]} -ne 0 ]; then
for (( i = 0; i < ${#tempArray[@]}; i++ )){
start=$( echo ${tempArray[i]} | awk -F: '{ print $1 }' )
end=$( echo ${tempArray[i+1]} | awk -F: '{ print $1 -1 }' )
name=$( echo ${tempArray[i]} | awk -F: '{ print $3 }' )
if [ $( echo "$name" | grep -E "\.app\/") ]; then
name=$( echo $name | awk -F "/" '{ print $2"__APP"}')
fi
if [ $(( ${#tempArray[@]} -1)) -eq ${#monitorArray[@]} ]; then
res=$( findFinish ${iAppFreeConfFile} $start )
end=$(( $res + $start -1 ))
fi
monitorArray["${name}"]="${start},${end}"
}
[ $DEBUG ] && declare -p monitorArray | tr ' ' '\n' # debug
else
echo -e "\tNo Monitor found"
fi
unset tempArray
}
get_persistence(){
declare -a tempArray
local start end name res
read -a tempArray <<< $( /bin/grep -nE "ltm persistence " ${iAppFreeConfFile} | sed -e 's/ltm persistence [a-zA-Z0-9\_\-]\+//g' -e 's/ \/Common\//\:/g' -e 's/{ \|{//g' | tr '\n' ' ' )
if [ ${#tempArray[@]} -ne 0 ]; then
for (( i = 0; i < ${#tempArray[@]}; i++ )){
start=$( echo ${tempArray[i]} | awk -F: '{ print $1 }' )
end=$( echo ${tempArray[i+1]} | awk -F: '{ print $1 -1 }' )
name=$( echo ${tempArray[i]} | awk -F: '{ print $3 }' )
if [ $( echo "$name" | grep -E "\.app\/") ]; then
name=$( echo $name | awk -F "/" '{ print $2"__APP"}')
fi
if [ $(( ${#tempArray[@]} -1)) -eq ${#persistArray[@]} ]; then
res=$( findFinish ${iAppFreeConfFile} $start )
end=$(( $res + $start -1 ))
fi
persistArray["${name}"]="${start},${end}"
}
[ $DEBUG ] && declare -p persistArray | tr ' ' '\n' # debug
else
echo -e "\tNo Persistence found"
fi
unset tempArray
}
get_dataGroup(){
declare -a tempArray
local start end name res
read -a tempArray <<< $( /bin/grep -nE "ltm data-group " ${iAppFreeConfFile} | sed -e 's/ltm data-group [a-zA-Z0-9\_\-]\+//g' -e 's/ \/Common\//\:/g' -e 's/{ \|{//g' | tr '\n' ' ' )
if [ ${#tempArray[@]} -ne 0 ]; then
for (( i = 0; i < ${#tempArray[@]}; i++ )){
start=$( echo ${tempArray[i]} | awk -F: '{ print $1 }' )
end=$( echo ${tempArray[i+1]} | awk -F: '{ print $1 -1 }' )
name=$( echo ${tempArray[i]} | awk -F: '{ print $3 }' )
if [ $( echo "$name" | grep -E "\.app\/") ]; then
name=$( echo $name | awk -F "/" '{ print $2"__APP"}')
fi
if [ $(( ${#tempArray[@]} -1)) -eq ${#dataGroupArray[@]} ]; then
res=$( findFinish ${iAppFreeConfFile} $start )
end=$(( $res + $start -1 ))
fi
dataGroupArray["${name}"]="${start},${end}"
}
[ $DEBUG ] && declare -p dataGroupArray | tr ' ' '\n' # debug
else
echo -e "\tNo Data Group found"
fi
unset tempArray
}
get_profile(){
declare -a tempArray
local start end name res
read -a tempArray <<< $( /bin/grep -nE "ltm profile " ${iAppFreeConfFile} | sed -e 's/ltm profile [a-zA-Z0-9\_\-]\+//g' -e 's/ \/Common\//\:/g' -e 's/{ \|{//g' | tr '\n' ' ' )
if [ ${#tempArray[@]} -ne 0 ]; then
for (( i = 0; i < ${#tempArray[@]}; i++ )){
start=$( echo ${tempArray[i]} | awk -F: '{ print $1 }' )
end=$( echo ${tempArray[i+1]} | awk -F: '{ print $1 -1 }' )
name=$( echo ${tempArray[i]} | awk -F: '{ print $3 }' )
if [ $( echo "$name" | grep -E "\.app\/") ]; then
name=$( echo $name | awk -F "/" '{ print $2"__APP"}')
fi
if [ $(( ${#tempArray[@]} -1)) -eq ${#profileArray[@]} ]; then
res=$( findFinish ${iAppFreeConfFile} $start )
end=$(( $res + $start -1 ))
fi
profileArray["${name}"]="${start},${end}"
}
[ $DEBUG ] && declare -p profileArray | tr ' ' '\n' # debug
else
echo -e "\t\nNo Profile found"
fi
unset tempArray
}
get_extraPools(){
# find dataGroups used in iRules
local lines
declare -a tempArray
lines="$1"
read -a tempArray <<< $( sed -n ${lines}p ${iAppFreeConfFile} | grep -E "matchclass|class match" \
| sed -e 's/if\|elseif\| else //g' -e 's/{\|}\|\[\|\]\|\!\|(\|)//g' -e 's/&&\|||/\n/g' | awk 'NF>1 { print $NF }' | tr '\n' ' ' )
for _e in ${!tempArray[@]}
do
[ $DEBUG ] && echo -e "Extra Pool Server Start,End --> ${dataGroupArray[${tempArray[$_e]}]}p"
if [ $( echo "${dataGroupArray[${tempArray[$_e]}]}" | grep -E "[0-9]+\,[0-9]+" ) ]; then
sed -n ${dataGroupArray[${tempArray[$_e]}]}p ${iAppFreeConfFile} | sed -e "s/[\_a-zA-Z0-9\-]\+\.app\///g" >> "$vsFileName"
else
echo "ERROR - Profile Start or End or both are missing"
echo -e "#############################################\n## ERROR - Looks like there is a Data-Group in the above irule\n## and somehow i could not parsed it\n#############################################\n" >> "$vsFileName"
fi
done
unset tempArray
}
extract_poolsFromiRules(){
# this is the function which extracts pool names from iRules and inserts their start,end tuple
# informations back in iRuleArray[]. This is the second design implamantation and this is faster than previous one.
# it accepts iRuleName as first and only parameter.
local _offset _ruleName _rule _pName _m
_pName="$1"
# lets replace real line numbers with numbers in array of pools where we found.
# since we assume ruleRange is our offset, we have to replace those numbers
_offset=$( echo $ruleRange | cut -d ',' -f 1 )
if [ $DEBUG ]; then
"Looking for pool $_pName. The offset is $_offset"
"We have line numbers of the iRules which used the pool $_pName"
declare -p poolUsedInRules | tr ' ' '\n'
echo -e "---------------------------------------------\n"
fi
for _m in "${!poolUsedInRules[@]}"
do
poolUsedInRules[$_m]=$(( ${_offset} + ${poolUsedInRules[$_m]} ))
[ $DEBUG ] && echo "The actual line number of ${_m}.th is = ${poolUsedInRules[$_m]} "
_rule=$( sed -n "1,${poolUsedInRules[$_m]}p" ${iAppFreeConfFile} | tac | grep -m1 -E "ltm rule /[a-zA-Z0-9\-\_\.]+/" | sed -e 's, {,,g' -e 's,/, ,g' | awk '{ print $NF }' )
poolUsedInRules[$_m]="$_rule"
[ $DEBUG ] && echo "poolUsedInRule Array ${_m}.th element is replaced with the name of irule ${poolUsedInRules[$_m]}"
done
[ $DEBUG ] && echo "We have located all iRules which used the pool $_pName"
[ $DEBUG ] && declare -p poolUsedInRules | tr ' ' '\n'
[ $DEBUG ] && echo -e "---------------------------------------------\n"
# How about a pool used more than once in a same iRule ?
# We will sanitisize it, that's it.
for _ruleName in $( echo "${poolUsedInRules[@]}" | tr ' ' '\n' | sort | uniq )
do
[ $DEBUG ] && echo "Now we are going to put pool start,end tuples in iRuleArrays"
[ $DEBUG ] && echo "Pool is $_pName and tuples of it ${poolArray[$_pName]}"
[ $DEBUG ] && echo "The iRule is $_ruleName ${iruleArray[$_ruleName]} before adding pool tuples"
iruleArray[$_ruleName]+=';'
iruleArray[$_ruleName]+=${poolArray[$_pName]}
[ $DEBUG ] && echo "The iRule ( $_ruleName --> ${iruleArray[$_ruleName]} ) updated with pool ( $_pName --> ${poolArray[$_pName]} ) tuples"
done
unset _offset _ruleName _rule _pName _m
}
extract_monitors(){
# This function aims to find monitor definitions in pools which sent by any other functions
# We need poolName and file name where we store the details of the monitor. That is it
local _poolName _monitorName _vsFileName
declare -a _monitors
_poolName="$1"
_vsFileName="$2"
[ $DEBUG ] && echo "DEBUG - extract_monitors called with ${_poolName}" # debug
if [ $( echo ${poolArray[$_poolName]} | grep -E "[0-9]+,[0-9]+" ) ]; then
# find used pool definitions in pool lines and list them
read -a _monitors <<< $( sed -n "${poolArray[$_poolName]}p" ${iAppFreeConfFile} | sed -e "s/[\_a-zA-Z0-9\-]\+\.app\///g" | grep monitor | tr ' ' '\n' | awk -F '/' '$0 ~ /\/[a-zA-Z0-9\-\_\.]+/ { print $NF } ' | sort -u | tr '\n' ' ' )
[ $DEBUG ] && echo -e "DEBUG - Monitor Count of (${_poolName}) is ${#_monitors[@]}"
[ $DEBUG ] && sed -n "${poolArray[$_poolName]}p" ${iAppFreeConfFile}
for _monitorName in "${!_monitors[@]}"
do
case "${_monitors[$_monitorName]}" in
tcp|gateway_icmp|http|https|udp)
;;
icmp|inband|real_server|snmp_dca)
;;
tcp_echo|tcp_half_open|http_head_f5)
;;
https_443|https_head_f5)
;;
*)
[ $DEBUG ] && echo -e "DEBUG - extract_monitors pool Name = $_poolName , Monitor Name = ${_monitors[$_monitorName]} , Monitor Start,End --> ${monitorArray[${_monitors[$_monitorName]}]}p"
sed -n ${monitorArray[${_monitors[$_monitorName]}]}p ${iAppFreeConfFile} | sed -e "s/[\_a-zA-Z0-9\-]\+\.app\///g" >> "$_vsFileName"
;;
esac
done
# Below lines adds support to call directly with the pool Start,End info instead of poolName. Also we check start and end line info stays inside of poolRange.
elif [ $( echo "$_poolName" | grep -E "[0-9]+,[0-9]+" ) ] && (( $( echo "$_poolName" | cut -d ',' -f 1 ) >= $( echo $poolRange | cut -d ',' -f 1) )) && (( $( echo "$_poolName" | cut -d ',' -f 2 ) <= $( echo $poolRange | cut -d ',' -f 2) )); then
read -a _monitors <<< $( sed -n "${_poolName}p" ${iAppFreeConfFile} | sed -e "s/[\_a-zA-Z0-9\-]\+\.app\///g" | grep monitor | tr ' ' '\n' | awk -F '/' '$0 ~ /\/[a-zA-Z0-9\-\_\.]+/ { print $NF } ' | sort -u | tr '\n' ' ' )
[ $DEBUG ] && echo -e "DEBUG - Monitor Count of (${_poolName}) is ${#_monitors[@]}"
for _monitorName in "${!_monitors[@]}"
do
case "${_monitors[$_monitorName]}" in
tcp|gateway_icmp|http|https|udp)
;;
icmp|inband|real_server|snmp_dca)
;;
tcp_echo|tcp_half_open|http_head_f5)
;;
https_443|https_head_f5)
;;
*)
[ $DEBUG ] && echo -e "DEBUG - extract_monitors pool Name or Tuple = $_poolName , Monitor Name = ${_monitors[$_monitorName]} , Monitor Start,End --> ${monitorArray[${_monitors[$_monitorName]}]}p"
sed -n ${monitorArray[${_monitors[$_monitorName]}]}p ${iAppFreeConfFile} | sed -e "s/[\_a-zA-Z0-9\-]\+\.app\///g" >> "$_vsFileName"
;;
esac
done
fi
unset _monitors
}
extract_poolsFromPolicy(){
# This function picks pools used in policies. No worry about pools used more than once.
# extract_poolsFromPolicy "${policyNames[$_policy]}" "$vsFileName"
local poolNames policyName _vsFileName monitorName monitorTuples
policyName="$1"
_vsFileName="$2"
sed -n "${policyArray[$policyName]}p" "${iAppFreeConfFile}" | awk -F '/' '$0 ~ /[[:space:]]pool\s/ { print $3 }' | sort -u | \
while read -r _line
do
[ $DEBUG ] && echo "DEBUG - 12 - Pool ${_line} found in Policy ${policyName}" # debug
if [ $( echo ${poolArray[${_line}]} | grep -E "[0-9]+,[0-9]+" ) ]; then
sed -n "${poolArray[${_line}]}p" "${iAppFreeConfFile}" | sed -e "s/[\_a-zA-Z0-9\-]\+\.app\///g" >> "$_vsFileName"
## invoke "extract_monitos() functions here with pool name
## it will take care of the rest of the problem.
extract_monitors "${_line}" "$_vsFileName"
#~ if [ $( echo ${monitorName} | grep -E "\.app\/" ) ]; then
#~ monitorName=$( echo ${monitorName}__APP | cut -d "/" -f2 )
#~ fi
else
echo -e "###########################################\n## WARNING - This Policy has pool forwarding option but somehow\n## i could not find any tuples - ${poolArray[${_line}]} - ${policyName}\n###########################################" >> "$_vsFileName"
fi
monitorTuples=""
done
}
extract_iFiles(){
# This function aims to extract ifile definitions in iRules.
# basically we need to know irule name which contains "ifile get " word syntax and the "VsConfig" file name
# and a final note. It is not easy to store any kind of file content here except the txt files. We need to store them carefully
# because we are going to use those iFiles later. So, prepare to be amazed. While storing iFile contents here, we encode them with base64
# Do not forget to decode them before use !!!
local ruleName ruleStartEnd _vsFileName
if [ $# -ne 2 ]; then
echo -e 'This function (extract_iFiles) need to know the name of iRule which we are looking for'
return 30
fi
ruleName="$1"
_vsFileName="$2"
[ $DEBUG ] && echo "DEBUG - 6 - extract iFiles in rule -> $ruleName"
ruleStartEnd=$( echo ${iruleArray[${ruleName}]} | cut -d ";" -f 1 )
[ $DEBUG ] && echo "DEBUG - 7 - extract iFiles of this range -> $ruleStartEnd"
_rule=$( sed -n "${ruleStartEnd}p" ${iAppFreeConfFile} )
for k in "${!ltmiFileArray[@]}"
do
echo "$_rule" | grep -E "$k" > /dev/null 2>&1
if [ $? -eq 0 ]; then
[ $DEBUG ] && echo "DEBUG - 8 - iruleArray[ruleName] ${iruleArray[${ruleName}]}" # debug
echo "${ltmiFileArray[$k]}" | tr ';' '\n' | while read -r line
do
case "$line" in
N)
echo -e "###########################################\n## ERROR - Can not locate the iFile - ${line} \n###########################################" >> "$_vsFileName"
;;
[0-9]*,[0-9]*)
[ $DEBUG ] && echo "DEBUG - 9 - iFile and sys file iFile to ${line}" # debug
sed -n "${line}p" ${iAppFreeConfFile} | sed -e "s/[\_a-zA-Z0-9\-]\+\.app\///g" >> "$_vsFileName"
;;
[a-zA-Z0-9\-\_\/\.]*)
[ $DEBUG ] && echo "DEBUG - 10 - iFile and sys file iFile contains - ${line}"
echo "## $line ##" >> "$_vsFileName"
echo "## -------------------------###################---------------------- ##" >> "$_vsFileName"
echo "## ---------------########### iFile START ##########------------- ##" >> "$_vsFileName"
echo "## ---------------######### Encoded with Base64 ########------------- ##" >> "$_vsFileName"
echo "## -------------------------###################---------------------- ##" >> "$_vsFileName"
cat "$line" | base64 >> "$_vsFileName"
echo "" >> "$_vsFileName"
echo "## -------------------------###################---------------------- ##" >> "$_vsFileName"
echo "## ---------------########### iFile END ##########------------- ##" >> "$_vsFileName"
echo "## -------------------------###################---------------------- ##" >> "$_vsFileName"
;;
*)
echo "DEBUG - 11 - ERROR - Could not get neither any information of Start,End of any tuple nor a file path - ${line}"
;;
esac
done
fi
done
return 0
}
### Main