Skip to content

Instantly share code, notes, and snippets.

@marco79cgn
Last active November 23, 2025 11:56
Show Gist options
  • Select an option

  • Save marco79cgn/299c669065e87949d01d6f367f894c01 to your computer and use it in GitHub Desktop.

Select an option

Save marco79cgn/299c669065e87949d01d6f367f894c01 to your computer and use it in GitHub Desktop.
Shell script which checks whether the public ip address has changed and updates myaddr.tools DynDNS
#!/bin/bash
# insert your myaddr data and ntfy push topic
myAddrApiKey="***"
myAddrUrl="replace-me.myaddr.io"
ntfyUrl="https://ntfy.sh/***"
publicDnsResolver="86.54.11.100" # use DNS4EU
# function to validate IPv4 address (format + range)
is_ipv4() {
local ip="$1"
local IFS=.
local o1 o2 o3 o4
# Split into 4 octets
read -r o1 o2 o3 o4 <<< "$ip"
# Must have 4 numeric fields
[[ "$o1" =~ ^[0-9]+$ && "$o2" =~ ^[0-9]+$ && "$o3" =~ ^[0-9]+$ && "$o4" =~ ^[0-9]+$ ]] || return 1
# Range check 0–255
(( o1 >= 0 && o1 <= 255 )) || return 1
(( o2 >= 0 && o2 <= 255 )) || return 1
(( o3 >= 0 && o3 <= 255 )) || return 1
(( o4 >= 0 && o4 <= 255 )) || return 1
return 0
}
# max 6 tries every 5 seconds (if connection is currently offline)
if ! currentDynDns=$(dig +time=5 +tries=6 +short $myAddrUrl @"$publicDnsResolver"); then
echo "Problem: dig network error or timeout."
exit 1
fi
# validate dyndns IPv4
if ! is_ipv4 "$currentDynDns"; then
echo "Fehler: '$currentDynDns' ist keine gültige IPv4-Adresse."
exit 1
fi
echo "myaddr dyndns ip address: $currentDynDns"
# check public ip address using ipify.org
currentIp=$(curl -s https://api.ipify.org)
# validate public IPv4
if ! is_ipv4 "$currentIp"; then
echo "Fehler: '$currentIp' ist keine gültige IPv4-Adresse."
exit 1
fi
echo "Public ip address: $currentIp"
# update dyndns only if ip address has changed
if [ "$currentIp" != "$currentDynDns" ]; then
echo "IP changed, updating myaddr dns..."
# update ipv4
curl -s "https://ipv4.myaddr.tools/update?ip=$currentIp&key=$myAddrApiKey"
# update ipv6
currentIpV6=$(curl -s 'https://api64.ipify.org')
curl -s "https://ipv6.myaddr.tools/update?ip=$currentIpV6&key=$myAddrApiKey"
# send push notification
ntfyResult=$(curl -s -X POST -H "Title: DDNS Update" -H "Tags: ballot_box_with_check" -d "$myAddrUrl mit IP-Adresse $currentIp aktualisiert!" $ntfyUrl)
else
echo "Nothing to do, ip hasn't changed."
fi
@marco79cgn
Copy link
Author

Claim your DynDNS domain at myaddr.tools

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment