Skip to content

Instantly share code, notes, and snippets.

@xiamaz
Created December 17, 2019 18:06
Show Gist options
  • Save xiamaz/cfff64e117ee9a707ef03677aaf79148 to your computer and use it in GitHub Desktop.
Save xiamaz/cfff64e117ee9a707ef03677aaf79148 to your computer and use it in GitHub Desktop.
Cloudflare dynamic dns (dyndns) update script
[Unit]
Description=Update Cloudflare IP periodically.
[Service]
Type=oneshot
ExecStart=/usr/local/bin/cloudflare.sh
#!/bin/sh
# Get Token with DNS EDIT Permission for needed zone. This can also be limited to the domain.
TOKEN="CLOUDFLARE API TOKEN"
ZONE="ZONE ID"
RECORD="RECORD ID"
DOMAIN="example.com"
# Get current IP
IPADDR=$(curl -s -4 ifconfig.me)
echo "Updating IP to $IPADDR"
## Use the commented part to get initial RECORD ID after creating the needed A entry in cloudflare.
# curl -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE/dns_records?type=A&name=$DOMAIN&page=1&per_page=20&order=type&direction=desc&match=all" \
# -H "Authorization: Bearer $TOKEN" \
# -H "Content-Type: application/json"
curl -X PUT "https://api.cloudflare.com/client/v4/zones/$ZONE/dns_records/$RECORD" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
--data "{\"type\":\"A\",\"name\":\"$DOMAIN\",\"content\":\"$IPADDR\",\"ttl\":1,\"proxied\":false}"
[Unit]
Description=Update cloudflare IP periodically.
[Timer]
OnActiveSec=1min
OnUnitActiveSec=15min
[Install]
WantedBy=timers.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment