Ignore:
Timestamp:
May 2, 2012, 8:00:55 PM (13 years ago)
Author:
rick
Message:

Make it look good and have it represent its output into /etc/resolv.conf to
avoid confusion why the sorting/shuffling is done.

Related-To: beheer#207

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/releng-9.0/nanobsd/files/tools/nameserver-shuffle

    r10643 r10645  
    1 #!/bin/sh
     1#!/bin/sh -
    22#
    33# Shuffle nameservers listed based on the query times, if enabled in
    44# resolv.conf. Bare in mind the special syntax.
    55#
    6 # Rick van der Zwet <info@rickvanderzwet.nl>
     6# Rick van der Zwet <rick@wirelessleiden.nl>
    77#
    88
    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
     10verbose=true
    1011if [ "$1" = "cron" ]; then
     12  verbose=false
    1113  sleep `expr $$ % 30`
    1214fi
    1315
    14 TAG='^# TAG: DYNAMIC LIST$'
     16TAG='^# START DYNAMIC LIST - updated by /tools/nameserver-shuffle$'
     17$verbose && echo "# Searching in /etc/resolv.conf for tag '$TAG'"
    1518
    1619TDIR=`mktemp -d -t $(basename $0)`
     20# Cleanup before going home
     21trap "rm -Rf $TDIR; exit 1" 1 2 3 15
     22trap "rm -Rf $TDIR; exit 0" 0
     23
    1724DYNLIST=$TDIR/dynlist
    1825RESULTLIST=$TDIR/resultlist
     
    2027
    2128# Get enabled DYNAMIC LIST nameservers
    22 sed "1,/$TAG/d" /etc/resolv.conf > $DYNLIST
    23 NAMESERVERS=`awk '/^nameserver/ {print $2}' $DYNLIST`
     29sed "1,\+$TAG+d" /etc/resolv.conf > $DYNLIST || exit 1
     30NAMESERVERS=`awk '/^nameserver/ {print $2}' $DYNLIST` || exit 1
    2431
    2532# Only do something if we have dynamic nameservers
     33$verbose && echo "# Processing" `echo $NAMESERVERS | wc -w` nameservers
    2634if [ -n "$NAMESERVERS" ]; then
    2735  # Find query times
    2836  for NAMESERVER in $NAMESERVERS; do
     37    $verbose && printf "## Testing nameserver %-16s query time: " $NAMESERVER
    2938    # Strict checking to avoid buggy links to return decent results.
    3039    QUERY_TIME=`dig +time=1 +tries=1 SOA wleiden.net @$NAMESERVER | awk '/Query time:/ {print $4}'`
    3140    # Failed to complete succesfully
    32     [ -z "$QUERY_TIME" ] && QUERY_TIME=9999
     41    [ -z "$QUERY_TIME" ] && QUERY_TIME="9999"
     42    $verbose && echo "$QUERY_TIME"
    3343    echo "$QUERY_TIME $NAMESERVER" >> $RESULTLIST
    3444  done
    3545 
    3646  # Get the header part
    37   sed -n "1,/$TAG/p" /etc/resolv.conf > $NEWRESOLV
     47  sed -n "1,\+$TAG+p" /etc/resolv.conf > $NEWRESOLV || exit 1
    3848 
    3949  # Output sorted list
    4050  NAMESERVERS=`sort -n $RESULTLIST | awk '{print $2}'`
    4151  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
    4358  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
    4667fi
    47 
    48 # Cleanup before going home
    49 rm -Rf $TDIR
Note: See TracChangeset for help on using the changeset viewer.