Skip to content

Instantly share code, notes, and snippets.

@s-mokrushin
Last active April 28, 2021 18:03
Show Gist options
  • Save s-mokrushin/002550c62ad14e7594f320600c417cbe to your computer and use it in GitHub Desktop.
Save s-mokrushin/002550c62ad14e7594f320600c417cbe to your computer and use it in GitHub Desktop.
Dynamic DNS updating bash script via Cloudflare API
#!/bin/bash
zoneidentity="put here"
dnsidentity="put here"
authtoken="put here"
domain="put here"
myip="$(dig +short myip.opendns.com @resolver1.opendns.com)"
echo "Detected WAN IP address: ${myip}"
if [ -f ".ipaddress" ]; then
compare=$(cat .ipaddress)
else
compare="unknown"
fi
echo "Last updated WAN IP address: ${compare}"
if [[ "$compare" != "$myip" ]]; then
response=$(curl \
--write-out %{http_code} --silent --output /dev/null \
-X PUT "https://api.cloudflare.com/client/v4/zones/${zoneidentity}/dns_records/${dnsidentity}" \
-H "Authorization: Bearer ${authtoken}" \
-H "Content-Type: application/json" \
--data "{\"type\":\"A\",\"name\":\"${domain}\",\"content\":\"${myip}\",\"ttl\":120,\"proxied\":false}")
if [[ "$response" == "200" ]]; then
echo "$myip" > .ipaddress
echo "Update success"
else
echo "Update error: ${response}"
fi
else
echo "IP already updated"
fi
# run script every minute
* * * * * /root/cloudflare-update-ip.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment