Created
March 19, 2019 18:45
-
-
Save joshlarsen/02ddcb0052e62416d88659e5f782e006 to your computer and use it in GitHub Desktop.
DNSimple dynamic IP update script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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