|
本帖最后由 aming.ou 于 2023-4-24 17:52 编辑
官方说明 https://dynv6.com/docs/apis
以下是我改进过的shell脚本,配置信息写入脚本
保存为dynv6.sh , 修改属于自己的www.dynv6.com上注册的二级域名,修改为自己的token,修改wget程序的路径,其他不变。
上传到/sbin/dynv6.sh ,修改文件权限 chmod 777 /sbin/dynv6.sh
crontab -e进入定时任务管理, 添加一条,每10分钟检查一次,发现IPv6地址变化后才更新
*/10 * * * * /sbin/dynv6.sh
------------------------------------------------------------
#!/bin/sh -e
source /etc/profile
hostname='ddns.dynv6.net'
token='jZsUe3pHmMvLsjdfdfdqc7JTXsxneR'
file=/tmp/.dynv6.addr6
fileerr=/tmp/.dynv6.addr6.log
[ -e $file ] && old=`cat $file`
if [ -z "$netmask" ]; then
netmask=64
fi
unset $address
for ipline in `ip -6 addr list scope global | grep $netmask | grep -v " fd" | sed -n 's/.*inet6 \([0-9a-f:]\+\).*/\1/p'`; do
unset $j
j=`echo $ipline |grep -o "\:" |wc -l`
if [ "$j" = "7" ];then
echo $ipline, $j
address=$ipline
fi
done
if [ -e /usr/bin/wget ]; then
bin="wget -q -O-"
else
echo "neither curl nor wget found"
exit 1
fi
if [ -z "$address" ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') : no IPv6 address found"
echo "$(date '+%Y-%m-%d %H:%M:%S') : no IPv6 address found" >> $fileerr
exit 1
fi
unset $tmpstr
for ipline in `$bin http://ip.zxinc.org/getip`; do
tmpstr=$ipline
echo "Local address= $tmpstr"
done
if [ -z "$tmpstr" ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') : The net is dead."
echo "$(date '+%Y-%m-%d %H:%M:%S') : The net is dead." >> $fileerr
exit 1
fi
# address with netmask
current=$address/$netmask
tmpstr=`ping -6 -c 1 ${hostname} | sed '1{s/[^(]*(//;s/).*//;q}'`
echo "oldIPv6= $tmpstr"
if [ "$old" = "$current" ] && [ "$tmpstr" = "$address" ]; then
echo "IPv6 address unchanged: $current"
echo "$(date '+%Y-%m-%d %H:%M:%S') : $current" >> $fileerr
exit
fi
echo "$(date '+%Y-%m-%d %H:%M:%S') : $old -> $current" >> $fileerr
# send addresses to dynv6
$bin "http://dynv6.com/api/update?hostname=$hostname&ipv6=$current&token=$token"
# save current address
echo $current > $file
------------------------------------------------------------
windows的bat脚本和Linux的shell脚本
Shell更新230424
------------------------------------------------------------2023-04-24更新
IPv6地址分配规则,DHCPv6由服务器分配,是短地址格式,如果是SLAAC分配就是长地址格式,原检测代码是基于长地址编写,为了兼容2种方式,代码修改如下
#----------------Begin
#!/bin/sh -e
source /etc/profile
hostname='ddns.dynv6.net'
token='jZsUe3pHmMvLsjdfdfdqc7JTXsxneR'
file=/tmp/.dynv6.addr6
fileerr=/tmp/.dynv6.addr6.log
[ -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
unset $address
unset $add2ress
for ipline in `ip -6 addr list scope global | grep -v " fd" | sed -n 's/.*inet6 \([0-9a-f:]\+\).*/\1/p'`; do
j=`echo $ipline |grep -o "\:" |wc -l`
echo "$ipline , $j"
if [ "$j" = "7" ];then
address=$ipline
else
if [ "${ipline:0:1}" = "2" ];then
add2ress=$ipline
fi
fi
done
if [ -e /usr/bin/wget ]; then
bin="wget -q -O-"
else
echo " wget not found"
exit 1
fi
if [ -z "$address" ] && [ -z "$add2ress" ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') : no IPv6 address found"
echo "$(date '+%Y-%m-%d %H:%M:%S') : no IPv6 address found" >> $fileerr
exit 1
elif [ -z "$address" ];then
address=$add2ress
fi
echo "IfIPv6= $address"
unset $tmpstr
for ipline in `$bin http://ip.zxinc.org/getip`; do
tmpstr=$ipline
echo "Local address= $tmpstr"
done
if [ -z "$tmpstr" ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') : The net is dead."
echo "$(date '+%Y-%m-%d %H:%M:%S') : The net is dead." >> $fileerr
exit 1
fi
echo "host= $hostname"
if [ $old ]; then
echo "oldIPv6= $old"
fi
# address with netmask
current=$address/$netmask
tmpstr=`ping -6 -c 1 ${hostname} | sed '1{s/[^(]*(//;s/).*//;q}'`
echo "dnsIPv6= $tmpstr"
if [ "$old" = "$current" ] && [ "$tmpstr" = "$address" ]; then
echo "IPv6 address unchanged: $current"
echo "$(date '+%Y-%m-%d %H:%M:%S') : $current" >> $fileerr
exit
elif [ "$old" != "$current" ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') : local= $old -> $current" >> $fileerr
else
echo "$(date '+%Y-%m-%d %H:%M:%S') : dnsIP= $tmpstr -> $current" >> $fileerr
fi
# send addresses to dynv6
$bin "http://dynv6.com/api/update?hostname=$hostname&ipv6=$current&token=$token"
echo .
# save current address
echo $current > $file
#----------------End
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
评分
-
查看全部评分
|