Last active
March 9, 2021 19:08
-
-
Save pexcn/c2e16205f2973d70f4e68d2f04b15178 to your computer and use it in GitHub Desktop.
Simple DDNS script for Hurricane Electric (dns.he.net)
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 -e | |
# | |
# Hurricane Electric DDNS script for OpenWrt | |
# | |
# crontab: */5 * * * * INTERFACE=pppoe-wan USE_PEERDNS=1 DOMAIN=example.com KEY=password /usr/bin/he-ddns.sh | |
# | |
set -o pipefail | |
INTERFACE="${INTERFACE:-pppoe-wan}" | |
DOMAIN="${DOMAIN:-example.com}" | |
KEY="${KEY:-password}" | |
USE_PEERDNS="${USE_PEERDNS:-0}" | |
get_interface_ipv4() { | |
ip addr show dev "$1" | grep -w "inet" | awk '{print $2}' | awk -F '/' '{print $1}' | |
} | |
get_interface_ipv6() { | |
ip addr show dev "$1" | grep -w "inet6" | awk '{print $2}' | awk -F '/' '{print $1}' | |
} | |
get_domain_ipv4() { | |
nslookup "$1" $([ "$USE_PEERDNS" = 1 ] && get_peerdns) | grep "Address 1:" | awk '{print $3}' | |
} | |
get_domain_ipv6() { | |
nslookup -query=AAAA "$1" $([ "$USE_PEERDNS" = 1 ] && get_peerdns) | grep "has AAAA address" | awk '{print $5}' | head -1 | |
} | |
get_peerdns() { | |
[ -f /tmp/resolv.conf.ppp ] && cat /tmp/resolv.conf.ppp | awk '{print $2}' | head -1 | |
} | |
ip_updatable() { | |
local interface_ip="$1" | |
local domain_ip="$2" | |
[ -z "$interface_ip" ] && return 1 | |
[ "$interface_ip" = "$domain_ip" ] && return 1 | |
return 0 | |
} | |
do_update() { | |
local update_url="$1" | |
for i in $(seq 1 10) | |
do | |
if wget -q -T 10 -O - "$update_url"; then | |
break | |
fi | |
sleep 3 | |
done | |
} | |
update_ddns() { | |
local interface_ipv4="$(get_interface_ipv4 $INTERFACE)" | |
local interface_ipv6="$(get_interface_ipv6 $INTERFACE)" | |
local domain_ipv4="$(get_domain_ipv4 $DOMAIN)" | |
local domain_ipv6="$(get_domain_ipv6 $DOMAIN)" | |
local update_url_ipv4="https://dyn.dns.he.net/nic/update?hostname=${DOMAIN}&password=${KEY}&myip=${interface_ipv4}" | |
local update_url_ipv6="https://dyn.dns.he.net/nic/update?hostname=${DOMAIN}&password=${KEY}&myip=${interface_ipv6}" | |
if ip_updatable "$interface_ipv4" "$domain_ipv4"; then | |
do_update "$update_url_ipv4" | |
fi | |
if ip_updatable "$interface_ipv6" "$domain_ipv6"; then | |
do_update "$update_url_ipv6" | |
fi | |
} | |
update_ddns |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment