Last active
September 18, 2025 20:16
-
-
Save fathonix/2ea5bae8bd1873e5b9b5909f9eeabfb8 to your computer and use it in GitHub Desktop.
Local DDNS for gateway IPs on OpenWrt
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/sh | |
| # dyngwdomain.sh - Local DDNS for gateway IPs on OpenWrt | |
| # Licensed under MIT. (c) 2025 Aldo Adirajasa Fathoni | |
| domain="$1" | |
| iface="$2" | |
| msg() { | |
| echo $@ | |
| logger -t dyngwdomain.sh[$$] "$@" | |
| } | |
| die() { | |
| msg "${@}, domain=${domain} iface=${iface}" >&2 | |
| exit 1 | |
| } | |
| idx=$(uci show dhcp | sed -nr "s/dhcp\.@domain\[(-?[0-9]+)\]\.name='${domain}'/\1/p") | |
| if ! [ "$idx" -ge 0 ] 2> /dev/null; then | |
| die "Error checking DNS record" | |
| fi | |
| oldip=$(uci get dhcp.@domain[${idx}].ip) | |
| if [ -z "$oldip" ]; then | |
| die "Error checking old gateway" | |
| fi | |
| newip=$(ip route show 0.0.0.0/0 dev "$iface" | cut -d\ -f3) | |
| if [ -z "$newip" ]; then | |
| die "Error checking new gateway" | |
| fi | |
| if [ "$oldip" != "$newip" ]; then | |
| uci set dhcp.@domain[${idx}].ip="$newip" | |
| uci commit dhcp.@domain[${idx}].ip | |
| /etc/init.d/dnsmasq reload | |
| msg "${domain}: ${oldip} -> ${newip}" | |
| else | |
| msg "${domain} still points to ${oldip}" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment