source: hybrid/branches/releng-9.0/nanobsd/files/tools/nameserver-shuffle@ 10643

Last change on this file since 10643 was 10643, checked in by rick, 13 years ago

Shuffle the dynamic entries in /etc/resolv.conf based on best Query time.

Related-To: beheer#207

  • Property svn:executable set to *
File size: 1.4 KB
Line 
1#!/bin/sh
2#
3# Shuffle nameservers listed based on the query times, if enabled in
4# resolv.conf. Bare in mind the special syntax.
5#
6# Rick van der Zwet <info@rickvanderzwet.nl>
7#
8
9# ``Random'' sleep between 0 and 30 minutes to avoid ``slamming'' on the DNS door
10if [ "$1" = "cron" ]; then
11 sleep `expr $$ % 30`
12fi
13
14TAG='^# TAG: DYNAMIC LIST$'
15
16TDIR=`mktemp -d -t $(basename $0)`
17DYNLIST=$TDIR/dynlist
18RESULTLIST=$TDIR/resultlist
19NEWRESOLV=$TDIR/resolv.conf.new
20
21# Get enabled DYNAMIC LIST nameservers
22sed "1,/$TAG/d" /etc/resolv.conf > $DYNLIST
23NAMESERVERS=`awk '/^nameserver/ {print $2}' $DYNLIST`
24
25# Only do something if we have dynamic nameservers
26if [ -n "$NAMESERVERS" ]; then
27 # Find query times
28 for NAMESERVER in $NAMESERVERS; do
29 # Strict checking to avoid buggy links to return decent results.
30 QUERY_TIME=`dig +time=1 +tries=1 SOA wleiden.net @$NAMESERVER | awk '/Query time:/ {print $4}'`
31 # Failed to complete succesfully
32 [ -z "$QUERY_TIME" ] && QUERY_TIME=9999
33 echo "$QUERY_TIME $NAMESERVER" >> $RESULTLIST
34 done
35
36 # Get the header part
37 sed -n "1,/$TAG/p" /etc/resolv.conf > $NEWRESOLV
38
39 # Output sorted list
40 NAMESERVERS=`sort -n $RESULTLIST | awk '{print $2}'`
41 for NAMESERVER in $NAMESERVERS; do
42 grep '\b'$NAMESERVER'\b' $DYNLIST >> $NEWRESOLV
43 done
44
45 cp $NEWRESOLV /etc/resolv.conf
46fi
47
48# Cleanup before going home
49rm -Rf $TDIR
Note: See TracBrowser for help on using the repository browser.