Created
December 4, 2016 23:18
-
-
Save lg/44c8c2656fb5c277ffed382d05ff10a7 to your computer and use it in GitHub Desktop.
Update router IP address on CloudFlare using their v4 API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)