Changeset 10645 in hybrid
- Timestamp:
- May 2, 2012, 8:00:55 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/releng-9.0/nanobsd/files/tools/nameserver-shuffle
r10643 r10645 1 #!/bin/sh 1 #!/bin/sh - 2 2 # 3 3 # Shuffle nameservers listed based on the query times, if enabled in 4 4 # resolv.conf. Bare in mind the special syntax. 5 5 # 6 # Rick van der Zwet < info@rickvanderzwet.nl>6 # Rick van der Zwet <rick@wirelessleiden.nl> 7 7 # 8 8 9 # ``Random'' sleep between 0 and 30 minutes to avoid ``slamming'' on the DNS door 9 # ``Random'' sleep to avoid ``slamming'' on the DNS door 10 verbose=true 10 11 if [ "$1" = "cron" ]; then 12 verbose=false 11 13 sleep `expr $$ % 30` 12 14 fi 13 15 14 TAG='^# TAG: DYNAMIC LIST$' 16 TAG='^# START DYNAMIC LIST - updated by /tools/nameserver-shuffle$' 17 $verbose && echo "# Searching in /etc/resolv.conf for tag '$TAG'" 15 18 16 19 TDIR=`mktemp -d -t $(basename $0)` 20 # Cleanup before going home 21 trap "rm -Rf $TDIR; exit 1" 1 2 3 15 22 trap "rm -Rf $TDIR; exit 0" 0 23 17 24 DYNLIST=$TDIR/dynlist 18 25 RESULTLIST=$TDIR/resultlist … … 20 27 21 28 # Get enabled DYNAMIC LIST nameservers 22 sed "1, /$TAG/d" /etc/resolv.conf > $DYNLIST23 NAMESERVERS=`awk '/^nameserver/ {print $2}' $DYNLIST` 29 sed "1,\+$TAG+d" /etc/resolv.conf > $DYNLIST || exit 1 30 NAMESERVERS=`awk '/^nameserver/ {print $2}' $DYNLIST` || exit 1 24 31 25 32 # Only do something if we have dynamic nameservers 33 $verbose && echo "# Processing" `echo $NAMESERVERS | wc -w` nameservers 26 34 if [ -n "$NAMESERVERS" ]; then 27 35 # Find query times 28 36 for NAMESERVER in $NAMESERVERS; do 37 $verbose && printf "## Testing nameserver %-16s query time: " $NAMESERVER 29 38 # Strict checking to avoid buggy links to return decent results. 30 39 QUERY_TIME=`dig +time=1 +tries=1 SOA wleiden.net @$NAMESERVER | awk '/Query time:/ {print $4}'` 31 40 # Failed to complete succesfully 32 [ -z "$QUERY_TIME" ] && QUERY_TIME=9999 41 [ -z "$QUERY_TIME" ] && QUERY_TIME="9999" 42 $verbose && echo "$QUERY_TIME" 33 43 echo "$QUERY_TIME $NAMESERVER" >> $RESULTLIST 34 44 done 35 45 36 46 # Get the header part 37 sed -n "1, /$TAG/p" /etc/resolv.conf > $NEWRESOLV47 sed -n "1,\+$TAG+p" /etc/resolv.conf > $NEWRESOLV || exit 1 38 48 39 49 # Output sorted list 40 50 NAMESERVERS=`sort -n $RESULTLIST | awk '{print $2}'` 41 51 for NAMESERVER in $NAMESERVERS; do 42 grep '\b'$NAMESERVER'\b' $DYNLIST >> $NEWRESOLV 52 QUERY_TIME=`grep $NAMESERVER $RESULTLIST | cut -d' ' -f1` 53 [ $QUERY_TIME = "9999" ] && STATUS="Query time: down" || STATUS="Query time: $QUERY_TIME" 54 # awk magic to get maximum length of comment field (for display purposes). 55 ML=`awk '/^nameserver/ {l=length($4);if (l>ml){ml=l}}END{print ml}' $DYNLIST` 56 # awk magic allows adding or updating of status of nameserver 57 awk '/^nameserver[[:blank:]]+'"$NAMESERVER"'[[:blank:]]*/ {printf "nameserver %-16s # %-'$ML's (%s)\n", $2, $4,"'"$STATUS"'"}' $DYNLIST >> $NEWRESOLV || exit 1 43 58 done 44 45 cp $NEWRESOLV /etc/resolv.conf 59 $verbose && echo "################################" 60 $verbose && echo "## BEGIN new /etc/resolv.conf ##" 61 $verbose && echo "################################" 62 $verbose && cat $NEWRESOLV 63 $verbose && echo "################################" 64 $verbose && echo "## END new /etc/resolv.conf ##" 65 $verbose && echo "################################" 66 cp $NEWRESOLV /etc/resolv.conf || exit 1 46 67 fi 47 48 # Cleanup before going home49 rm -Rf $TDIR
Note:
See TracChangeset
for help on using the changeset viewer.