Skip to content

Instantly share code, notes, and snippets.

@ukn
Created January 27, 2020 20:56
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 ukn/4f2658fed9484d8b87ee34cd360d4580 to your computer and use it in GitHub Desktop.
Save ukn/4f2658fed9484d8b87ee34cd360d4580 to your computer and use it in GitHub Desktop.
Update Cloudflare A record when your IP changes
#!/bin/bash
# Updates Cloudflare dns record
# when your home IP changes
# Put in in cron with desired frequency
# but don't abuse WTF service (they throttle anyway)
set -e
API_TOKEN=YOUR_API_TOKEN_HERE
ZONE=$1
DNS_ID=$2
DNS_NAME=$3
if [[ $# -lt 3 ]]; then
echo "Not enough arguments"
exit 1
fi
getip() {
IP=`curl -sL https://wtfismyip.com/text`
OLDIP=`cat ./${DNS_NAME}-old.ip`
}
logger() {
echo `date +%Y:%H:%M:%S` $1 >> ./${DNS_NAME}.log
}
getip
if [ "$IP" ]; then
if [ "$OLDIP" != "$IP" ]; then
logger "Old IP: $OLDIP => New IP: $IP"
curl --silent -X PUT "https://api.cloudflare.com/client/v4/zones/$ZONE/dns_records/$DNS_ID" \
-H "Authorization: Bearer $API_TOKEN" \
-H "Content-Type: application/json" \
--data "{\"type\":\"A\",\"name\":\"${DNS_NAME}\",\"content\":\"${IP}\",\"proxied\":true}" \
&& echo -n "$IP" > ./${DNS_NAME}-old.ip && logger "IP updated to $IP" \
|| logger "Couldn't update IP"
fi
else
logger "Could not get IP from wtf"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment