Skip to content

Instantly share code, notes, and snippets.

@ShortyDev
Last active February 17, 2022 14:23
Show Gist options
  • Save ShortyDev/a6fc108087e4d408d54e4964808e990c to your computer and use it in GitHub Desktop.
Save ShortyDev/a6fc108087e4d408d54e4964808e990c to your computer and use it in GitHub Desktop.
Script to update your IP on a Cloudflare DNS entry
if [ ! -e address.txt ]; then
touch address.txt
echo "nothing" > address.txt
fi
read storedAddress < address.txt
dnsName=%%_CLOUDFLARE_DNS_NAME_%%
zoneId=%%_CLOUDFLARE_ZONE_ID_%%
dnsId=%%_CLOUDFLARE_DNS_ID_%%
token=%%_CLOUDFLARE_API_TOKEN_%%
apiUrl=https://api.cloudflare.com/client/v4/zones/$zoneId/dns_records/$dnsId
fetchedAddress=$(curl -s ifconfig.me/ip)
if [ $storedAddress != $fetchedAddress ]; then
echo $fetchedAddress > address.txt
echo "Changing IP..."
curl -X PUT $apiUrl -H "Content-Type: application/json" -H "Authorization: Bearer $token" --data-binary "{\"type\":\"A\",\"name\":\"$dnsName\",\"content\":\"$fetchedAddress\",\"ttl\":1,\"proxied\":false}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment