Skip to content

Instantly share code, notes, and snippets.

@augustohp
Created July 26, 2019 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save augustohp/acb1b4cf3776484dac529d44919b29ac to your computer and use it in GitHub Desktop.
Save augustohp/acb1b4cf3776484dac529d44919b29ac to your computer and use it in GitHub Desktop.
Checks if the name servers of a domain are changed.
#!/usr/bin/env sh
#
# Checks if the nameservers of a domain changed.
#
# Example:
# $ dns-ns-is-not example.org auto.dns.br \
# && send-to-room.sh "DNS change applied! \o/"
DIG=$(command -v dig)
DNS="8.8.8.8"
DOMAIN=${1:-""}
PATTERN=${2:-""}
WAIT_SECONDS=60
if [ ! -z "$DEBUG" ]
then
set -x
fi
if [ -z "$DIG" ]
then
echo "Error: dig is not installed." 2>&1
exit 2
fi
display_help()
{
cat <<-EOT
Usage: $APP_NAME <domain> <pattern>
Will check the name servers of a domain, every 10 seconds, so
the pattern is not found. Exits with 0 (zero) once the pattern
is not found anymore.
EOT
}
remove_leading_spaces()
{
sed 's/^[ ]*//g'
}
count_lines()
{
wc -l
}
check_pattern_in_domain()
{
domain="$1"
pattern="$2"
now=$(date)
lines_found=$($DIG @"$DNS" +short NS "${DOMAIN}" | grep "$PATTERN" | count_lines | remove_leading_spaces)
if [ $lines_found -gt 0 ]
then
sleep $WAIT_SECONDS
echo "[${now}] Pattern found, retrying in ${WAIT_SECONDS} seconds..."
check_pattern_in_domain "${domain}" "${pattern}"
else
return 0
fi
}
if [ -z "$DOMAIN" ] || [ -z "$PATTERN" ]
then
display_help
exit 1
fi
check_pattern_in_domain "${DOMAIN}" "${PATTERN}"
exit 0
# vim: ft=sh sw=4 ts=4 ai noet:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment