Skip to content

Instantly share code, notes, and snippets.

@Ham5ter
Last active November 23, 2016 13:26
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 Ham5ter/fe34f0f63aefa7328cfb20d95847e35d to your computer and use it in GitHub Desktop.
Save Ham5ter/fe34f0f63aefa7328cfb20d95847e35d to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Very simple Script that checks a configured DNS Record,
# If it changed since the Last time this script was run,
# it alerts a specified Mail Recipient
# http://tecadmin.net/ways-to-send-email-from-linux-command-line/
#
# Config Stuff
DOMAIN="example.com"
DNS_SERVER="8.8.8.8"
MAIL_RECIPIENT="alert@example.com"
MAIL_SUBJECT="Mail Alert DynDNS"
# Dont touch stuf after this comment, if you dont know what you are doing!
OLDIP_FILE="OLDIP.txt"
CURRENTIP=$(dig +noall +answer +nocomments ${DOMAIN} @${DNS_SERVER} | awk '{print $5}')
OLDIP=$(cat "${OLDIP_FILE}")
echo "${CURRENTIP}" > "${OLDIP_FILE}"
if [ "${OLDIP}" == "${CURRENTIP}" ]; then
exit 0
else
echo "ERROR=DNS RECORD CHANGED!"
echo "DOMAIN=\"${DOMAIN}\""
echo "DNS_SERVER=\"${DNS_SERVER}\""
echo "OLD_IP=\"${OLDIP}\""
echo "NEW_IP=\"${CURRENTIP}\""
# Send Mail
echo "DNS RECORD for \"${DOMAIN}\" changed from \"${OLDIP}\" to \"${CURRENTIP}\"" | mail -s ${MAIL_SUBJECT} ${MAIL_RECIPIENT}
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment