Sort resolv.conf by fastest nameserver and remove not working ones

From Urandom
Jump to: navigation, search

/opt/dnschecker/check_dns.sh

#!/bin/bash
DNS_SERVERS=" 10.180.1.165 10.180.131.200 10.180.131.253 10.180.1.253"
if [ -f /opt/dnschecker/dns_server.env ]
then
source /opt/dnschecker/dns_server.env 
fi
logfile="/opt/dnschecker/check_dns.log"
TEST_HOST="wwtll006.fleet.zone"
TEST_RESULT="10.180.1.226"


  RESOLV="/opt/dnschecker/resolv.temp"
  cat /etc/resolv.conf | grep -v "nameserver" > $RESOLV
  for server in $DNS_SERVERS
  do
    NSL=`nslookup $TEST_HOST $server | grep $TEST_RESULT`
    if [ $? -eq 0 ]
    then
      TIME=`ping $server -i 0.1 -c 3 -q -n | tail -1 | cut -d"/" -f5`
      echo "$TIME $server"
    else
      echo "`date +%Y-%m-%d_%H:%M:%S` $server Failed to respond" >> $logfile
    fi
  done | sort | while read line
  do
    echo "nameserver `echo $line | cut -d' ' -f2`" >> $RESOLV
  done
  cat /etc/resolv.conf | grep nameserver | sort > $RESOLV.original
  cat $RESOLV | grep nameserver | sort > $RESOLV.new
  diff -q $RESOLV.new $RESOLV.original
  if [ $? -eq 1 ]
  then
    nlen=`cat $RESOLV.new | wc -l`
    olen=`cat $RESOLV.original | wc -l`
    if [ $nlen -eq 0 -o $nlen -ne $olen ]
    then 
    echo "`date +%Y-%m-%d_%H:%M:%S` Replacing resolv.conf with updated nameservers. $nlen rows new conf. $olen rows old conf. < - rows added, > - rows removed
`diff $RESOLV.new $RESOLV.original`" | mail -s "Nameserver update `hostname`" root | tee -a $logfile
    fi
    mv $RESOLV /etc/resolv.conf
fi  
Personal tools