Last active
April 28, 2021 18:03
-
-
Save s-mokrushin/002550c62ad14e7594f320600c417cbe to your computer and use it in GitHub Desktop.
Dynamic DNS updating bash script via Cloudflare API
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 | |
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 |
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
# 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