Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 122 lines (106 sloc) 3.37 KB
#!/bin/bash
set -e -f -u -o pipefail
debug_host_output() {
local host_output_file="$1"
sed -e 's/^/->/' -e 's/$/<-/' "${host_output_file}"
}
find_address() {
local atype="$1" pattern="$2" host_output_file="$3"
readonly atype pattern host_output_file hostname
if ! grep -q "${pattern}" "${host_output_file}"; then
echo "Bad ${atype} address: pattern '${pattern}' not found" >&2
debug_host_output "${host_output_file}" >&2
return 1
fi
}
maybe_remove_output_file() {
local retval="$1" host_output_file="$2"
if [ "${retval}" -eq 0 ]; then
rm "${host_output_file}"
else
echo "host output in ${host_output_file}"
fi
}
check_dns_for_host() {
local hostname="$1" ipv4_address="$2" ipv6_address="$3"
readonly hostname ipv4_address ipv6_address
# www.johntobin.ie has address 88.99.86.190
# www.johntobin.ie has IPv6 address 2a01:4f8:c17:7156::2
local host_output_file pattern retval=0
host_output_file="$(mktemp /tmp/check-dns.XXXXXXXXXXXX)"
readonly host_output_file
host "${hostname}" > "${host_output_file}"
pattern="^${hostname} has address ${ipv4_address}\$"
if ! find_address "ipv4" "${pattern}" "${host_output_file}"; then
retval=1
fi
pattern="^${hostname} has IPv6 address ${ipv6_address}\$"
if ! find_address "ipv6" "${pattern}" "${host_output_file}"; then
retval=1
fi
maybe_remove_output_file "${retval}" "${host_output_file}"
return "${retval}"
}
check_mx_for_host() {
local hostname="$1"
readonly hostname
shift
# johntobin.ie mail is handled by 1 aspmx.l.google.com.
# johntobin.ie mail is handled by 10 aspmx2.googlemail.com.
# johntobin.ie mail is handled by 10 aspmx3.googlemail.com.
# johntobin.ie mail is handled by 5 alt1.aspmx.l.google.com.
# johntobin.ie mail is handled by 5 alt2.aspmx.l.google.com.
local host_output_file
host_output_file="$(mktemp /tmp/check-dns.XXXXXXXXXXXX)"
readonly host_output_file
host -t MX "${hostname}" > "${host_output_file}"
local record pattern retval=0
for record in "$@"; do
pattern="^${hostname} mail is handled by ${record}\$"
if ! find_address "MX" "${pattern}" "${host_output_file}"; then
retval=1
fi
done
maybe_remove_output_file "${retval}" "${host_output_file}"
return "${retval}"
}
main() {
local hostname retval=0
local ipv4_address="88.99.86.190" ipv6_address="2a01:4f8:c17:7156::2"
local mx_addresses=("1 aspmx.l.google.com." "10 aspmx2.googlemail.com." \
"10 aspmx3.googlemail.com." "5 alt1.aspmx.l.google.com." \
"5 alt2.aspmx.l.google.com.")
readonly ipv4_address ipv6_address mx_addresses
for hostname in "$@"; do
if ! check_dns_for_host "${hostname}" \
"${ipv4_address}" "${ipv6_address}"; then
retval=1
fi
if echo "${hostname}" | grep -q '\..*\.'; then
# This is a subdomain, e.g. dev.arianetobin.ie.
# Don't check www.subdomain or MX records for it.
continue
fi
if ! check_dns_for_host "www.${hostname}" \
"${ipv4_address}" "${ipv6_address}"; then
retval=1
fi
if ! check_mx_for_host "${hostname}" "${mx_addresses[@]}"; then
retval=1
fi
done
return "${retval}"
}
if [ $# -eq 0 ]; then
main \
"ariane.ie" \
"arianetobin.com" \
"arianetobin.ie" \
"dev.arianetobin.ie" \
"johntobin.ie" \
"metalatplay.com" \
"metalatwork.com" \
"nakedmetalsmith.com"
else
main "$@"
fi
You can’t perform that action at this time.