Last active
August 11, 2022 02:20
-
-
Save mbierman/d4c6aac8e741fb0cbb289ce1899df59a to your computer and use it in GitHub Desktop.
DDNS update for Cloudflare with dual WAN support
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
#!/usr/bin/env bash | |
zoneID="" | |
ID="" | |
APIToken="-" | |
name="." | |
days=2020 | |
result="" | |
# */5 * * * * /home/pi/.firewalla/config/post_main.d/updateIPCloudflare.sh >/dev/null 2>&1walla" ]; then | |
# currentIP=$(curl -s ifconfig.me/ip) | |
# currentIP=$(ifconfig eth0 | grep "inet" | grep netmask | cut -f2 -d"t" | sed -e 's| ||g' -e 's|[a-zA-Z]||g') | |
DIRECTORY=$(cd `dirname $0` && pwd) | |
log="$DIRECTORY/updateIP.log" | |
touch $log | |
echo $log | |
logMe () { | |
echo "$(tail -n $days $log)" > $log | |
} | |
IPINFO=$(curl -s https://ipinfo.io/) | |
provider=$(echo $IPINFO | jq ".org" | grep Sail) | |
currentIP=$(echo $IPINFO | jq ".ip" | sed -e 's|"||g') | |
echo $(date) Org: $provider | tee -a $log | |
echo $(date) IP: $currentIP | tee -a $log | |
if [ -n "$provider" ]; then | |
echo $(date) "Sail away!" | tee -a $log | |
else | |
echo $(date) "LTE: skipping update..." | tee -a $log | |
logMe | |
exit | |
fi | |
cloudflaresays=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zoneID/dns_records?type=A&name=private.thebiermans.net&page=1&per_page=5&match=all" \ | |
-H "Authorization: Bearer $APIToken" \ | |
-H "Content-Type: application/json" | grep content | sed -e 's|{.*"content":"||g' | sed -e 's|","proxiable.*||g') | |
echo "Current:" $currentIP | |
echo "clouddlare" $cloudflaresays | |
if [ "$1" = "-f" ] ; then | |
cloudflaresays="666" | |
result="(forced update)" | |
echo "May the force be with you, $cloudflaresays" | |
fi | |
if [ "$currentIP" = "$cloudflaresays" ] ; then | |
echo $(date) "No change required" | tee -a $log | |
logMe | |
exit | |
else | |
curl -s -X PATCH "https://api.cloudflare.com/client/v4/zones/$zoneID/dns_records/$ID" \ | |
-H "Authorization: Bearer $APIToken" \ | |
-H "Content-Type: application/json" \ | |
--data '{"type":"A","name":"'$name'","content":"'$currentIP'","ttl":600,"proxied":false}' > \ | |
/dev/null && echo $(date) $name was updated to: $currentIP $result | tee -a $log | |
fi | |
logMe |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run through cron.
To create scheduled jobs, you may add your own cronjob by putting cronjob expression in this file. It will be incorporated to the system crontab when Firewalla service restarts.
/home/pi/.firewalla/config/user_crontab
Example:
pi@firewalla:~ (Firewalla) $ cat ~/.firewalla/config/user_crontab
You can verify if it's incorporated by running "crontab -l"
See also https://github.com/ccpk1/Firewalla-Gold-Tools/blob/main/cloudflare/cloudflare_ddns_update.sh