|
此脚本可以用,需自己添加域名,token ID密码。 我用H大的固件,只能添加群晖定时任务才能成功。路由器定时任务失败。
#!/bin/sh
#################################################
# AnripDdns v5.08
# 基于DNSPod用户API实现的动态域名客户端
# 作者: 若海[mail@anrip.com]
# 介绍: http://www.anrip.com/ddnspod
# 时间: 2016-02-24 16:25:00
# Mod: 荒野无灯 http://ihacklog.com 2016-03-16
# ReMod: 寒霜 https://www.frost.net.cn 2018-12-06
#################################################
# 使用Token认证(推荐) 请去 https://www.dnspod.cn/console/user/security 获取
arToken="XXXX,XXXXXXXXXXXXXXXXXXXX"
# 使用邮箱和密码认证
arMail=""
arPass=""
# 获得外网ipv4地址
arIpAdress() {
local inter=$(nvram get wan0_ipaddr)
echo $inter
}
# 获得外网ipv6地址
arIpAdress_v6() {
local hostIP_v6=$(ip addr show ppp0 | grep inet6 | sed 's/\/.*//g' | awk '{print $2}' | grep 2408)
while [[ -z $hostIP_v6 ]]
do
sleep 60
hostIP_v6=$(ip addr show ppp0 | grep inet6 | sed 's/\/.*//g' | awk '{print $2}' | grep 2408)
done
echo $hostIP_v6
}
# 读取接口数据
# 参数: 接口类型 Post数据
arApiPost() {
local agent="AnripDdns/5.07(mail@anrip.com)"
local inter="https://dnsapi.cn/${1:?'Info.Version'}"
if [ "x${arToken}" = "x" ]; then # undefine token
local param="login_email=${arMail}&login_password=${arPass}&format=json&${2}"
else
local param="login_token=${arToken}&format=json&${2}"
fi
wget --quiet --no-check-certificate --output-document=- --user-agent=$agent --post-data $param $inter
}
# 获得ID与初步数据
getIDs() {
domainID=$(arApiPost "Domain.Info" "domain=${domain}"|sed 's/.\+{"id":"\([0-9]\+\)".\+/\1/')
returnDatav4=$(arApiPost "Record.List" "domain_id=${domainID}&sub_domain=${sub_domain}&record_type=A")
returnDatav6=$(arApiPost "Record.List" "domain_id=${domainID}&sub_domain=${sub_domain}&record_type=AAAA")
}
# 查询域名地址
# 参数: 待查询域名
arNslookup() {
local lastIP=$(echo $returnDatav4|grep -Eo "(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}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|))(%.+)?\s*")
echo $lastIP_v6
}
# 更新ipv4记录信息
arDdnsUpdate() {
local recordID recordRS recordCD
# 获取记录ID
recordID=$(echo $returnDatav4|sed 's/.\+\[{"id":"\([0-9]\+\)".\+/\1/')
# 更新记录IP
recordRS=$(arApiPost "Record.Ddns" "domain_id=${domainID}&record_id=${recordID}&sub_domain=${sub_domain}&record_line=默认")
recordCD=$(echo $recordRS | sed 's/.\+{"code":"\([0-9]\+\)".\+/\1/')
# 输出记录IP
if [ "$recordCD" == "1" ]; then
echo $recordRS | sed 's/.\+,"value":"\([0-9\.]\+\)".\+/\1/'
return 0
fi
# 输出错误信息
echo $recordRS | sed 's/.\+,"message":"\([^"]\+\)".\+/\1/'
return 1
}
# 更新ipv6记录信息
arDdnsUpdate_v6() {
local hostIP_v6=${1}
local recordID recordRS recordCD
recordID=$(echo $returnDatav6|sed 's/.\+\[{"id":"\([0-9]\+\)".\+/\1/')
# 更新记录IP
recordRS=$(arApiPost "Record.Modify" "domain_id=${domainID}&record_id=${recordID}&sub_domain=${subdomain}&value=${hostIP_v6}&record_type=AAAA&record_line=默认")
recordCD=$(echo -E $recordRS | sed 's/.\+{"code":"\([0-9]\+\)".\+/\1/')
# 输出记录IP
if [ "$recordCD" == "1" ]; then
echo "success"
return 0
fi
# 输出错误信息
echo $recordRS | sed 's/.\+,"code":"\([0-9\.]\+\)".\+/\1/'
echo $recordRS | sed 's/.\+,"message":"\([^"]\+\)".\+/\1/'
return 1
}
# 动态检查更新
# 参数: 主域名 子域名
arDdnsCheck() {
# 全局变量
domain=$1
subdomain=$2
getIDs
local postRS hostIP lastIP postRS_v6 hostIP_v6 lastIP_v6
# ipv4
hostIP=$(arIpAdress)
echo "hostIP: ${hostIP}"
lastIP=$(arNslookup)
echo "lastIP: ${lastIP}"
if [ "$lastIP" != "$hostIP" ]; then
arDdnsUpdate
if [ $? -ne 0 ]; then
/sbin/ddns_custom_updated 1
else
/sbin/ddns_custom_updated 0
fi
else
/sbin/ddns_custom_updated 1
fi
# ipv6
hostIP_v6=$(arIpAdress_v6)
echo "hostIP_v6: ${hostIP_v6}"
lastIP_v6=$(arNslookup_v6)
echo "lastIP_v6: ${lastIP_v6}"
if [ "$lastIP_v6" != "$hostIP_v6" ]; then
arDdnsUpdate_v6 ${hostIP_v6}
fi
}
###################################################
# 检查更新域名
# 指定 domain subdomain
arDdnsCheck "frostfall.cn" "" |
|