Skip to content

Instantly share code, notes, and snippets.

@D3strukt0r
Created May 7, 2020 18:34
Show Gist options
  • Save D3strukt0r/82c517786b2407c2e916640bd874b532 to your computer and use it in GitHub Desktop.
Save D3strukt0r/82c517786b2407c2e916640bd874b532 to your computer and use it in GitHub Desktop.
Automatically update your CloudFlare DNS record to the IP, Dynamic DNS for Cloudflare
#!/usr/bin/env bash
# The zone ID of the domain
CF_ZONE_ID=
# The email of the user and the global api key
CF_USER_EMAIL=
CF_GLOBAL_API_KEY=
# A generated API key with the permission "Zone.DNS"
CF_API_KEY=
# The hostname to update
CF_HOSTNAME=
# GET ZONE ID
## USAGE WITH GLOBAL API KEY
curl -X GET "https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records?type=A&name=$CF_HOSTNAME&match=all" \
-H "X-Auth-Email: $CF_USER_EMAIL" \
-H "X-Auth-Key: $GLOBAL_API" \
-H "Content-Type: application/json"
## USAGE WITH API KEY
curl -X GET "https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records?type=A&name=$CF_HOSTNAME&match=all" \
-H "Authorization: Bearer $API" \
-H "Content-Type: application/json"
json["success"] = true
CF_DNS_ID=json["result"][0]["id"]
# GET CURRENT IPv4
IPV4=$(curl https://api.ipify.org/)
# UPDATE IP ON CLOUDFLARE
## USAGE WITH GLOBAL API KEY
curl -X PATCH "https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records/$CF_DNS_ID" \
-H "X-Auth-Email: $CF_USER_EMAIL" \
-H "X-Auth-Key: $GLOBAL_API" \
-H "Content-Type: application/json" \
--data '{"content":"'"$IPV4"'"}'
## USAGE WITH API KEY
curl -X PATCH "https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records/$CF_DNS_ID" \
-H "Authorization: Bearer $API" \
-H "Content-Type: application/json" \
--data '{"content":"'"$IPV4"'"}'
json["success"] = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment