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
|
---|
10 | if [ "$1" = "cron" ]; then
|
---|
11 | sleep `expr $$ % 30`
|
---|
12 | fi
|
---|
13 |
|
---|
14 | TAG='^# TAG: DYNAMIC LIST$'
|
---|
15 |
|
---|
16 | TDIR=`mktemp -d -t $(basename $0)`
|
---|
17 | DYNLIST=$TDIR/dynlist
|
---|
18 | RESULTLIST=$TDIR/resultlist
|
---|
19 | NEWRESOLV=$TDIR/resolv.conf.new
|
---|
20 |
|
---|
21 | # Get enabled DYNAMIC LIST nameservers
|
---|
22 | sed "1,/$TAG/d" /etc/resolv.conf > $DYNLIST
|
---|
23 | NAMESERVERS=`awk '/^nameserver/ {print $2}' $DYNLIST`
|
---|
24 |
|
---|
25 | # Only do something if we have dynamic nameservers
|
---|
26 | if [ -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
|
---|
46 | fi
|
---|
47 |
|
---|
48 | # Cleanup before going home
|
---|
49 | rm -Rf $TDIR
|
---|