Skip to content

Instantly share code, notes, and snippets.

@lg
Created December 4, 2016 23:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save lg/44c8c2656fb5c277ffed382d05ff10a7 to your computer and use it in GitHub Desktop.
Save lg/44c8c2656fb5c277ffed382d05ff10a7 to your computer and use it in GitHub Desktop.
Update router IP address on CloudFlare using their v4 API
#!/bin/sh
#
# cloudflare v4 ip updater for edgerouter
#
# this uses the v4 api which ddclient doesn't support directly. though v1 api of cloudflare is still active, we've disabled it (for SAML support), so v4 is mandatory.
# updating the ip of a hostname is a 3 step process:
#
# 1. get the list of zones:
# curl -X GET "https://api.cloudflare.com/client/v4/zones" -H 'X-Auth-Email: ***CLOUDFLARE-EMAIL***' -H 'X-Auth-Key: ***CLOUDFLARE-TOKEN***' -H 'Content-Type: application/json'
# 2. get the DNS records in a zone (by the id returned above)
# curl -X GET "https://api.cloudflare.com/client/v4/zones/***ZONE-ID***/dns_records" -H 'X-Auth-Email: ***CLOUDFLARE-EMAIL***' -H 'X-Auth-Key: ***CLOUDFLARE-TOKEN***' -H 'Content-Type: application/json'
# 3. update the desired record (by the ids returned above):
EMAIL=''
TOKEN=''
ZONE_ID=''
DNS_RECORD_ID=''
DNS_RECORD_HOST=''
DNS_RECORD_TYPE='A'
DNS_RECORD_TTL=1
NEW_IP=$(curl 'http://v4.ifconfig.co')
curl -X PUT "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$DNS_RECORD_ID" -H "X-Auth-Email: $EMAIL" -H "X-Auth-Key: $TOKEN" -H 'Content-Type: application/json' --data "{\"type\":\"$DNS_RECORD_TYPE\",\"name\":\"$DNS_RECORD_HOST\",\"content\":\"$NEW_IP\",\"ttl\":$DNS_RECORD_TTL,\"proxied\":false}"
@randyramig
Copy link

Thanks for this! Note that they have updated to API tokens and deprecated the API keys per https://developers.cloudflare.com/api/tokens/create/
(just putting this here for folks who might hit that issue)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment