Skip to content

Instantly share code, notes, and snippets.

@saudiqbal
Last active November 13, 2023 01:49
Show Gist options
  • Save saudiqbal/fb567cf058f4b2b87c7b4e78ea7b9e66 to your computer and use it in GitHub Desktop.
Save saudiqbal/fb567cf058f4b2b87c7b4e78ea7b9e66 to your computer and use it in GitHub Desktop.
Dynamic DNS bash script for IPv6 prefix using DHCPv6
#!/bin/bash
# Create an empty file first at $HOME/ddns-ipv6-address.txt with a 0 in it.
ADDRSTORE=$HOME/ddns-ipv6-address.txt
__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
}
DHCPv6=$(/sbin/ip -6 addr | grep inet6 | grep -vE 'host|mngtmpaddr|temporary' | grep -F 'scope global dynamic' | sed -e 's/^.*inet6 \([^ ]*\)\/.*$/\1/;t;d' | grep '^2')
CURRENTIPV6="$(__rfc5952_expand $DHCPv6)"
STOREDIPV6=$(cat $ADDRSTORE)
if [ $CURRENTIPV6 != $STOREDIPV6 ]; then
# Your DDNS update command(s) go here
# Use the variable $CURRENTIPV6 to substitute the IPv6 address in your script.
echo $CURRENTIPV6 > $ADDRSTORE
logger "DDNS new IP address detected $CURRENTIPV6"
fi
exit 0
@saudiqbal
Copy link
Author

saudiqbal commented Nov 13, 2023

Quick command to create a text file.

`
cat > $HOME/ddns-ipv6-address.txt <<EOL

0

EOL

chmod 777 $HOME/ddns-ipv6-address.txt
`

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