Skip to content

Instantly share code, notes, and snippets.

@SijmenHuizenga
Last active July 17, 2020 07:50
Show Gist options
  • Save SijmenHuizenga/7f6383e8c581982c661edfb868bed3ef to your computer and use it in GitHub Desktop.
Save SijmenHuizenga/7f6383e8c581982c661edfb868bed3ef to your computer and use it in GitHub Desktop.
Dynamic IP with Cloudflare
# Add to `crontab -e`:
# @hourly /home/pi/updateip.sh > /home/pi/.ip-log.txt
# @reboot sleep 30 && /home/pi/updateip.sh > /home/pi/.ip-log.txt
IP=`curl -s http://api.ipify.org/`
if [ -f $HOME/.ip.txt ]; then
OLD_IP=`cat $HOME/.ip.txt`
else
echo "No file, need IP"
OLD_IP=""
fi
if [ "$IP" = "$OLD_IP" ]; then
echo "IP Unchanged"
exit 0
fi
echo "Updating ip to $IP"
RESPONSE=$(
curl -X PUT "https://api.cloudflare.com/client/v4/zones/ZONEID/dns_records/DNSRECORDID" \
-H "X-Auth-Email: AUTHEMAIL" \
-H "X-Auth-Key: APIKEY" \
-H "Content-Type: application/json" \
--data '{"type":"A","name":"DNSNAME","content":"'"$IP"'","ttl":1,"proxied":false}'
)
if [ "$RESPONSE" != "${RESPONSE%success*}" ]; then
echo "Updated succesfuly!"
echo $IP > $HOME/.ip.txt
exit
else
echo 'Something went wrong'
echo "Response: $RESPONSE"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment