Updated nscheck to be more resilient. In some cases, nscheck would incorrectly believe that the parent TLD was the root zone, ie it would assume that maff.com is a subdomain and "com" itself is the root zone

This commit is contained in:
Matthew Connelly 2013-08-03 22:21:53 +01:00
parent 609b2d628b
commit 6221842027
1 changed files with 25 additions and 41 deletions

66
nscheck
View File

@ -1,35 +1,27 @@
#!/usr/bin/env bash
#!/usr/local/bin/bash
#nscheck.sh - Simple script to do fairly good DNS diagnostics, to determine: Failing/slow nameservers, result inconsistency and so on.
# Copyright (c) 2012, Matthew Connelly
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
# - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
# - Neither the name of the software nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#TODO
# - Handle multiple results for single record names (DNS round-robin)
# - Use getopt for better commandline arg parsing
#Configurables
TIMEOUT=1
#Binary locations
DIG_BIN="$(command -v dig)"
if [ $? -ne 0 -o -z "$DIG_BIN" ]; then
echo "Error: 'dig' cannot be found. Please ensure you have the relevant package for 'dig' installed."
exit 1
fi
#Functions
function usage () {
echo "nscheck 0.1: Bash script to do DNS diagnosis.
NOTE: This script will not, at present, function properly for DNS queries which result in more than one record of the same type (ie, DNS round robins)
Usage: nscheck [-4|-6] DNS.ENTITY
nscheck -m: Show machine-parseable output (keypairs indicating nameserver responsiveness)
nscheck -h: Show this usage.
nscheck -4: Use IPv4 [Default].
nscheck -6: Use IPv6."
}
DIG_BIN="/usr/bin/dig"
#Internal variables
DIG_RECORD_TYPE="A"
HUMAN_READABLE=1
ZONE=""
if [ "$1" == "-6" ]; then
DIG_RECORD_TYPE="AAAA"
@ -37,33 +29,33 @@ if [ "$1" == "-6" ]; then
elif [ "$1" == "-4" ]; then
ZONE=$2
elif [ "$1" == "-h" ]; then
usage
echo "nscheck 0.1: Bash script to do DNS diagnosis.
NOTE: This script will not, at present, function properly for DNS queries which result in more than one record of the same type (ie, DNS round robins)
Usage: nscheck [-4|-6] DNS.ENTITY
nscheck -h: Show this usage.
nscheck -4: Use IPv4 [Default].
nscheck -6: Use IPv6."
exit 0
elif [ "$1" == "-m" ]; then
HUMAN_READABLE=0
ZONE=$2
else
ZONE=$1
fi
if [ -z "$ZONE" ]; then
usage
exit 1
fi
DOMAIN=$ZONE
MACHINE_SEPARATOR=";"
IPS_REPORTED=""
NS_REPORTED=""
FAILED_NS=""
IPS_CHECKED=""
SUCCESS_OUT="NS | NS-IP | NS-REVERSE | IP | REVERSE"
FAIL_OUT="NS | NS-IP | NS-REVERSE"
MACHINE_OUT=""
#Because this is a DNS debugging script, we don't rely on a domain's nameservers themselves for a list of nameservers. Instead, we query root and TLD nameservers.
#We could in future use WHOIS data to get nameservers.
#First we make sure we're querying the nameservers for the actual domain and not a subdomain. Also; remove trailing dot.
OUT=$($DIG_BIN $ZONE SOA|grep "^;; AUTH" -A1|tail -n1|awk '{print $1}'|sed -e 's/.$//g')
OUT=$($DIG_BIN $ZONE SOA|grep "^;; ANSWER" -A1|tail -n1|awk '{print $1}'|sed -e 's/.$//g')
if [ -z "$OUT" ]; then
#Try again.
OUT=$($DIG_BIN $ZONE SOA|grep "^;; AUTH" -A1|grep "SOA"|tail -n1|awk '{print $1}'|sed -e 's/.$//g')
fi
if [ -z "$OUT" ]; then
#Try again.
OUT=$($DIG_BIN $ZONE SOA|grep "^;; ANSWER" -A6|grep "SOA"|tail -n1|awk '{print $1}'|sed -e 's/.$//g')
@ -75,7 +67,7 @@ for ns in $($DIG_BIN +short $ZONE NS); do
if [[ $NS_REPORTED != *"$ns"* ]]; then
NS_REPORTED="$NS_REPORTED$ns "
fi
for ip in $($DIG_BIN +short $ns); do
for ip in $($DIG_BIN +short $ns $DIG_RECORD_TYPE); do
if [[ $IPS_CHECKED != *"$ip"* ]]; then
IPS_CHECKED="$IPS_CHECKED $ip"
#We need to use tail here in order to stop dig from being fucking retarded and providing a cname as well as the result
@ -101,21 +93,13 @@ for ns in $($DIG_BIN +short $ZONE NS); do
fi
SUCCESS_OUT="$SUCCESS_OUT
$ns | $ip | $REVERSE | $OUT | $REVOUT"
MACHINE_OUT="$MACHINE_OUT$ip${MACHINE_SEPARATOR}1
"
else
FAIL_OUT="$FAIL_OUT
$ns | $ip | $REVERSE"
MACHINE_OUT="$MACHINE_OUT$ip${MACHINE_SEPARATOR}0
"
fi
fi
done
done
if [ $HUMAN_READABLE -eq 0 ]; then
echo -n "$MACHINE_OUT"|sort -n
exit
fi
if [[ $ZONE != $DOMAIN ]]; then
echo "Found root zone for $DOMAIN: $ZONE"
fi