Skip to content

Instantly share code, notes, and snippets.

@awaxa
Created November 15, 2013 19:08
Show Gist options
  • Save awaxa/7489850 to your computer and use it in GitHub Desktop.
Save awaxa/7489850 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
[ "$1" == "-d" ] && set -vx # debug
conf=$HOME/.nsupdate-aws.conf
check="http://ifconfig.me/ip"
if [ -f $conf ]
then
. $conf
elif [ -a $conf ]
then
echo "exception: $conf exists and is not a file"
exit 1
else
response=""
cli53=/usr/local/bin/cli53
AWS_ACCESS_KEY_ID=""
AWS_SECRET_ACCESS_KEY=""
zone="awaxa.net"
record="$(hostname)"
ttl=300
echo "Please configure nsupdate-aws.sh"
read -p "cli53 path (default=$cli53): " response
[ ! $response = "" ] && cli53=$response
read -p "AWS_ACCESS_KEY_ID (default=$AWS_ACCESS_KEY_ID): " AWS_ACCESS_KEY_ID
read -p "AWS_SECRET_ACCESS_KEY (default=$AWS_SECRET_ACCESS_KEY): " AWS_SECRET_ACCESS_KEY
read -p "zone (default=$zone): " response
[ ! $response = "" ] && zone=$response
read -p "record (default=$record): " response
[ ! $response = "" ] && record=$response
>$conf echo "#!/usr/bin/env bash
export cli53=$cli53
export AWS_ACCESS_KEY_ID=\"$AWS_ACCESS_KEY_ID\"
export AWS_SECRET_ACCESS_KEY=\"$AWS_SECRET_ACCESS_KEY\"
export zone=\"$zone\"
export record=\"$record\"
export ttl=$ttl"
echo "nsupdate-aws is now configured"
exit 0
fi
IPREGEX='[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}'
myip=$(curl -s $check)
ip=$(echo $myip | grep -E $IPREGEX)
valid=$?
if [ $valid -ne 0 ] ; then
echo $0 $(date) invalid response from $check
exit 0
fi
dig=$(dig +short +trace $record.$zone. | grep ^A | cut -f2 -d' ')
echo $0 $(date) curl=$ip dig=$ip
update="$cli53 rrcreate $zone $record A $ip --ttl $ttl --replace"
if [ "$1" == "-f" ] || [ "$ip" != "$dig" ] ; then
echo $0 $(date) $update
$update
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment