-
-
Save madatomanic/8514cbc93e200f905f8d3e81fb7c5f40 to your computer and use it in GitHub Desktop.
A bash script to update a Cloudflare DNS A record with the external IP of the source machine
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 | |
# A bash script to update a Cloudflare DNS A record with the external IP of the source machine | |
# Used to provide DDNS service for my home | |
# Needs the DNS record pre-creating on Cloudflare | |
# Proxy - uncomment and provide details if using a proxy | |
#export https_proxy=http://<proxyuser>:<proxypassword>@<proxyip>:<proxyport> | |
# Cloudflare zone is the zone which holds the record | |
zone=example.com | |
# DNS record is the A record which will be updated | |
dnsrecord=www.example.com | |
## Cloudflare authentication details | |
## Get Auth Key from Profile > API Tokens > Global API key | |
cloudflare_auth_email=me@cloudflare.com | |
cloudflare_auth_key=1234567890abcdef1234567890abcdef | |
#Variables for Logging messages (location and timestamp format) | |
#Ensure you have permissions to write to the Logfile | |
LOGFILE="/var/log/syslog" | |
TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"` | |
# Get the current external IP address | |
ip=$(curl -s -X GET https://checkip.amazonaws.com) | |
echo "$TIMESTAMP CLOUDFLARE-SCRIPT : Current IP is $ip" | tee -a $LOGFILE | |
if host $dnsrecord 1.1.1.1 | grep "has address" | grep "$ip"; then | |
echo "$TIMESTAMP CLOUDFLARE-SCRIPT : $dnsrecord is currently set to $ip; no changes needed" | tee -a $LOGFILE | |
exit | |
fi | |
# if here, the dns record needs updating | |
# get the zone id for the requested zone | |
zoneid=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone&status=active" \ | |
-H "X-Auth-Email: $cloudflare_auth_email" \ | |
-H "X-Auth-Key: $cloudflare_auth_key" \ | |
-H "Content-Type: application/json" | jq -r '{"result"}[] | .[0] | .id') | |
echo "$TIMESTAMP CLOUDFLARE-SCRIPT : Zone ID for $zone is $zoneid" | tee -a $LOGFILE | |
# get the dns record id | |
dnsrecordid=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records?type=A&name=$dnsrecord" \ | |
-H "X-Auth-Email: $cloudflare_auth_email" \ | |
-H "X-Auth-Key: $cloudflare_auth_key" \ | |
-H "Content-Type: application/json" | jq -r '{"result"}[] | .[0] | .id') | |
echo "$TIMESTAMP CLOUDFLARE-SCRIPT : DNS Record ID for $dnsrecord is $dnsrecordid" | tee -a $LOGFILE | |
# update the record | |
curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zoneid/dns_records/$dnsrecordid" \ | |
-H "X-Auth-Email: $cloudflare_auth_email" \ | |
-H "X-Auth-Key: $cloudflare_auth_key" \ | |
-H "Content-Type: application/json" \ | |
--data "{\"type\":\"A\",\"name\":\"$dnsrecord\",\"content\":\"$ip\",\"ttl\":1,\"proxied\":false}" | jq |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment