Skip to content

Instantly share code, notes, and snippets.

@s-leon
Created March 15, 2021 16:49
Show Gist options
  • Save s-leon/9ae318766eb0947fa33a4bb4e5120526 to your computer and use it in GitHub Desktop.
Save s-leon/9ae318766eb0947fa33a4bb4e5120526 to your computer and use it in GitHub Desktop.
#/usr/bin/env sh
# Get the Zone ID from: https://www.cloudflare.com/a/overview/<your-domain>
DNS_ZONE=***
# Get the existing identifier for DNS entry:
# https://api.cloudflare.com/#dns-records-for-a-zone-list-dns-records
IDENTIFIER=***
# Get these from: https://www.cloudflare.com/a/account/my-account
AUTH_EMAIL=***
AUTH_KEY=***
# Desired domain name
DOMAIN_NAME="***"
# Get previous IP address
_PREV_IP_FILE="/tmp/public-ip.txt"
_PREV_IP=$(cat $_PREV_IP_FILE)
# Curl fritzbox public IP
_IP=$(curl "http://192.168.1.1:49000/igdupnp/control/WANIPConn1" \
-H "Content-Type: text/xml; charset="utf-8"" \
-H "SoapAction:urn:schemas-upnp-org:service:WANIPConnection:1#GetExternalIPAddress" \
-d "<?xml version='1.0' encoding='utf-8'?> <s:Envelope s:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'> <s:Body> <u:GetExternalIPAddress xmlns:u='urn:schemas-upnp-org:service:WANIPConnection:1' /> </s:Body> </s:Envelope>" \
-s | grep -Eo '\<[[:digit:]]{1,3}(\.[[:digit:]]{1,3}){3}\>')
#_IP=$(curl --silent https://api.ipify.org)
echo $_IP
# If new/previous IPs match, no need for an update.
if [ "$_IP" = "$_PREV_IP" ]; then
exit 0
fi
echo $_IP
_UPDATE=$(cat <<EOF
{ "type": "A",
"name": "$DOMAIN_NAME",
"content": "$_IP",
"ttl": 120,
"proxied": true }
EOF
)
curl "https://api.cloudflare.com/client/v4/zones/$DNS_ZONE/dns_records/$IDENTIFIER" \
--silent \
-X PUT \
-H "Content-Type: application/json" \
-H "X-Auth-Email: $AUTH_EMAIL" \
-H "X-Auth-Key: $AUTH_KEY" \
-d "$_UPDATE" > /tmp/cloudflare-ddns-update.json && \
echo $_IP > $_PREV_IP_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment