-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreconfigure_dns.sh
7636 lines (7400 loc) · 305 KB
/
reconfigure_dns.sh
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/sh
# Updated 20131217
# Configure DNS servers using nearest-neighbor logic
# Primary and Secondary will be chosen from Site-Specific DNS servers
# Tertiary will be chosen from between corporate DNS servers
### ###
### PRELIMINARY SETTINGS ###
### ###
# This is the default action the script will take without arguments
# Possible settings are PRETEND, UPDATE, or FORCE
# PRETEND - same as -P argument
# will go through the motions and produce a report but will not update resolv.conf
#
# UPDATE - same as -C argument
# will update resolv.conf using the safest settings and produce a report
#
# FORCE - same as -F argument
# will ignore some safety checks and update resolv.conf with recommended settings, and produce a report
# ONLY CHANGE THIS VALUE IF YOU CAN'T RUN THE SCRIPT WITH ARGUMENTS, OTHERWISE JUST USE THE ARGUMENTS
DEFAULT_ACTION=PRETEND
SERVERLIST=/maint/scripts/dnsservers.txt
TEST_A_REC=west.com
TMPLST=/tmp/dlst.tmp
OPTTMP=/tmp/dopt.tmp
RESOLV=/etc/resolv.conf
RESOLVNEW=/etc/.resolv.conf.new
TS=`date +%Y%m%d%H%M%S`
RESOLVBAK=/etc/resolv.conf.${TS}
RESOLVBAKLAST=/etc/.resolv.conf.lastbackup
export TEST_A_REC
# Determine Operating System
OS=`uname -s`
f_CleanExit () {
EC=$1
#if [ -f "/tmp/pushscript.cmd" ]; then
# $RM -f "/tmp/pushscript.cmd"
#fi
exit $EC
}
# Define executables
case $OS in
Linux ) NSLOOKUP=/usr/bin/nslookup
GREP=/bin/grep
RM=/bin/rm
BC=/usr/bin/bc
TIME=/usr/bin/time
CAT=/bin/cat
SORT=/bin/sort
AWK=/bin/awk
HEAD=/usr/bin/head
TAIL=/usr/bin/tail
EGREP=/bin/egrep
SED=/bin/sed
CP=/bin/cp
MV=/bin/mv
PS=/bin/ps
WC=/usr/bin/wc
CUT=/bin/cut
WHICH=/usr/bin/which
CHMOD=/bin/chmod
HNAME=/bin/hostname
IFCONFIG=/sbin/ifconfig
NETSTAT=/bin/netstat
if [ ! -x "$CP" ]; then
if [ -n `find /opt/cosmos | grep /cp$` ]; then
CP=`find /opt/cosmos | grep /cp$`
fi
fi
# Special command switches
NSLOOKUP_C="$NSLOOKUP -sil"
;;
AIX ) NSLOOKUP=/usr/bin/nslookup
GREP=/usr/bin/grep
RM=/usr/bin/rm
BC=/usr/bin/bc
TIME=/usr/bin/time
CAT=/usr/bin/cat
SORT=/usr/bin/sort
AWK=/usr/bin/awk
HEAD=/usr/bin/head
TAIL=/usr/bin/tail
EGREP=/usr/bin/egrep
SED=/usr/bin/sed
CP=/usr/bin/cp
MV=/usr/bin/mv
PS=/usr/bin/ps
WC=/usr/bin/wc
CUT=/usr/bin/cut
WHICH=/usr/bin/which
CHMOD=/usr/bin/chmod
HNAME=/usr/bin/hostname
IFCONFIG=/etc/ifconfig
NETSTAT=/usr/bin/netstat
# Special command switches
NSLOOKUP_C="$NSLOOKUP -sil"
VER=`uname -v`
REL=`uname -r`
export VER
if [ "$VER" = "4" ]; then
echo ",FAILURE:UNSUPPORTED_OS_OR_VERSION-AIX${VER}.${REL}:EXIT_CODE_7"
f_CleanExit 7
exit
fi
if [ "$VER" = "5" ]; then
if [ "$REL" = "1" ]; then
echo ",FAILURE:UNSUPPORTED_OS_OR_VERSION-AIX${VER}.${REL}:EXIT_CODE_7"
f_CleanExit 7
exit
fi
if [ "$REL" = "2" ]; then
echo ",FAILURE:UNSUPPORTED_OS_OR_VERSION-AIX${VER}.${REL}:EXIT_CODE_7"
f_CleanExit 7
exit
fi
fi
;;
SunOS ) NSLOOKUP=/usr/sbin/nslookup
GREP=/usr/bin/grep
RM=/usr/bin/rm
BC=/usr/bin/bc
TIME=/usr/bin/time
CAT=/usr/bin/cat
SORT=/usr/bin/sort
AWK=/usr/bin/awk
HEAD=/usr/bin/head
TAIL=/usr/bin/tail
EGREP=/usr/bin/egrep
SED=/usr/bin/sed
CP=/usr/bin/cp
MV=/usr/bin/mv
PS=/usr/bin/ps
WC=/usr/bin/wc
CUT=/usr/bin/cut
WHICH=/usr/bin/which
CHMOD=/usr/bin/chmod
HNAME=/usr/bin/hostname
IFCONFIG=/usr/sbin/ifconfig
NETSTAT=/usr/bin/netstat
BASH=/usr/bin/bash
# Special command switches
if [ `uname -r` = 5.8 ]; then
NSLOOKUP_C=$NSLOOKUP
else
NSLOOKUP_C="$NSLOOKUP -sil"
fi
;;
* ) echo ",FAILURE:UNSUPPORTED_OS_OR_VERSION-${OS}:EXIT_CODE_2"
f_CleanExit 2
exit
;;
esac
# Verify that the essential executables are all present and accounted for
REQ_BINS="$NSLOOKUP $GREP $RM $BC $TIME $CAT $SORT $AWK $HEAD $TAIL $EGREP $SED $CP $MV $PS $WC $CUT $WHICH $CHMOD $HNAME $IFCONFIG $NETSTAT"
# Solaris uniquely requires BASH
if [ "$OS" = "SunOS" ]; then
REQ_BINS="$REQ_BINS $BASH"
fi
# Assume pre-check failure is false to start with - it'll be switched to TRUE for any single failure
PCF=FALSE
ML=
for REQ_BIN in $REQ_BINS; do
if [ ! -x "$REQ_BIN" ]; then
PCF=TRUE
ML="$ML;$REQ_BIN"
fi
done
if [ $PCF = TRUE ]; then
echo ",FAILURE:MISSING_REQUIRED_BINARY(s)[$ML]:EXIT_CODE_3"
f_CleanExit 3
exit
fi
# The Solaris version of /bin/sh is not compatible with this script, so
# if we encounter Solaris, we'll attempt to change the shebang and re-run
# the script with BASH
if [ "$OS" = "SunOS" ]; then
# Define the temporary executable name
SOLEX=/tmp/configure_dns_solaris.sh
# Check to see if the shebang is already set to BASH
$HEAD -1 $0 | $GREP $BASH 2>&1 >> /dev/null
if [ $? != 0 ]; then
# If not already set to BASH, then we'll need to write
# out the new executable
if [ -f $SOLEX ]; then $RM $SOLEX; fi
# Escape all of the slashes in the BASH path string so we can use it with sed
EBASH=`echo $BASH | $SED 's/\\//\\\\\//g'`
# Use sed to replace the default shebang with BASH
$SED "s/^#!\/bin\/sh/#!${EBASH}/" $0 > $SOLEX
# Verify that we wrote a file and it isn't empty
if [ -s $SOLEX ]; then
# Make the new temp file executable
$CHMOD +x $SOLEX
# Execute the new script and capture it's exit code as EC
$SOLEX
EC=$?
# Remove the temp script
$RM $SOLEX
# Exit with whatever code the temp script exited with
exit $EC
else
echo ",FAILURE:FAILURE_TO_CREATE$SOLEX:EXIT_CODE_8"
f_CleanExit 8
exit
fi
fi
fi
# Export executable variables
export NSLOOKUP GREP RM BC TIME CAT SORT AWK HEAD TAIL EGREP SED CP MV PS WC CUT WHICH CHMOD HNAME NSLOOKUP_C IFCONFIG NETSTAT
### ###
### END PRELIMINARY SETTINGS ###
### ###
### ###
### FUNCTION DEFINITIONS ###
### ###
f_Usage () {
echo "Usage: "
echo " $0 [-F|-P]"
echo ""
echo ""
echo " If run without arguments the script will run in pretend/read only mode"
echo ""
echo " The script will automatically try to determine the best servers"
echo " based on the contents of /maint/scripts/dnsservers.txt"
echo " The format of the file must be:"
echo " <type> <DNS Server FQDN> <DNS Server IP>"
echo ""
echo " Where:"
echo " <type> is either \"s\" for site-specific, or \"p\" for primary site"
echo " <DNS Server FQDN> is the fully qualified domain name of the DNS Server"
echo " <DNS Server IP> is the IP address of the server"
echo ""
echo " The script will choose the two fastest-responding site servers as primary"
echo " and secondary servers, and the fastest of the primary site servers as a"
echo " tertiary."
echo ""
echo " If the file is missing, hard-coded internal defaults will be used."
echo ""
echo " By default the script will only replace DNS servers that it knows about."
echo " If the /etc/resolv.conf of a server has DNS servers that are not recognized"
echo " They will be left intact and in the same order as they originally appeared."
echo ""
echo " If a server is not configured for DNS at all, the script will not attempt"
echo " to make any changes, as doing so may impact system performance."
echo ""
echo " OPTIONS:"
echo ""
echo " -F FORCE override both safety catches: replace unknown servers in resolv.conf"
echo " with the recommended servers, and configure servers that are not"
echo " presently configured for DNS."
echo ""
echo " -P Pretend only - don't make updates, just show what updates would have"
echo " been made. (This is the default action)"
echo ""
echo " -C Change/update. This will replace known legacy servers, preserve all existing"
echo " optional settings, and avoid configuring servers that don't already use DNS."
echo ""
}
# Function to get average response time from a DNS server
# Usage: f_DNSAverageResponse <IP>
# Output: <average response time in seconds>
# OR
# -1 (DNS Service not found)
f_DNSAverageResponse () {
unset DITC
DITC=$1
# Verify that we're even getting responses on port 53
RESPONDS=NO
if [[ `uname -s` == SunOS ]] && [[ `uname -r` == 5.8 ]]; then
if [[ -z `echo -e "set timeout=3\nset retries=2\nserver $DITC\nwest.com\nexit\n" | $NSLOOKUP 2>&1 | $EGREP -i "no response"` ]]; then
RESPONDS=YES
fi
elif [[ `uname -s` == AIX ]] && [[ `uname -v` == 5 ]]; then
if [[ -z `echo "set timeout=3\nset retries=2\nserver $DITC\nwest.com\nexit\n" | $NSLOOKUP 2>&1 | $EGREP -i "no response"` ]]; then
RESPONDS=YES
fi
else
if [[ -z `$NSLOOKUP_C west.com ${DITC} 2>&1 | $GREP "connection timed out"` ]]; then
RESPONDS=YES
fi
fi
if [[ $RESPONDS == YES ]]; then
# make sure we're always dealing with fresh variables
unset thistime totaltime average lasttime fastesttime
#Set the number of subsequent times to check
CHECKCOUNT=8
CHECK=0
while [[ $CHECK -lt $CHECKCOUNT ]]; do
#for i in {1..4}; do
# Adding a sleep here will greatly extend the running time of this script but may get us cleaner results
#sleep 1
# measure time with nmap
#thistime=`/usr/bin/nmap -sU -p 53 $DITC 2>&1 | grep seconds | awk '{print $(NF-1)}'`
# measure with time and nslookup
# perform one lookup to get the entry in cache, so we're averaging raw response time
if [[ `uname -s` == SunOS ]] && [[ `uname -r` == 5.8 ]]; then
echo -e "set timeout=3\nset retries=2\nserver $DITC\nwest.com\nexit\n" | $NSLOOKUP 2>&1 > /dev/null
thistime=`{ time echo -e "set timeout=3\nset retries=2\nserver $DITC\nwest.com\nexit\n" | $NSLOOKUP; } 2>&1 | $GREP ^real | $AWK -F'm' '{print $2}' | $SED 's/s$//'`
elif [[ `uname -s` == AIX ]] && [[ `uname -v` == 5 ]]; then
echo "set timeout=3\nset retries=2\nserver $DITC\nwest.com\nexit\n" | $NSLOOKUP 2>&1 > /dev/null
thistime=`{ time echo "set timeout=3\nset retries=2\nserver $DITC\nwest.com\nexit\n" | $NSLOOKUP; } 2>&1 | $GREP ^real | $AWK -F'm' '{print $2}' | $SED 's/s$//'`
else
$NSLOOKUP_C west.com $DITC 2>&1 > /dev/null
thistime=`{ time $NSLOOKUP_C west.com ${DITC}; } 2>&1 | $GREP ^real | $AWK -F'm' '{print $2}' | $SED 's/s$//'`
fi
# first time through, just assign thstime to totaltime
if [[ -z $totaltime ]]; then
totaltime=$thistime
else
totaltime=`echo "$totaltime + $thistime" | $BC -l`
fi
# Calculate the fastest response observed out of the sample
if [[ -z $lasttime ]] || [[ $thistime < $lasttime ]]; then
fastesttime=$thistime
fi
let CHECK=$CHECK+1
done
average=`echo "$totaltime / $CHECKCOUNT" | $BC -l | $CUT -b1-5`
lasttime=$thistime
#echo $average
echo $totaltime
#echo $fastesttime
else
# The server doesn't respond to DNS at all
echo -1
fi
}
#-----------------
# Function: f_ValidIPv4
#-----------------
# Checks a string to ensure it contains a valid
# ipv4 address
#-----------------
# Usage: f_ValidIPv4 <IP>
#-----------------
# Returns: TRUE, FALSE
f_ValidIPv4 () {
IPv4=$1
# Valid until proven otherwise
RESULT=TRUE
# Does it have 4 octets?
if [[ `echo $IPv4 | $AWK -F'.' '{print NF}'` -ne 4 ]]; then
RESULT=FALSE
else
# Look at each octet
for o in `echo $IPv4 | $SED 's/\./ /g'`; do
# Is the octet numeric?
if [[ -z `echo $o | $EGREP "^[0-9]+$"` ]]; then
RESULT=FALSE
else
# Is the octet less than 0 or greater than 255?
if [[ $o -lt 0 ]] || [[ $o -gt 255 ]]; then
RESULT=FALSE
fi
fi
done
fi
echo $RESULT
}
### ###
### END FUNCTION DEFINITIONS ###
### ###
##############################
### ###
### MAIN EXECUTION START ###
### ###
##############################
############################################################################
###################
### DNS NOMINATION
### ###
### READ ARGUMENTS ###
### ###
# The default action is pretend - this is to prevent accidental modification of a system
ACTION=$DEFAULT_ACTION
if [[ -n "$1" ]]; then
if [[ "$1" == "-C" ]]; then
ACTION=UPDATE
elif [[ "$1" == "-F" ]]; then
ACTION=FORCE
elif [[ "$1" == "-P" ]]; then
ACTION=PRETEND
else
f_Usage
exit 5
fi
fi
### ###
### END READ ARGUMENTS ###
### ###
### ###
### CHECK CURRENT STATE ###
### ###
# First need to see if this server is even configured for DNS
USEDNS=NO
if [[ -s "$RESOLV" ]] && [[ -n `$EGREP -v "^.*#" $RESOLV | $EGREP "nameserver"` ]]; then
USEDNS=YES
fi
### ###
### END CHECK CURRENT STATE ###
### ###
### ###
### CHECK UPDATE POTENTIAL ###
### ###
# Verify whether resolv.conf is writeable
if [[ ! -w "$RESOLV" ]]; then
WRITABLE=NO
# If we're doing a "pretend" operation, this isn't such a big deal
# but if we're doing an update operation, we're going to exit with a failure code
if [[ $ACTION == UPDATE ]] || [[ $ACTION == FORCE ]]; then
echo ",FAILURE:RESOLV.CONF NOT WRITABLE BY USER $USER:EXIT_CODE_9"
f_CleanExit 9
exit
fi
else
WRITABLE=YES
fi
if [[ "$USEDNS" == "NO" ]] && [[ "$ACTION" != "FORCE" ]]; then
echo ",FAILURE:SERVER DOES NOT USE DNS:EXIT_CODE_13"
f_CleanExit 13
exit
fi
### ###
### END CHECK UPDATE POTENTIAL ###
### ###
### ###
### READ/SET VARIABLES ###
### ###
# This is a list of servers that need to be removed from
# resolv.conf
#LEGACY_IPLIST="172.30.9.151 172.30.94.108 172.30.41.227 10.28.101.26 216.57.98.32 216.57.106.45 216.57.106.46 216.57.98.33 216.57.102.97 216.57.102.98 216.57.106.48 216.57.106.49 216.57.102.95 216.57.102.96 216.57.110.24 216.57.110.25 216.57.102.42 10.28.101.40 10.28.101.41 10.64.10.74 10.64.10.75 10.70.1.25 10.70.1.26 172.30.41.224 172.30.41.225 172.30.8.24 172.30.8.25 172.30.8.204 192.168.18.15 192.168.18.16 192.168.45.14 192.168.45.15 192.168.12.14 192.168.12.15"
LEGACY_IPLIST="10.0.0.210 10.0.14.100 10.0.35.210 10.19.124.23 10.28.101.26 10.28.101.40 10.28.101.41 10.51.254.102 10.62.21.10 10.62.21.20 10.62.220.2 10.64.10.74 10.64.10.75 10.70.1.25 10.70.1.26 10.70.192.22 10.70.71.68 172.30.41.224 172.30.41.225 172.30.41.227 172.30.8.204 172.30.8.24 172.30.8.25 172.30.9.151 172.30.9.228 172.30.94.108 172.30.94.116 192.168.12.14 192.168.12.15 192.168.18.15 192.168.18.16 192.168.45.14 192.168.45.15"
#LEGACY_TER="10.62.21.10 10.62.21.20 172.30.9.151 172.30.94.108"
LEGACY_TER="10.0.0.210 10.0.14.100 10.0.35.210 10.19.124.23 10.28.101.26 10.28.101.40 10.28.101.41 10.51.254.102 10.62.21.10 10.62.21.20 10.62.220.2 10.64.10.74 10.64.10.75 10.70.1.25 10.70.1.26 10.70.192.22 10.70.71.68 172.30.41.224 172.30.41.225 172.30.41.227 172.30.8.204 172.30.8.24 172.30.8.25 172.30.9.151 172.30.9.228 172.30.94.108 172.30.94.116 192.168.12.14 192.168.12.15 192.168.18.15 192.168.18.16 192.168.45.14 192.168.45.15"
# Default values - this is a fallback in case everything else fails
D_PRIMARY="10.19.119.82"
D_SECONDARY="10.19.119.83"
D_TERTIARY="10.17.126.43"
D_SEARCH="wic.west.com icallinc.com one.west.com"
# Try to see if we have a serverlist - that will be the preferred method
#echo "Looking for $SERVERLIST"
if [[ -s $SERVERLIST ]]; then
PRIMARY_IPLIST=`grep ^p $SERVERLIST | awk '{print $3}'`
SS_IPLIST=`grep ^s $SERVERLIST | awk '{print $3}'`
#else
# echo " not found, using defaults"
fi
# If we don't have these populated, then fall back on static values
if [[ -z $PRIMARY_IPLIST ]]; then
PRIMARY_IPLIST="10.19.119.82 10.19.119.83"
fi
if [[ -z $SS_IPLIST ]]; then
SS_IPLIST="10.28.101.51 10.28.101.52 10.70.1.60 10.70.1.61 10.17.126.43 10.17.126.44 10.64.10.152 10.64.10.153 10.166.128.52 10.166.128.53"
fi
### ###
### END READ/SET VARIABLES ###
### ###
### ###
### CALCULATE RECOMMENDED CONFIG ###
### ###
# Determine primary and secondary servers
#echo "Negotiating best Site-Specific DNS servers."
#echo ""
# Remove any previous version of TMPLST
if [[ -f $TMPLST ]]; then $RM $TMPLST; fi
for s in $SS_IPLIST; do
# echo "Checking server $s"
# Make sure the server actually responds to dns requests
RESPONDS=NO
if [[ `uname -s` == SunOS ]] && [[ `uname -r` == 5.8 ]]; then
if [[ -z `echo -e "set timeout=3\nset retries=2\nserver $s\n$TEST_A_REC\nexit\n" | $NSLOOKUP 2>&1 | grep -i "no response"` ]]; then
RESPONDS=YES
fi
else
if [[ -z `$NSLOOKUP_C $TEST_A_REC ${s} 2>&1 | $GREP "connection timed out"` ]]; then
RESPONDS=YES
fi
fi
if [[ $RESPONDS == YES ]]; then
# Add the server to the validated version of the list
V_SS_IPLIST="$V_SS_IPLIST,$s,"
# Collect the lookup data (may not be average, but will be sortable just the same
time=`f_DNSAverageResponse $s`
if [[ $time != -1 ]]; then
# If we got a valid response time for the server, then add it to the TMPLST
echo "$s,$time" >> $TMPLST
fi
fi
done
if [[ ! -s $TMPLST ]]; then
echo ",FAILURE:NO_REACHABLE_PRIMARY_SECONDARY:EXIT_CODE_6"
f_CleanExit 6
exit
fi
#Use sort to grab the two fastest servers from the list
PRIMARY_SS=`$CAT $TMPLST | $SORT -n -t , -k2 | $HEAD -1 | $AWK -F',' '{print $1}'`
SECONDARY_SS=`$CAT $TMPLST | $SORT -n -t , -k2 | head -2 | $TAIL -1 | $AWK -F',' '{print $1}'`
$RM $TMPLST
# Now pick a tertiary from the list of primary DNS servers
#echo "Negotiating best Tertiary DNS server."
#echo ""
for p in $PRIMARY_IPLIST; do
# Make sure the server actually responds to DNS requests
if [[ -z `$NSLOOKUP_C $TEST_A_REC $p 2>&1 | $GREP -i "connection timed out"` ]]; then
# Add to valid list
V_PRIMARY_IPLIST="$V_PRIMARY_IPLIST,$p,"
# Collect response times
time=`f_DNSAverageResponse $p`
echo "$p,$time" >> $TMPLST
fi
done
if [[ ! -s $TMPLST ]]; then
echo ",FAILURE:NO_REACHABLE_TERTIARY:EXIT_CODE_60"
f_CleanExit 6
exit
fi
TERTIARY=`$CAT $TMPLST | $SORT -n -t , -k2 | $HEAD -1 | $AWK -F',' '{print $1}'`
$RM $TMPLST
# Verify that the value we pulled out of the TMPLST is a valid IP address
# Different systems may handle certain syntax differently
EP_VALID=`f_ValidIPv4 $PRIMARY_SS`
ES_VALID=`f_ValidIPv4 $SECONDARY_SS`
ET_VALID=`f_ValidIPv4 $TERTIARY`
if [[ "$EP_VALID" != "TRUE" ]] || [[ "$ES_VALID" != "TRUE" ]] || [[ "$ET_VALID" != "TRUE" ]]; then
echo ",FAILURE:INVALID SERVER ADDRESSES REPORTED BY ELECTION EP=\"$PRIMARY_SS\" ES=\"$SECONDARY_SS\" ET=\"$TERTIARY\":EXIT_CODE_14"
f_CleanExit 14
exit
fi
# Check against hardware list
# Set a general deviation flag for easy identification
DEVIATE=NO
# Set this server's hostname
THN=`$HNAME | $AWK -F'.' '{print $1}'`
# Get the location by hostname
THLOC=`$GREP -i "^##@NIBU@##${THN}," $0 | $AWK -F',' '{print $4}'`
# Attempt to set the location by IP
TGW=`$NETSTAT -rn | $EGREP "^0.0.0.0|^default" | $AWK '{print $2}'`
if [ "$TGW" = "0.0.0.0" ]; then
if [ -f /etc/sysconfig/network ]; then
if [ -n `$GREP "^GATEWAY=" /etc/sysconfig/network` ]; then
TGW=`$GREP "^GATEWAY=" /etc/sysconfig/network | $AWK -F'=' '{print $2}'`
fi
fi
fi
TNWP=`echo $TGW | $AWK -F'.' '{print $1"."$2"."$3"."}'`
TIP=`$IFCONFIG -a | $GREP $TNWP | $AWK '{print $2}' | $SED 's/^addr://' | $HEAD -1`
TILOC=`$GREP ",${TIP}," $0 | $GREP "^##@NIBU@##" | $AWK -F',' '{print $4}' | $HEAD -1`
# See if the locationi by hostname and location by ip match
HWDBMATCH=NO
if [[ -n $THLOC ]] && [[ -n $TILOC ]]; then
if [[ -n `echo $THLOC | $GREP -i "^${TILOC}$"` ]]; then
HWDBMATCH=YES
fi
else
HDBMATCH=N/A
fi
# if location is not found, set it to unknown
if [[ -z $THLOC ]]; then
THLOC=UNK
THBU=UNK
else
THBU=`$GREP -i "^##@NIBU@##${THN}," $0 | $AWK -F',' '{print $3}' | $HEAD -1`
fi
if [[ -z $TILOC ]]; then
TILOC=UNK
TIBU=UNK
else
TIBU=`$GREP ",${TIP}," $0 | $GREP "^##@NIBU@##" | $AWK -F',' '{print $3}' | $HEAD -1`
fi
PLOC=`$GREP "^##@NSSC@##${PRIMARY_SS}," $0 | $AWK -F',' '{print $2}'`
SLOC=`$GREP "^##@NSSC@##${SECONDARY_SS}," $0 | $AWK -F',' '{print $2}'`
# See if the (either of) the server's site(s) in the hw database has a SS dns server
SWS='^ATL01$|^SWN01$|^DEN01$|^DEN06$|^OMA10$|^LON13$'
if [[ -n `echo $THLOC | $EGREP -i "$SWS"` ]] || [[ -n `echo $TILOC | $EGREP -i "$SWS"` ]]; then
HASL=YES
# Verify that the elected servers are the proper ones for this server according to it's location
# in the hw database either by hostname or IP
if [[ -n `echo $PLOC | $EGREP -i "^${THLOC}$|^${TILOC}$"` ]]; then
PMATCH=MATCH
else
PMATCH=MISMATCH
DEVIATE=YES
fi
if [[ -n `echo $SLOC | $EGREP -i "^${THLOC}$|^${TILOC}$"` ]]; then
SMATCH=MATCH
else
SMATCH=MISMATCH
DEVIATE=YES
fi
else
# If there is no local/site specific dns server then matching is not necessary
HASL=NO
PMATCH=N/A
SMATCH=N/A
fi
### DNS NOMINATION
###################
############################################################################
############################################################################
###################
### HOST ANALYSIS
### ###
### GET CURRENT SETTINGS ###
### ###
# If the server currently uses DNS, read the current values.
if [[ "$USEDNS" == "YES" ]]; then
# Read the current configuration - options will be handled separately during the update process
C_SEARCH=`$EGREP -v "^#" $RESOLV | $EGREP "^.*search" | $SED "s/^.*search[ \t]//"`
C_DOMAIN=`$EGREP -v "^#" $RESOLV | $EGREP "^.*domain" | $SED "s/^.*domain[ \t]//"`
# For DNS resolver addresses, we need to determine how many are being used to accurately set the variables
RESOLVER_COUNT=`$EGREP -v "^#" $RESOLV | $EGREP "^.*nameserver" | wc -l`
C_PRIMARY=`$EGREP -v "^#" $RESOLV | $EGREP "^.*nameserver" | $SED "s/^.*nameserver[ \t]//" | $AWK '{print $1}' | $HEAD -1`
if [[ $RESOLVER_COUNT -ge 2 ]]; then
C_SECONDARY=`$EGREP -v "^#" $RESOLV | $EGREP "^.*nameserver" | $SED "s/^.*nameserver[ \t]//" | $AWK '{print $1}' | $HEAD -2 | tail -1`
if [[ $RESOLVER_COUNT -ge 3 ]]; then
C_TERTIARY=`$EGREP -v "^#" $RESOLV | $EGREP "^.*nameserver" | $SED "s/^.*nameserver[ \t]//" | $AWK '{print $1}' | $HEAD -3 | tail -1`
fi
fi
fi
### ###
### END GET CURRENT SETTINGS ###
### ###
### HOST ANALYSIS
###################
############################################################################
############################################################################
###################
### EXECUTE AND REPORT
### ###
### DECIDE NEW RESOLVERS ###
### ###
# Only servers we know about will be replaced
META_IPLIST="$LEGACY_IPLIST $SS_IPLIST $PRIMARY_IPLIST"
# If we successfully elected a primary and...
# the result we got back is definitely an IP address and...
# either there was no existing primary or the existing primary is on or list of servers to remove...
if [[ -n $PRIMARY_SS ]] && [[ `f_ValidIPv4 $PRIMARY_SS` != FALSE ]] && ( [[ -z $C_PRIMARY ]] || [[ -n `echo $META_IPLIST | grep $C_PRIMARY` ]] ); then
# If the server has a local server and its BU is WIC then we'll force it to use the local server
# regardless of the election process
if [[ $HASL == YES ]] && ([[ "$THBU" == "WIC" ]] || [[ "$TIBU" == "WIC" ]]); then
N_PRIMARY=`$GREP "^##@NSSC@##" $0 | $SED 's/^##@NSSC@##//g' | $AWK -F',' -v L=$TILOC '{if($2==L) print $1}' | $HEAD -1`
if [[ -z $N_PRIMARY ]]; then
N_PRIMARY=`$GREP "^##@NSSC@##" $0 | $SED 's/^##@NSSC@##//g' | $AWK -F',' -v L=$THLOC '{if($2==L) print $1}' | $HEAD -1`
fi
# Since we're bypassing the algorithm for WIC, it's no longer safe to assume that the elected server is reachable
if [[ -z `echo $V_SS_IPLIST | egrep ",$N_PRIMARY,|,$N_PRIMARY$"` ]]; then
echo ",FAILURE:WIC SITE OVERRIDE ($TILOC) SERVER $N_PRIMARY NOT REACHABLE:EXIT_CODE_10"
f_CleanExit 10
exit
fi
else
N_PRIMARY=$PRIMARY_SS
fi
else
N_PRIMARY=$C_PRIMARY
fi
# Negotiate Secondary
if [[ -n $SECONDARY_SS ]] && [[ `f_ValidIPv4 $SECONDARY_SS` != FALSE ]] && ( [[ -z $C_SECONDARY ]] || [[ -n `echo $META_IPLIST | grep $C_SECONDARY` ]] ); then
# If the server has a local server and its BU is WIC then we'll force it to use the local server
# regardless of the election process
if [[ $HASL == YES ]] && ([[ "$THBU" == "WIC" ]] || [[ "$TIBU" == "WIC" ]]); then
N_SECONDARY=`$GREP "^##@NSSC@##" $0 | $SED 's/^##@NSSC@##//g' | $AWK -F',' -v L=$TILOC '{if($2==L) print $1}' | $TAIL -1`
if [[ -z $N_SECONDARY ]]; then
N_SECONDARY=`$GREP "^##@NSSC@##" $0 | $SED 's/^##@NSSC@##//g' | $AWK -F',' -v L=$THLOC '{if($2==L) print $1}' | $TAIL -1`
fi
# Since we're bypassing the algorithm for WIC, it's no longer safe to assume that the elected server is reachable
if [[ -z `echo $V_SS_IPLIST | egrep ",$N_SECONDARY,|,$N_SECONDARY$"` ]]; then
echo ",FAILURE:WIC SITE OVERRIDE ($TILOC) SERVER $N_SECONDARY NOT REACHABLE:EXIT_CODE_10"
f_CleanExit 10
exit
fi
else
N_SECONDARY=$SECONDARY_SS
fi
else
N_SECONDARY=$C_SECONDARY
fi
# Negotiate Tertiary - if the intercall server(s) "LEGACY_TER" is tertiary, it will be replaced
if [[ -n $TERTIARY ]] && [[ `f_ValidIPv4 $TERTIARY` != FALSE ]] && ( [[ -z $C_TERTIARY ]] || [[ -n `echo $META_IPLIST | grep $C_TERTIARY` ]] || [[ -n `echo $LEGACY_TER | grep "$C_TERTIARY"` ]] ); then
N_TERTIARY=$TERTIARY
else
N_TERTIARY=$C_TERTIARY
fi
### ###
### DECIDE NEW RESOLVERS ###
### ###
### ###
### EXECUTE ACTION / GENERATE CONFIG ###
### ###
#if [[ $ACTION == PRETEND ]]; then
# RESOLVBAK_S=N/A
# UPDATE_GENERATED=NO
#
#elif [[ $ACTION == UPDATE ]]; then
if [[ $ACTION == UPDATE ]] || [[ $ACTION == PRETEND ]]; then
# Check to see if we actually need to update anything - if the new pri, sec, ter are the same as current, then we don't
if [[ "$N_PRIMARY" == "$C_PRIMARY" ]] && [[ "$N_SECONDARY" == "$C_SECONDARY" ]] && [[ "$N_TERTIARY" == "$C_TERTIARY" ]]; then
RESOLVBAK_S=N/A
UPDATE_GENERATED=NO
else
if [[ $WRITABLE == YES ]]; then
# Preserve any options that may have been set
if [[ -n `$EGREP -v "^#" $RESOLV | $EGREP "^.*options"` ]]; then
$EGREP -v "^#" $RESOLV | $EGREP "^.*options" > $OPTTMP
fi
# Clear out any previous "new" files that didn't get cleaned up
if [[ -f ${RESOLVNEW} ]]; then echo > ${RESOLVNEW}; fi
# Add a domain line if the server originally had one
if [[ -n $C_DOMAIN ]]; then
echo "domain $C_DOMAIN" >> ${RESOLVNEW}
fi
# Add a search line. Use the default if the server did not have one to begin with.
if [[ -n $C_SEARCH ]]; then
echo "search $C_SEARCH" >> ${RESOLVNEW}
else
echo "search $D_SEARCH" >> ${RESOLVNEW}
fi
# Add primary server
echo "nameserver $N_PRIMARY" >> ${RESOLVNEW}
# Add secondary server IF we have one
if [[ -n $N_SECONDARY ]]; then
echo "nameserver $N_SECONDARY" >> ${RESOLVNEW}
fi
# Add tertiary server IF we have one
if [[ -n $N_TERTIARY ]]; then
echo "nameserver $N_TERTIARY" >> ${RESOLVNEW}
fi
# Add in any options that were present in the original file
if [[ -s $OPTTMP ]]; then
$CAT $OPTTMP >> ${RESOLVNEW}
$RM "$OPTTMP"
fi
# The new config has been generated
# Updated will be set to yes here, but may still be switched back to no
# if the update fails for some reason, such as backup failure, etc...
if [[ -s "${RESOLVNEW}" ]]; then
UPDATE_GENERATED=YES
else
UPDATE_GENERATED=NO
fi
else
UPDATE_GENERATED=NO
fi
fi
elif [[ $ACTION == FORCE ]]; then
# Check to see if we actually have enough data to forcibly update
if ([[ -n $PRIMARY_SS ]] && [[ `f_ValidIPv4 $PRIMARY_SS` != FALSE ]]) && ([[ -n $SECONDARY_SS ]] && [[ `f_ValidIPv4 $SECONDARY_SS` != FALSE ]]) && ([[ -n $TERTIARY ]] && [[ `f_ValidIPv4 $TERTIARY` != FALSE ]]); then
if [[ $WRITABLE == YES ]]; then
# Preserve any options that may have been set
if [[ -n `$EGREP -v "^#" $RESOLV | $EGREP "^.*options"` ]]; then
$EGREP -v "^#" $RESOLV | $EGREP "^.*options" > $OPTTMP
fi
# Clear out any previous "new" files that didn't get cleaned up
if [[ -f ${RESOLVNEW} ]]; then echo > ${RESOLVNEW}; fi
# Add a domain line if the server originally had one
if [[ -n $C_DOMAIN ]]; then
echo "domain $C_DOMAIN" >> ${RESOLVNEW}
fi
# Add a search line. Use the default if the server did not have one to begin with.
if [[ -n $C_SEARCH ]]; then
echo "search $C_SEARCH" >> ${RESOLVNEW}
else
echo "search $D_SEARCH" >> ${RESOLVNEW}
fi
# Add primary server
echo "nameserver $PRIMARY_SS" >> ${RESOLVNEW}
# Add secondary server
echo "nameserver $SECONDARY_SS" >> ${RESOLVNEW}
# Add tertiary server IF we have one
echo "nameserver $TERTIARY" >> ${RESOLVNEW}
# Add in any options that were present in the original file
if [[ -s $OPTTMP ]]; then
$CAT $OPTTMP >> ${RESOLVNEW}
$RM "$OPTTMP"
fi
# The new config has been generated
# Updated will be set to yes here, but may still be switched back to no
# if the update fails for some reason, such as backup failure, etc...
if [[ -s "${RESOLVNEW}" ]]; then
UPDATE_GENERATED=YES
else
UPDATE_GENERATED=NO
fi
else
UPDATE_GENERATED=NO
fi
else
RESOLVBAK_S=N/A
UPDATE_GENERATED=NO
fi
else
UPDATED=NO