aliddns ipv6_群晖使用阿里云DDNS(ipv4和ipv6)

复制,写成sh脚本,添加进群晖定时任务即可。

ipv4

注意前四个需要自己填入,还有获取ip那里要注意网络接口,我的是ppp0。

#!/bin/sh

ApiId="***************"

ApiKey="***************"

Domain='************'

SubDomain='********' #这四个自己写

Nonce=$(date -u "+%N")

Timestamp=$(date -u "+%Y-%m-%dT%H%%3A%M%%3A%SZ")

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

else

echo "null"

# $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

# $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

# Get new IP address

echo "获取当前 IP…"

NewIP=`ip addr show ppp0 | grep "inet" | awk '{print $2}' | awk -F"/" '{print $1}' `

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

ipv6

同样前四个自己填,和上边的类似。网络接口也同理,可以进群晖ssh,用ip addr自己查接口名称。

#!/bin/sh

aliddnsipv6_ak="**************"

aliddnsipv6_sk="**********************"

aliddnsipv6_name1='****************'

aliddnsipv6_domain='**************'

aliddnsipv6_ttl="600"

if [ "$aliddnsipv6_name1" = "@" ]

then

aliddnsipv6_name=$aliddnsipv6_domain

else

aliddnsipv6_name=$aliddnsipv6_name1.$aliddnsipv6_domain

now=`date`

die () {

echo $1

ipv6s=`ip addr show eth0 | grep "inet6.2409" | awk '{print $2}' | awk -F"/" '{print $1}'` || die "$ipv6"

for ipv6 in $ipv6s

#ipv6 = $ipv6

break

done

echo $ipv6

current_ipv6=`nslookup -query=AAAA $aliddnsipv6_name 2>&1`

#echo $current_ipv6

current_ipv6=`echo "$current_ipv6" | grep 'Address: ' | tail -n1 | awk '{print $NF}'`

echo $current_ipv6

if [ "$?" -eq "0" ]

then

current_ipv6=`echo "$current_ipv6" | grep 'Address: ' | tail -n1 | awk '{print $NF}'`

echo $current_ipv6

if [ "$ipv6" = "$current_ipv6" ]

then

echo "skipping"

# fix when A record removed by manual dns is always update error

else

unset aliddnsipv6_record_id

timestamp=`date -u "+%Y-%m-%dT%H%%3A%M%%3A%SZ"`

urlencode() {

# urlencode

out=""

while read -n1 c

case $c in

[a-zA-Z0-9._-]) out="$out$c" ;;

*) out="$out`printf '%%%02X' "'$c"`" ;;

esac

done

echo -n $out

enc() {

echo -n "$1" | urlencode

send_request() {

local args="AccessKeyId=$aliddnsipv6_ak&Action=$1&Format=json&$2&Version=2015-01-09"

local hash=$(echo -n "GET&%2F&$(enc "$args")" | openssl dgst -sha1 -hmac "$aliddnsipv6_sk&" -binary | openssl base64)

curl -s "http://alidns.aliyuncs.com/?$args&Signature=$(enc "$hash")"

get_recordid() {

grep -Eo '"RecordId":"[0-9]+"' | cut -d':' -f2 | tr -d '"'

query_recordid() {

send_request "DescribeSubDomainRecords" "SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&SubDomain=$aliddnsipv6_name&Timestamp=$timestamp&Type=AAAA"

update_record() {

send_request "UpdateDomainRecord" "RR=$aliddnsipv6_name1&RecordId=$1&SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&TTL=$aliddnsipv6_ttl&Timestamp=$timestamp&Type=AAAA&Value=$(enc $ipv6)"

add_record() {

send_request "AddDomainRecord&DomainName=$aliddnsipv6_domain" "RR=$aliddnsipv6_name1&SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&TTL=$aliddnsipv6_ttl&Timestamp=$timestamp&Type=AAAA&Value=$(enc $ipv6)"

#add support */%2A and @/%40 record

if [ "$aliddnsipv6_record_id" = "" ]

then

aliddnsipv6_record_id=`query_recordid | get_recordid`

#echo '—————–' $aliddnsipv6_record_id

if [ "$aliddnsipv6_record_id" = "" ]

then

aliddnsipv6_record_id=`add_record | get_recordid`

echo "added record $aliddnsipv6_record_id"

else

update_record $aliddnsipv6_record_id

echo "updated record $aliddnsipv6_record_id"

原文链接:https://blog.csdn.net/weixin_39678163/article/details/111840955?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168466843816800197020850%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=168466843816800197020850&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~times_rank-15-111840955-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

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享