Skip to content

Instantly share code, notes, and snippets.

@fbatta
Last active November 3, 2022 16:02
Show Gist options
  • Save fbatta/506920f439593000b60ae55e8c255549 to your computer and use it in GitHub Desktop.
Save fbatta/506920f439593000b60ae55e8c255549 to your computer and use it in GitHub Desktop.
Use cloudflare as a dynamic dns with your own domain name (bash script)
#!/bin/bash
## I use this script in a crontab that runs every couple of hours
## Get public IPv4 address with ipify API
PUBLIC_IP=$(curl --request GET --url https://api.ipify.org/)
## Replace auth key with your own cloudflare API key
AUTH_KEY=...
## Replace record name with the name of the (sub)domain you want to assign to the dynamic IP
RECORD_NAME=subdomain.example.com
TTL=120
## Replace with the id of the zone you want to change. Can be found in the cloudflare dashboard
ZONE_ID=...
## Replace with the id of the dns record corresponding to RECORD_NAME. Can be found by sending GET request to https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records
RECORD_ID=...
## PUT request to cloudflare
curl --request PUT \
--url https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID \
--header "authorization: Bearer $AUTH_KEY" \
--header 'content-type: application/json' \
--data "{
\"type\": \"A\",
\"name\": \"$RECORD_NAME\",
\"content\": \"$PUBLIC_IP\",
\"ttl\": \"$TTL\"
}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment