Skip to content

Instantly share code, notes, and snippets.

@corny
Last active May 31, 2022
Embed
What would you like to do?
Update script for dynv6.com to set your IPv4 address and IPv6 prefix
#!/bin/sh -e
hostname=$1
device=$2
file=$HOME/.dynv6.addr6
[ -e $file ] && old=`cat $file`
if [ -z "$hostname" -o -z "$token" ]; then
echo "Usage: token=<your-authentication-token> [netmask=64] $0 your-name.dynv6.net [device]"
exit 1
fi
if [ -z "$netmask" ]; then
netmask=128
fi
if [ -n "$device" ]; then
device="dev $device"
fi
address=$(ip -6 addr list scope global $device | grep -v " fd" | sed -n 's/.*inet6 \([0-9a-f:]\+\).*/\1/p' | head -n 1)
if [ -e /usr/bin/curl ]; then
bin="curl -fsS"
elif [ -e /usr/bin/wget ]; then
bin="wget -O-"
else
echo "neither curl nor wget found"
exit 1
fi
if [ -z "$address" ]; then
echo "no IPv6 address found"
exit 1
fi
# address with netmask
current=$address/$netmask
if [ "$old" = "$current" ]; then
echo "IPv6 address unchanged"
exit
fi
# send addresses to dynv6
$bin "http://dynv6.com/api/update?hostname=$hostname&ipv6=$current&token=$token"
$bin "http://ipv4.dynv6.com/api/update?hostname=$hostname&ipv4=auto&token=$token"
# save current address
echo $current > $file
@helianling
Copy link

helianling commented Nov 15, 2021

Only for IPV6 DDNS

filename: dynv6.sh
path:/usr/bin
chmod +x dynv6.sh


#!/bin/sh -e
GETIPV6="https://ip.ddnspod.com"
hostname="xxxxx.v6.rocks"
token="xxxxxxxxxxxxxxxxxxxxxxxxxx"

file=$HOME/.dynv6.addr6
[ -e $file ] && OldIP=cat $file
echo "[Old IP]:$OldIP"

NewIP=$(curl -6 -s -k $GETIPV6)
echo "[New IP]:$NewIP"

if [ "$OldIP" == "$NewIP" ];then
echo "IP IS SAME,SKIP UPDATE."
exit
fi

#send addresses to dynv6
curl "http://dynv6.com/api/update?hostname=$hostname&ipv6=$NewIP&token=$token"
#curl "http://ipv4.dynv6.com/api/update?hostname=$hostname&ipv4=auto&token=$token"

#save current address
echo $NewIP > $file


[root@C921 ~]# crontab -e
*/1 * * * * dynv6.sh &>/dev/null 2>&1

@tolshao
Copy link

tolshao commented Nov 29, 2021

@andi34
the following script was out of work
address=$(wget -O- "https://api6.ipify.org")
"api64.ipify.org " always returns ipv4 address

@andi34
Copy link

andi34 commented Nov 29, 2021

@tolshao nope, works fine. Typo on your end? api64 vs api6?

@tm-107
Copy link

tm-107 commented Feb 16, 2022

I don't like caching the "old" IP in a file because if it was changed manually on dynv6.com or the update went wrong, the script doesn't notice.
So this variant works very well for me:

#!/bin/sh

HOSTNAME_DYNV6="****.dynv6.net"
TOKEN_DYNV6="****"

IP4ADDR=$(curl -s http://ipecho.net/plain)
IP6ADDR=`ip addr show eth0 | grep 'scope global dynamic' | grep -Po 'inet6 \K[0-9a-fA-F:]+'`

if [ "$IP4ADDR" = "" ]
then
        echo "Error: unable to determine IPv4 address" 1>&2
fi

if [ "$IP6ADDR" = "" ]
then
        echo "Error: unable to determine IPv6 address" 1>&2
fi

if [ "$IP4ADDR" != "" ]
then
	ping $HOSTNAME_DYNV6 -4 -c 1 > null # a little dirty - needed to update dns-cache
	IP4ADDR_DYNV6=$(dig $HOSTNAME_DYNV6 A +short)

	if [ "$IP4ADDR" != "$IP4ADDR_DYNV6" ]
	then
		echo "IPv4 adress has changed -> update ..."
		curl -s "https://ipv4.dynv6.com/api/update?hostname=$HOSTNAME_DYNV6&token=$TOKEN_DYNV6&ipv4=auto"
		echo "---"
	fi
fi

if [ "$IP6ADDR" != "" ]
then
        ping $HOSTNAME_DYNV6 -6 -c 1 > null # a little dirty - needed to update dns-cache
		IP6ADDR_DYNV6=$(dig $HOSTNAME_DYNV6 AAAA +short)

	if [ "$IP6ADDR" != "$IP6ADDR_DYNV6" ]
	then
		echo "IPv6 adress has changed -> update ..."
		curl -s "https://ipv6.dynv6.com/api/update?hostname=$HOSTNAME_DYNV6&token=$TOKEN_DYNV6&ipv6prefix=auto"
		echo "---"
	fi
fi

@vinnymac
Copy link

vinnymac commented May 31, 2022

@tm-107 thanks for the script. While this did most of the work for me, I had to modify it to the following for it to work for my needs. For some reason auto was setting the IPV6 address to one that ended in :: and was missing the last three characters of my address. I made some small modifications to use the current address, including the netmask.

#!/bin/sh

HOSTNAME_DYNV6="****.dynv6.net"
TOKEN_DYNV6="****"
DEVICE_INTERFACE=eno1

IP4ADDR=$(curl -s http://ipecho.net/plain)
IP6ADDR_WITH_MASK=`ip -6 addr list scope global dynamic $DEVICE_INTERFACE | grep -Po 'inet6 \K[0-9a-fA-F:\/]+' | head -n1 | tr -d '\n'`
IP6ADDR=`echo $IP6ADDR_WITH_MASK | tr '/' '\n' | head -n1`

if [ "$IP4ADDR" = "" ]
then
        echo "Error: unable to determine IPv4 address" 1>&2
fi

if [ "$IP6ADDR" = "" ]
then
        echo "Error: unable to determine IPv6 address" 1>&2
fi

if [ "$IP4ADDR" != "" ]
then
	ping $HOSTNAME_DYNV6 -4 -c 1 > null # a little dirty - needed to update dns-cache
	IP4ADDR_DYNV6=$(dig $HOSTNAME_DYNV6 A +short)

	if [ "$IP4ADDR" != "$IP4ADDR_DYNV6" ]
	then
		echo "IPv4 adress has changed -> update ..."
		curl -s "https://ipv4.dynv6.com/api/update?hostname=$HOSTNAME_DYNV6&token=$TOKEN_DYNV6&ipv4=auto"
		echo "---"
	fi
fi

if [ "$IP6ADDR" != "" ]
then
        ping $HOSTNAME_DYNV6 -6 -c 1 > null # a little dirty - needed to update dns-cache
		IP6ADDR_DYNV6=$(dig $HOSTNAME_DYNV6 AAAA +short)

	if [ "$IP6ADDR" != "$IP6ADDR_DYNV6" ]
	then
		echo "IPv6 adress has changed -> update ..."
		curl -s "https://ipv6.dynv6.com/api/update?hostname=$HOSTNAME_DYNV6&token=$TOKEN_DYNV6&ipv6prefix=$IP6ADDR_WITH_MASK"
		echo "---"
	fi
fi

@tolshao and @andi34
curl https://api6.ipify.org will actually return an IPV4 address, but only if you have disabled IPV6. I imagine that was what happened above.

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