Skip to content

Instantly share code, notes, and snippets.

@hiifeng
Last active February 27, 2024 00:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiifeng/0afc189bb79b3a6cc239d45af3ec6dbe to your computer and use it in GitHub Desktop.
Save hiifeng/0afc189bb79b3a6cc239d45af3ec6dbe to your computer and use it in GitHub Desktop.
Because we don't want to use crontab to execute the script, we hope to execute the update script through the /etc/ppp/ipv6-up script of the system after establishing the pppoe session. But I found that after my router established pppoe session, the IPv6 address acquisition would be delayed, resulting in the update failure when $address was empty…
#!/bin/sh -e
# based on https://gist.github.com/corny/7a07f5ac901844bd20c9
# modify by ifeng; May 13, 2021
#hostname=$1
#device=$2
# Enter the relevant parameters in line 10-12. “device” enter the interface name, such as pppoe0, switch0, it is recommended to enter pppoe0.
token=your token
hostname=example.dynv6.net
device=pppoe0
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
# When the address is empty, wait for 1 minute and then assign the value again. The loop ends after 30 times.
i=1
while [ $i -lt 30 ]
do
address=$(ip -6 addr list scope global $device | grep -v " fd" | sed -n 's/.*inet6 \([0-9a-f:]\+\).*/\1/p' | head -n 1)
if [ -z "$address" ]; then
sleep 1m
((i++))
else
break
fi
done
# 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
@hiifeng
Copy link
Author

hiifeng commented May 13, 2021

Because we don't want to use crontab to execute the script, we hope to execute the update script through the /etc/ppp/ipv6-up script of the system after establishing the pppoe session. But I found that after my router established pppoe session, the IPv6 address acquisition would be delayed, resulting in the update failure when $address was empty. Therefore, the base script is modified. When it is found that $address is empty, wait for 1 minute and then assign the value again, The loop ends after 30 times.

Note: crontab is not recommended for this script, which will increase the operating system load.

@LEON-CD
Copy link

LEON-CD commented Feb 27, 2024

Very good!!!

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