Last active
November 13, 2023 01:49
-
-
Save saudiqbal/9adbaa44a40a02cc4f0cdf28c62c56c8 to your computer and use it in GitHub Desktop.
Dynamic DNS bash script for IPv6 prefix using token
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 | |
# Create an empty file first at $HOME/ddns-ipv6-address.txt with a 0 in it. | |
SUFFIX="::71c6:b34f:8e2a:54f5" | |
ADDRSTORE=$HOME/ddns-ipv6-address.txt | |
STOREDIPV6=$(cat $ADDRSTORE) | |
__rfc5952_expand () { | |
read addr mask < <(IFS=/; echo $1) | |
quads=$(grep -oE "[a-fA-F0-9]{1,4}" <<< ${addr/\/*} | wc -l) | |
grep -qs ":$" <<< $addr && { addr="${addr}0000"; (( quads++ )); } | |
grep -qs "^:" <<< $addr && { addr="0000${addr}"; (( quads++ )); } | |
[ $quads -lt 8 ] && addr=${addr/::/:$(for (( i=1; i<=$(( 8 - quads )) ; i++ )); do printf "0000:"; done)} | |
addr=$(for quad in $(IFS=:; echo ${addr}); do printf "${delim}%04x" "0x${quad}"; delim=":"; done) | |
[ ! -z $mask ] && echo $addr/$mask || echo $addr | |
} | |
SUFFIX="$(__rfc5952_expand $SUFFIX)" | |
SUFFIX=${SUFFIX: -19} | |
ALLIPV6=$(/sbin/ip -6 addr | grep inet6 | awk -F '[ \t]+|/' '{print $3}' | grep '^2') | |
for i in $ALLIPV6; do | |
EXPANDED="$(__rfc5952_expand $i)" | |
EXPANDEDSUFFIX=${EXPANDED: -19} | |
if [ $EXPANDEDSUFFIX == $SUFFIX ]; then | |
if [ $EXPANDED != $STOREDIPV6 ]; then | |
# Your DDNS update command(s) go here | |
# Use the variable $EXPANDED to substitute the IPv6 address in your script. | |
echo $EXPANDED > $ADDRSTORE | |
logger "DDNS new IP address detected $EXPANDED" | |
fi | |
fi | |
done | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Full details: https://www.saudiqbal.com/blog/dynamic-dns-bash-shell-script-for-ipv6-prefix-using-token-or-dhcpv6-for-linux.php