Skip to content

Instantly share code, notes, and snippets.

@joshlarsen
Created March 19, 2019 18:45
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 joshlarsen/02ddcb0052e62416d88659e5f782e006 to your computer and use it in GitHub Desktop.
Save joshlarsen/02ddcb0052e62416d88659e5f782e006 to your computer and use it in GitHub Desktop.
DNSimple dynamic IP update script
#!/bin/bash
#
# Update DNSimple DNS record if ISP IP changes
#
# Requirements:
# existing DNS record hosted with DNSimple
# curl in $PATH
#
# run from cron
#
# e.g.
# */5 * * * * /home/user/scripts/dns.sh 2&>1 > /dev/null
DNSIMPLE_ACCOUNT_ID=12345
DNSIMPLE_DOMAIN_NAME='example.org'
DNSIMPLE_RECORD_ID=12345678
DNSIMPLE_API_TOKEN='redacted'
IP_FILE='/home/user/scripts/IP'
LAST_IP=$(cat ${IP_FILE})
CURRENT_IP=$(curl -s ipinfo.io/ip)
JSON="{\"content\":\"${CURRENT_IP}\"}"
if [ "$LAST_IP" == "$CURRENT_IP" ]
then
# IP is the same, don't do anything
echo $LAST_IP
echo "IP hasn't changed"
else
# IP has changed, update DNSimple and re-write IP file
echo "IP has changed... updating IP"
curl -H "Authorization: Bearer ${DNSIMPLE_API_TOKEN}" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-X PATCH \
-d "${JSON}" \
https://api.dnsimple.com/v2/${DNSIMPLE_ACCOUNT_ID}/zones/${DNSIMPLE_DOMAIN_NAME}/records/${DNSIMPLE_RECORD_ID}
# update current IP address file
echo $CURRENT_IP > ${IP_FILE}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment