Skip to content

Instantly share code, notes, and snippets.

@Aebian
Last active November 16, 2019 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aebian/507ecf4e0e05a8ed9c01ddf2fd5f9753 to your computer and use it in GitHub Desktop.
Save Aebian/507ecf4e0e05a8ed9c01ddf2fd5f9753 to your computer and use it in GitHub Desktop.
configure
set service dns dynamic interface eth0 service custom-cloudflare login <user@domain.com>
set service dns dynamic interface eth0 service custom-cloudflare password <cloudflare-api-key>
set service dns dynamic interface eth0 service custom-cloudflare protocol cloudflare
set service dns dynamic interface eth0 service custom-cloudflare options zone=domain.com
commit ; save
######################################################
auth_email="mail@company.ca" #Account email
zone_identifier="s0m3n4mb3r" #Log in to Cloudflare, select domain, copy "Zone ID" from Overview
auth_key="s3cr3ts4llth3w4y" #Below "Zone ID", click "Get your API key" and copy the global key
record_name="subdomain.company.ca" #Domain used
######################################################
ipSix=$(/bin/ip -6 addr show $iface|sed -n 's%.*inet6\s\(.*\)/64.*global.*%\1%p')
echo "$ipSix"
log_file="/var/log/update-cloudflare-ddns.log"
log() {
if [ "$1" ]; then
echo -e "[$(date)] - $1" >> $log_file
fi
}
recordAAAA_ip=$(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" | grep -Po '(?<="content":")[^"]*')
#Stop when same
if [[ $ipSix == $recordAAAA_ip ]]; then
exit 0
fi
recordAAAA_identifier=$(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" | grep -Po '(?<="id":")[^"]*')
#Update when AAAA different
if [[ $ipSix != $recordAAAA_ip ]]; then
updateSix=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$recordAAAA_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"AAAA\",\"name\":\"$record_name\",\"content\":\"$ipSix\"}")
if [[ $updateSix == *"\"success\":false"* ]]; then
message="API UPDATE FAILED. DUMPING RESULTS:\n$update"
log "IPv6 error + $message"
echo -e "IPv6 error + $message"
exit 1
else
message="IPv6 changed to: $ipSix"
log "$message"
fi
fi
* * * * * /config/utilities/cloudflare-ddns.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment