Last active
January 30, 2020 08:05
-
-
Save keyakko/d66ba38ebf67785de767dd270de60e40 to your computer and use it in GitHub Desktop.
dns_record_check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zsh | |
TARGET_DOMAIN_NAME= | |
TARGET_RECORD=A | |
ROOT_SERVERS=`echo {a..m}.root-servers.net` | |
local -A opthash | |
zparseopts -D -M -A opthash -- \ | |
h -help=h \ | |
-root: \ | |
-record: | |
if [[ -n "${opthash[(i)-h]}" ]]; then | |
echo "dns check" | |
echo $0 "[OPTION]..." "[TARGET_DOMAIN_NAME]" | |
echo | |
echo "[option]" | |
echo "--help show help" | |
echo "--root set DNS root-server" | |
echo "--record set target record type" | |
return 0 | |
fi | |
if [[ -z "$@" ]]; then | |
echo "target is empty." | |
return 1 | |
fi | |
TARGET_DOMAIN_NAME=`echo -n $@ | awk '{print $1}'` | |
if [[ -n "${opthash[(i)--root]}" ]]; then | |
ROOT_SERVERS=${opthash[--root]} | |
fi | |
if [[ -n "${opthash[(i)--record]}" ]]; then | |
TARGET_RECORD=${opthash[--record]} | |
fi | |
BASE_SEARCH_RESULT= | |
DNS_SERVERS=$ROOT_SERVERS | |
NEXT_TARGET_DNS_SERVER= | |
while true; do | |
BASE_SEARCH_RESULT= | |
NEXT_TARGET_DNS_SERVER= | |
# empty check | |
if [[ -z $DNS_SERVERS ]]; then break; fi | |
echo -n $DNS_SERVERS | \ | |
tr ' ' '\n' | \ | |
while read DNS_SV; do | |
SEARCH_RESULT=`dig @${DNS_SV} ${TARGET_DOMAIN_NAME} ${TARGET_RECORD} +norecurse | grep -E "[0-9].*IN.*(NS|${TARGET_RECORD})"` | |
# show result | |
echo "++++++++++" $DNS_SV "++++++++++" | |
echo $SEARCH_RESULT | |
echo "++++++++++++++++++++++++++++++++++++++++++++++++++" | |
if [[ -z $BASE_SEARCH_RESULT ]]; then | |
BASE_SEARCH_RESULT=$SEARCH_RESULT | |
echo $SEARCH_RESULT | grep $TARGET_DOMAIN_NAME 2>/dev/null | grep $TARGET_RECORD 2>&1 1>/dev/null | |
if [[ $? -ne 0 ]]; then | |
NEXT_TARGET_DNS_SERVER=`echo $SEARCH_RESULT | grep NS | awk '{print $5}' | tr '\n' ' '` | |
fi | |
else | |
echo $SEARCH_RESULT | \ | |
awk '{print $5}' | \ | |
while read SV_VAL; do | |
echo $BASE_SEARCH_RESULT | grep $SV_VAL 2>&1 1>/dev/null | |
if [[ $? -ne 0 ]]; then | |
echo "check failed!" "sv: " $DNS_SV ", value: "$SV_VAL | |
fi | |
done | |
fi | |
done | |
DNS_SERVERS=$NEXT_TARGET_DNS_SERVER | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Execution example.