Skip to content

Instantly share code, notes, and snippets.

@n-st
Last active October 9, 2018 16:10
Show Gist options
  • Save n-st/abf6cf7af6741bae8d3cc7d680d2bb21 to your computer and use it in GitHub Desktop.
Save n-st/abf6cf7af6741bae8d3cc7d680d2bb21 to your computer and use it in GitHub Desktop.
Quick DNS overview for one or multiple domains. Requires bash, GNU awk, coreutils, dig.
#!/usr/bin/env bash
ip_to_hostname ()
{
if [ -z "$1" ]
then
printf '%s\n' "unknown"
else
nc -w 1 "$1" 587 < /dev/null | head -n 1 | awk '{if ($0 == "") {print "none"} else if ($1 == "220" && $0 ~ "SMTP ") {print $2} else {print "unknown"}}'
fi
}
root_domain ()
{
printf '%s\n' "${1#www.}"
}
dns_lookup ()
{
dig +short -t "$2" "$1" | sort -n
}
ptr_lookup ()
{
dig +short -x "$1" | xargs | sed 's/ /+/g'
}
for domain in "$@"
do
printf '%s\n' "$domain"
{
recordtype="CNAME"
while read -r line
do
printf '%s | |%s\n' "$recordtype" "$line"
domain="$line"
done < <(dns_lookup "$domain" "CNAME")
recordtype="NS"
while read -r line
do
printf '%s | |%s\n' "$recordtype" "$line"
unset recordtype
done < <(dns_lookup "$domain" "NS")
if [ -n "$recordtype" ]
then
printf '%s | |%s\n' "$recordtype" "[none]"
fi
recordtype="A"
while read -r line
do
printf '%s | | |%s|ptr: %s|helo587: %s\n' "$recordtype" "$line" "$(ptr_lookup "$line")" "$(ip_to_hostname "$line")"
unset recordtype
done < <(dns_lookup "$domain" "A")
if [ -n "$recordtype" ]
then
printf '%s | |%s\n' "$recordtype" "[none]"
fi
recordtype="AAAA"
while read -r line
do
printf '%s | | |%s|ptr: %s|helo587: %s\n' "$recordtype" "$line" "$(ptr_lookup "$line")" "$(ip_to_hostname "$line")"
unset recordtype
done < <(dns_lookup "$domain" "AAAA")
if [ -n "$recordtype" ]
then
printf '%s | |%s\n' "$recordtype" "[none]"
fi
recordtype="MX"
dns_lookup "$domain" "MX" | while read -r line
do
prio="${line%% *}"
mx="${line##* }"
arecs=$(dns_lookup "$mx" "A")
aaaarecs=$(dns_lookup "$mx" "AAAA")
if [ -n "$arecs" -o -n "$aaaarecs" ]
then
for mxip in $arecs
do
printf '%s |%2s |%s |%s|ptr: %s|helo587: %s\n' "$recordtype" "$prio" "$mx" "$mxip" "$(ptr_lookup "$mxip")" "$(ip_to_hostname "$mxip")"
unset recordtype prio mx
done
for mxip in $aaaarecs
do
printf '%s |%2s |%s |%s|ptr: %s|helo587: %s\n' "$recordtype" "$prio" "$mx" "$mxip" "$(ptr_lookup "$mxip")" "$(ip_to_hostname "$mxip")"
unset recordtype prio mx
done
else
printf '%s |%2s |%s |%s\n' "$recordtype" "$prio" "$mx" "no A or AAAA record"
fi
done
if [ -n "$recordtype" ]
then
printf '%s | |%s\n' "$recordtype" "[none]"
fi
} \
| column -t -s '|' \
| sed 's/^/ /'
recordtype="TXT"
while read -r line
do
printf ' TXT %s\n' "$line"
unset recordtype
done < <(dns_lookup "$domain" "TXT")
if [ -n "$recordtype" ]
then
printf ' TXT %s\n' "[none]"
fi
printf '\n'
done
% dnsinfo gmail.com example.com wiki.haskell.org
gmail.com
NS ns1.google.com.
ns2.google.com.
ns3.google.com.
ns4.google.com.
A 216.58.205.229 ptr: fra15s24-in-f229.1e100.net.+fra15s24-in-f5.1e100.net. helo587:
AAAA 2a00:1450:4001:824::2005 ptr: fra16s24-in-x05.1e100.net. helo587:
MX 5 gmail-smtp-in.l.google.com. 74.125.133.26 ptr: wo-in-f26.1e100.net. helo587:
2a00:1450:400c:c07::1a ptr: wo-in-x1a.1e100.net. helo587:
10 alt1.gmail-smtp-in.l.google.com. 64.233.164.26 ptr: lf-in-f26.1e100.net. helo587:
2a00:1450:4010:c07::1b ptr: lf-in-x1b.1e100.net. helo587:
20 alt2.gmail-smtp-in.l.google.com. 74.125.68.27 ptr: sc-in-f27.1e100.net. helo587:
2404:6800:4003:c02::1b ptr: sc-in-x1b.1e100.net. helo587:
30 alt3.gmail-smtp-in.l.google.com. 108.177.125.26 ptr: helo587:
2404:6800:4008:c01::1a ptr: helo587:
40 alt4.gmail-smtp-in.l.google.com. 74.125.195.26 ptr: wj-in-f26.1e100.net. helo587:
2607:f8b0:400e:c09::1b ptr: helo587:
MX [none]
TXT "v=spf1 redirect=_spf.google.com"
example.com
NS a.iana-servers.net.
b.iana-servers.net.
A 93.184.216.34 ptr: helo587:
AAAA 2606:2800:220:1:248:1893:25c8:1946 ptr: helo587:
MX [none]
TXT "$Id: example.com 4415 2015-08-24 20:12:23Z davids $"
TXT "v=spf1 -all"
wiki.haskell.org
CNAME dualstack.j.shared.global.fastly.net.
NS [none]
A 151.101.1.175 ptr: helo587:
151.101.129.175 ptr: helo587:
151.101.193.175 ptr: helo587:
151.101.65.175 ptr: helo587:
AAAA 2a04:4e42:200::431 ptr: helo587:
2a04:4e42:400::431 ptr: helo587:
2a04:4e42::431 ptr: helo587:
2a04:4e42:600::431 ptr: helo587:
MX [none]
TXT dualstack.j.shared.global.fastly.net.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment