Last active
October 15, 2020 04:35
-
-
Save EdisonJwa/c7e208fa19de49005d61a2cef0e13489 to your computer and use it in GitHub Desktop.
CloudFlare DDNS Script
This file contains 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/bash | |
# Install curl and dig before using it | |
# Using this command to get your record id | |
# curl -X GET "https://api.cloudflare.com/client/v4/zones/[YOUR_ZONE_ID]/dns_records?type=[TYPE A (for ipv4)or AAAA (for ipv6)]&name=[DOMAIN]&match=all" \ | |
# -H "X-Auth-Email: [YOUR_EMAIL_ADDRESS]" \ | |
# -H "X-Auth-Key: [YOUR_API_KEY]" \ | |
# -H "Content-Type: application/json" | |
ipv4=$(curl -s ipv4.whatismyip.akamai.com) | |
ipv6=$(curl -s ipv6.whatismyip.akamai.com) | |
email=user@example.com | |
zone_id=xxxxx | |
api_key=xxxxx | |
v4record_id=xxxxx | |
v6record_id=xxxxx | |
domain=abc.example.com # Full domain address used for DDNS | |
ttl=120 | |
if [ "${ipv4}" = "$(dig ${domain} A +short)" ]; | |
then | |
echo 'IPv4 address is unchanged.' | |
else | |
curl -X PUT "https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records/${v4record_id}" \ | |
-H "X-Auth-Email: ${email}" \ | |
-H "X-Auth-Key: ${api_key}" \ | |
-H "Content-Type: application/json" \ | |
--data '{"type":"A", "name":"'${domain}'", "content":"'${ipv4}'", "ttl":'${ttl}', "proxied":false}' | |
fi | |
if [ "${ipv6}" = "$(dig ${domain} AAAA +short)" ]; | |
then | |
echo 'IPv6 address is unchanged.' | |
else | |
curl -X PUT "https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records/${v6record_id}" \ | |
-H "X-Auth-Email: ${email}" \ | |
-H "X-Auth-Key: ${api_key}" \ | |
-H "Content-Type: application/json" \ | |
--data '{"type":"AAAA", "name":"'${domain}'", "content":"'${ipv6}'", "ttl":'${ttl}', "proxied":false}' | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment