我有三种设备,synology openwrt raspberry
1,首先,我在openwrt上实现, 这有分为二种方法,其实也是一种方法, 1, 下载ddns-scripts_aliyun_1.0.0-1_all.ipk这个安装包,直接安装,在app-ddns里面就可以看到阿里去的服务商了, 然后输入用户名,密码就可以了。
2, 使用自定义的脚本也行,
先安装python库
pip3
install
aliyunsdkcore
pip3
install
aliyun-python-sdk-alidns
ddns.py
修改自己的ID KEY,主机名。
执行 python3 ddns.py &
在群晖上可以使用2种方法,1, docker, 这个我没有试。 2是使用脚本,我个我试了,比较方便, 网上下载了一个aliyun.sh, 加到群晖的自定义任务里,10分钟运行一次,就可以了,非常简单方便易行。
- www:解析后的域名为 www.1756260160.com
sh aliyun.sh <id> <secret> <主域名> <主机记录>
sh /var/services/homes/wandersky/aliddns/aliyun.sh L******************74 V0********************CMi **********.com www
脚本命令格式如上。
#!/bin/sh
set -e
if [ $1 ]; then
ApiId=$1
fi
if [ $2 ]; then
ApiKey=$2
fi
if [ $3 ]; then
Domain=$3
fi
if [ -z "$ApiId" -o -z "$ApiKey" -o -z "$Domain" ]; then
echo "参数缺失"
exit 1
fi
if [ $4 ]; then
SubDomain=$4
fi
if [ -z "$SubDomain" ]; then
SubDomain="@"
fi
Nonce=$(date -u "+%N") # 有bug?
Timestamp=$(date -u "+%Y-%m-%dT%H%%3A%M%%3A%SZ") # SB 阿里云, 什么鬼时间格式
Nonce=$Timestamp
urlencode() {
local raw="$1";
local len="${#raw}"
local encoded=""
for i in `seq 1 $len`; do
local j=$((i+1))
local c=$(echo $raw | cut -c$i-$i)
case $c in [a-zA-Z0-9.~_-]) ;;
*)
c=$(printf '%%%02X' "'$c") ;;
esac
encoded="$encoded$c"
done
echo $encoded
}
# $1 = query string
getSignature() {
local encodedQuery=$(urlencode $1)
local message="GET&%2F&$encodedQuery"
local sig=$(echo -n "$message" | openssl dgst -sha1 -hmac "$ApiKey&" -binary | openssl base64)
echo $(urlencode $sig)
}
sendRequest() {
local sig=$(getSignature $1)
local result=$(wget -qO- –no-check-certificate –content-on-error "https://alidns.aliyuncs.com?$1&Signature=$sig")
echo $result
}
getRecordId() {
echo "获取 $SubDomain.$Domain 的 IP…" >&2
local queryString="AccessKeyId=$ApiId&Action=DescribeSubDomainRecords&Format=JSON&SignatureMethod=HMAC-SHA1&SignatureNonce=$Nonce&SignatureVersion=1.0&SubDomain=$SubDomain.$Domain&Timestamp=$Timestamp&Type=A&Version=2015-01-09"
local result=$(sendRequest "$queryString")
local code=$(echo $result | sed 's/.*,"Code":"\([A-z]*\)",.*/\1/')
local recordId=$(echo $result | sed 's/.*,"RecordId":"\([0-9]*\)",.*/\1/')
if [ "$code" = "$result" ] && [ ! "$recordId" = "$result" ]; then
local ip=$(echo $result | sed 's/.*,"Value":"\([0-9\.]*\)",.*/\1/')
if [ "$ip" == "$NewIP" ]; then
echo "IP 无变化, 退出脚本…" >&2
echo "quit"
else
echo $recordId
fi
else
echo "null"
fi
}
# $1 = record ID, $2 = new IP
updateRecord() {
local queryString="AccessKeyId=$ApiId&Action=UpdateDomainRecord&DomainName=$Domain&Format=JSON&RR=$SubDomain&RecordId=$1&SignatureMethod=HMAC-SHA1&SignatureNonce=$Nonce&SignatureVersion=1.0&Timestamp=$Timestamp&Type=A&Value=$2&Version=2015-01-09"
local result=$(sendRequest $queryString)
local code=$(echo $result | sed 's/.*,"Code":"\([A-z]*\)",.*/\1/')
if [ "$code" = "$result" ]; then
echo "$SubDomain.$Domain 已指向 $NewIP." >&2
else
echo "更新失败." >&2
echo $result >&2
fi
}
# $1 = new IP
addRecord() {
local queryString="AccessKeyId=$ApiId&Action=AddDomainRecord&DomainName=$Domain&Format=JSON&RR=$SubDomain&SignatureMethod=HMAC-SHA1&SignatureNonce=$Nonce&SignatureVersion=1.0&Timestamp=$Timestamp&Type=A&Value=$1&Version=2015-01-09"
local result=$(sendRequest $queryString)
local code=$(echo $result | sed 's/.*,"Code":"\([A-z]*\)",.*/\1/')
if [ "$code" = "$result" ]; then
echo "$SubDomain.$Domain 已指向 $NewIP." >&2
else
echo "添加失败." >&2
echo $result >&2
fi
}
# Get new IP address
echo "获取当前 IP…"
NewIP=$(wget -qO- –no-check-certificate "http://members.3322.org/dyndns/getip")
echo "当前 IP 为 $NewIP."
# Get record ID of sub domain
recordId=$(getRecordId)
if [ ! "$recordId" = "quit" ]; then
if [ "$recordId" = "null" ]; then
echo "域名记录不存在, 添加 $SubDomain.$Domain 至 $NewIP…"
addRecord $NewIP
else
echo "域名记录已存在, 更新 $SubDomain.$Domain 至 $NewIP…"
updateRecord $recordId $NewIP
fi
fi
原文链接:https://blog.csdn.net/wandersky0822/article/details/112724046?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168449620216800192241364%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=168449620216800192241364&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-18-112724046-null-null.blog_rank_default&utm_term=NAS%E3%80%81%E7%BE%A4%E6%99%96%E3%80%81%E9%98%BF%E9%87%8C%E4%BA%91%E3%80%81%E5%9F%9F%E5%90%8D%E8%A7%A3%E6%9E%90%E3%80%81%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F%E3%80%81ipv6%E3%80%81ddns%E3%80%81%E8%BD%BB%E9%87%8F%E7%BA%A7%E4%BA%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E3%80%81%E9%93%81%E5%A8%81%E9%A9%AC%E3%80%81%E5%A8%81%E8%81%94%E9%80%9A%E3%80%81DSM%E3%80%81DSM6.0%E3%80%81%E7%BE%A4%E6%99%96nas%E3%80%81%E4%BA%91%E6%9C%8D%E5%8A%A1%E5%99%A8%E3%80%81%E8%9C%97%E7%89%9B%E6%98%9F%E9%99%85%E3%80%81%E9%BB%91%E7%BE%A4%E6%99%96%E3%80%81docker%E3%80%81%E5%AE%B9%E5%99%A8%E9%95%9C%E5%83%8F%E3%80%81%E5%9F%9F%E5%90%8D%E6%B3%A8%E5%86%8C%E3%80%81%E5%AE%9D%E5%A1%94%E3%80%81%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86%E3%80%81nginx%E3%80%81frp%E3%80%81%E5%8A%A8%E6%80%81%E5%9F%9F%E5%90%8D%E8%A7%A3%E6%9E%90