|
#!/bin/bash |
|
|
|
# Forked by benkulbertis/cloudflare-update-record.sh |
|
# CHANGE THESE |
|
auth_email="john.appleseed@example.org" # The email used to login 'https://dash.cloudflare.com' |
|
auth_key="f1nd7h47fuck1n6k3y1ncl0udfl4r3c0n50l3" # Top right corner, "My profile" > "Global API Key" |
|
zone_identifier="f1nd7h3fuck1n6z0n31d3n71f13r4l50" # Can be found in the "Overview" tab of your domain |
|
record_name="ipv4.example.org" # Which record you want to be synced |
|
|
|
# DO NOT CHANGE LINES BELOW |
|
ip4=$(curl -s https://ipv4.icanhazip.com/) |
|
ip6=$(curl -s https://ipv6.icanhazip.com/) |
|
|
|
# SCRIPT START |
|
echo "[Cloudflare DDNS] Check Initiated" |
|
|
|
# Seek for the record |
|
record4=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name&type=A" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json") |
|
record6=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name&type=AAAA" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json") |
|
|
|
# Can't do anything without both record |
|
if [[ $record4 == *"\"count\":0"* || $record6 == *"\"count\":0"* ]]; then |
|
>&2 echo -e "[Cloudflare DDNS] Dual stack records do not exist, perhaps create them first?" |
|
exit 1 |
|
fi |
|
|
|
# Set existing IP address from the fetched record |
|
old_ip4=$(echo "$record4" | grep -Po '(?<="content":")[^"]*' | head -1) |
|
old_ip6=$(echo "$record6" | grep -Po '(?<="content":")[^"]*' | head -1) |
|
|
|
# Compare either one is the same |
|
# NOTE: The script will update even one IP remains the same. |
|
if [[ $ip4 == $old_ip4 && $ip6 == $old_ip6 ]]; then |
|
echo "[Cloudflare DDNS] IPs have not changed." |
|
exit 0 |
|
fi |
|
|
|
# Set the record identifier from result |
|
record4_identifier=$(echo "$record4" | grep -Po '(?<="id":")[^"]*' | head -1) |
|
record6_identifier=$(echo "$record6" | grep -Po '(?<="id":")[^"]*' | head -1) |
|
|
|
# The execution of update |
|
update4=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record4_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"proxied\":false,\"name\":\"$record_name\",\"content\":\"$ip4\"}") |
|
update6=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record6_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"AAAA\",\"proxied\":false,\"name\":\"$record_name\",\"content\":\"$ip6\"}") |
|
|
|
# The moment of truth |
|
if [[ $update4 == *"\"success\":false"* || $update6 == *"\"success\":false"* ]]; then |
|
>&2 echo -e "[Cloudflare DDNS] Update failed. DUMPING RESULTS:\n$update4\n$update6" |
|
exit 1 |
|
else |
|
echo "[Cloudflare DDNS] IPv4 address '$ip4' and IPv6 address '$ip6' has been synced to Cloudflare." |
|
fi |
Great work on this, thank you. I had to make a few minor edits to the cfupdater-dualstack file to make it work. I only tested it standalone (.sh), no systemd. Prior to the changes below, I was getting a bash error for line 33, and a "7001" error from Cloudflare, "Method PUT not available for that URI."
Line 12 should read
ip6=$(curl -s https://ipv6.icanhazip.com/)
instead ofip6=$(curl -s https://ipv46.icanhazip.com/)
Line 33 should read
if [[ $ip4 == $old_ip4 && $ip6 == $old_ip6 ]]; then
instead ofif [ $ip4 == $old_ip4 && $ip6 == $old_ip6 ]; then
Line 43 should read
update4=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record4_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"proxied\":false,\"name\":\"$record_name\",\"content\":\"$ip4\"}")
instead of
update4=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier4" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"proxied\":false,\"name\":\"$record_name\",\"content\":\"$ip4\"}")
Line 44 should read
update6=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record6_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"AAAA\",\"proxied\":false,\"name\":\"$record_name\",\"content\":\"$ip6\"}")
instead of
update6=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier6" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"AAAA\",\"proxied\":false,\"name\":\"$record_name\",\"content\":\"$ip6\"}")