Last active
February 20, 2023 06:38
-
-
Save oxycoder/a24e5c2ade5e4dd1cd7c6afd433d08be to your computer and use it in GitHub Desktop.
DDNS Cloudflare
This file contains hidden or 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 hours | |
0 * * * * /ddns.sh |
This file contains hidden or 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 | |
CF_EMAIL="your cf email" | |
CF_TOKEN="your cf token" | |
CF_ZONE_ID="your cf zone id" | |
# array of subdomains to update | |
declare -a subDomains=( | |
"record_1_id sub1.example.com" | |
"record_2_id sub2.example.com" | |
) | |
updateIP() { | |
printf "Update sub domain $1 with $IP\n" | |
curl -X PATCH https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records/$1 \ | |
-H 'Content-Type: application/json' \ | |
-H 'X-Auth-Email: '$CF_EMAIL'' \ | |
-H 'Authorization: Bearer '$CF_TOKEN'' \ | |
--data '{ | |
"type": "A", | |
"content": "'$IP'", | |
"name": "'$2'", | |
"proxied": true, | |
"ttl": 1 | |
}' | |
printf "\n" | |
} | |
IP=$(curl https://api.ipify.org) | |
echo "Server IP: $IP" | |
for i in "${subDomains[@]}" | |
do | |
readarray -d " " -t strarr <<< "$i" | |
updateIP ${strarr[0]} ${strarr[1]} | |
done | |
echo "Finished" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment