ddns更新IPv6地址

适用群晖需要设置ipv6的ddns时,可以适用下面的脚本定时更新解析地址(计划任务)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/bin/bash
noip_user="example@sina.com"
noip_pass="xxxxxxx"
noip_domain='example.ddns.net'

noip_url='https://dynupdate.no-ip.com/nic/update'

ipv6=`ip addr show eth0 | grep "inet6.*global" | awk '{print $2}' | awk -F"/" '{print $1}'`
current_ipv6=`nslookup -query=AAAA $noip_domain 2>&1`
current_ipv6=`echo "$current_ipv6" | grep 'Address: ' | tail -n1 | awk '{print $NF}'`

echo '-----------------' $ipv6
echo '-----------------' $current_ipv6

noip_data="?hostname=$noip_domain&myip=$ipv6"

noip_head=$(echo $noip_user:$noip_pass | base64)

if [ "$ipv6" = "$current_ipv6" ]
then
    echo '-----------------' "Skipping"
else
    curl -H "Authorization: Basic $noip_head" -s "$noip_url$noip_data"
    echo '-----------------' "Updated record $ipv6"
fi

阿里云更新脚本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash
aliddnsipv6_ak="LTAIxxxxxxxxSDebv" 
aliddnsipv6_sk="3JSixxxxxxxxxxeEWTLxZYnW3"
aliddnsipv6_name1='nas'
aliddnsipv6_domain='xxx.fun'
aliddnsipv6_ttl="600"
aliddnsipv6_name=$aliddnsipv6_name1.$aliddnsipv6_domain

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



ipv6=`ip addr show eth0 | grep "inet6.*global" | awk '{print $2}' | awk -F"/" '{print $1}'`
current_ipv6=`nslookup -query=AAAA $aliddnsipv6_name 2>&1`
current_ipv6=`echo "$current_ipv6" | grep 'Address: ' | tail -n1 | awk '{print $NF}'`

echo '-----------------' $ipv6
echo '-----------------' $current_ipv6


urlencode() {
    # urlencode <string>
    out=""
    while read -n1 c
    do
        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)"
}

aliddnsipv6_record_id=`query_recordid | get_recordid`
echo '-----------------' $aliddnsipv6_record_id

if [ "$ipv6" = "$current_ipv6" ]
then
	echo '-----------------' "Skipping"
else
    update_record $aliddnsipv6_record_id
    echo '-----------------' "Updated record $ipv6"
fi 
comments powered by Disqus