Skip to content

Instantly share code, notes, and snippets.

@sihil
Created December 19, 2016 15:47
Show Gist options
  • Save sihil/41a0d694d687e42ee1cf0e39678ace2d to your computer and use it in GitHub Desktop.
Save sihil/41a0d694d687e42ee1cf0e39678ace2d to your computer and use it in GitHub Desktop.
Script for gluing nsnotifyd and cli53 together
#!/bin/bash
set -e
# Script to update a zone - designed to be triggerd by nsnotifyd
PATH=/bin:/usr/bin:/usr/local/bin:$PATH
# Constants
DYN_TRANSFER_HOST=xfrout1.dynect.net
# Get command line parameters
ZONE=$1
NOTIFY_SERIAL=$2
SOURCE=$3
# check command line parameters
if [ -z "${ZONE}" -o -z "${NOTIFY_SERIAL}" ]; then
echo "Usage: $0 <zone> <serial> [<source>]" >&2
exit 1
fi
ZONEFILE=`mktemp /tmp/${ZONE}.zonefile.XXXXXXXXX`
dig AXFR ${ZONE} @${DYN_TRANSFER_HOST} > ${ZONEFILE} 2>&1
# check if the transfer was successful
if ! egrep "Transfer failed|connection timed out|Name or service not known|connection refused|network unreachable|host unreachable|end of file|communications error|couldn't get address" ${ZONEFILE} > /dev/null; then
#extract serial from ZONEFILE
SOA_SERIAL=$( cat ${ZONEFILE} | awk '{if ($4 == "SOA") print $7;}' | head -1 )
if [ "$SOA_SERIAL" -eq "$SOA_SERIAL" ] 2>/dev/null; then
SERIAL=$SOA_SERIAL
else
echo "SOA record not found in transferred zone, couldn't extract serial"
exit
fi
# append a serial number record for troubleshooting
echo -e "\n_serial._dyn.${ZONE}. IN TXT \"${SERIAL}\"" >> ${ZONEFILE}
# push into Route53
cli53 import --file ${ZONEFILE} --replace ${ZONE}
fi
rm ${ZONEFILE}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment