Skip to content

Instantly share code, notes, and snippets.

@keyakko
Last active Jan 30, 2020
Embed
What would you like to do?
dns_record_check
#!/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
@keyakko
Copy link
Author

keyakko commented Jan 29, 2020

Execution example.

%  ./d.zsh --record NS gmail.com
++++++++++ a.root-servers.net ++++++++++
com.                    172800  IN      NS      e.gtld-servers.net.
com.                    172800  IN      NS      b.gtld-servers.net.
com.                    172800  IN      NS      j.gtld-servers.net.
com.                    172800  IN      NS      m.gtld-servers.net.
com.                    172800  IN      NS      i.gtld-servers.net.
com.                    172800  IN      NS      f.gtld-servers.net.
com.                    172800  IN      NS      a.gtld-servers.net.
com.                    172800  IN      NS      g.gtld-servers.net.
com.                    172800  IN      NS      h.gtld-servers.net.
com.                    172800  IN      NS      l.gtld-servers.net.
com.                    172800  IN      NS      k.gtld-servers.net.
com.                    172800  IN      NS      c.gtld-servers.net.
com.                    172800  IN      NS      d.gtld-servers.net.
++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++ b.root-servers.net ++++++++++
com.                    172800  IN      NS      m.gtld-servers.net.
com.                    172800  IN      NS      c.gtld-servers.net.
com.                    172800  IN      NS      a.gtld-servers.net.
com.                    172800  IN      NS      g.gtld-servers.net.
com.                    172800  IN      NS      l.gtld-servers.net.
com.                    172800  IN      NS      f.gtld-servers.net.
com.                    172800  IN      NS      b.gtld-servers.net.
com.                    172800  IN      NS      j.gtld-servers.net.
com.                    172800  IN      NS      i.gtld-servers.net.
com.                    172800  IN      NS      k.gtld-servers.net.
com.                    172800  IN      NS      d.gtld-servers.net.
com.                    172800  IN      NS      h.gtld-servers.net.
com.                    172800  IN      NS      e.gtld-servers.net.
++++++++++++++++++++++++++++++++++++++++++++++++++
<omitted...>
++++++++++ h.gtld-servers.net. ++++++++++
gmail.com.              172800  IN      NS      ns2.google.com.
gmail.com.              172800  IN      NS      ns1.google.com.
gmail.com.              172800  IN      NS      ns3.google.com.
gmail.com.              172800  IN      NS      ns4.google.com.
++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++ l.gtld-servers.net. ++++++++++
gmail.com.              172800  IN      NS      ns2.google.com.
gmail.com.              172800  IN      NS      ns1.google.com.
gmail.com.              172800  IN      NS      ns3.google.com.
gmail.com.              172800  IN      NS      ns4.google.com.
++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++ k.gtld-servers.net. ++++++++++
gmail.com.              172800  IN      NS      ns2.google.com.
gmail.com.              172800  IN      NS      ns1.google.com.
gmail.com.              172800  IN      NS      ns3.google.com.
gmail.com.              172800  IN      NS      ns4.google.com.
++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++ c.gtld-servers.net. ++++++++++
gmail.com.              172800  IN      NS      ns2.google.com.
gmail.com.              172800  IN      NS      ns1.google.com.
gmail.com.              172800  IN      NS      ns3.google.com.
gmail.com.              172800  IN      NS      ns4.google.com.
++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++ d.gtld-servers.net. ++++++++++
gmail.com.              172800  IN      NS      ns2.google.com.
gmail.com.              172800  IN      NS      ns1.google.com.
gmail.com.              172800  IN      NS      ns3.google.com.
gmail.com.              172800  IN      NS      ns4.google.com.
++++++++++++++++++++++++++++++++++++++++++++++++++

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment