使用阿里云DNS自建DDNS

之前一直使用的cf的dns和api自建ddns,发现有时候更新不及时,后面用了阿里云dns,基本10分钟就能全部解析。直接放代码,复制按照里面的注释修改信息即可。

在使用前,先安装nslookup。这里是Debian10的安装方法。

apt-get install dnsutils

定时运行

先给脚本权限

chmod +x aliyun-ddns.sh

然后输入

crontab -e

选择一个适合自己的

no crontab for root - using an empty one

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.basic
  3. /usr/bin/vim.tiny

Choose 1-3 [1]: 

插入

*/1 * * * * /root/aliyun-ddns.sh >/dev/null 2>&1

这个意思是每分钟运行一次这个脚本。

下面是脚本。复制粘贴使用即可。

#!/bin/bash
set -e

#================================================================================================================#
# 功能:用于更新阿里云DNS解析IP,实现DDNS功能
#
# 在 http://www.gebi1.com/forum.php?mod=viewthread&tid=287344&page=1&_dsign=8f94f74c 提供的脚本文件基础上修改的。
# ghui, modified 12/2/2019
# 在 N1 debian Buster with Armbian Linux 5.3.0-aml-g12 手动执行/定时任务(crontab)执行测试通过
#================================================================================================================#
#
# 使用方法:
#
# 方法1. 外部参数
# 修改源码,将对应参数 修改为$1,$2,$3,$4,$5,$6 
# aliddns.sh <aliddns_ak> <aliddns_sk> <aliddns_subdomain> <aliddns_domain> <aliddns_iptype> <aliddns_ttl>
# 示例(A 代表 IPv4,AAAA 代表 IPv6): 
# 执行:aliddns.sh "xxxx" "xxx" "test" "mydomain.site" "A" 600
# 执行:aliddns.sh "xxxx" "xxx" "test" "mydomain.site" "AAAA" 600
#
# 方法2. 内部参数
# 修改源码,将$1,$2,$3,$4,$5,$6 替换为对应参数
# 
# 示例: 
# aliddns_ak="xxxx"
# aliddns_sk="xxx"
# aliddns_subdomain="test"
# aliddns_domain="mydomain.site"
# aliddns_iptype="A"
# aliddns_ttl=600 
# 执行:aliddns.sh
#
#================================================================================================================#

#--------------------------------------------------------------
# 参数
#
# (*)阿里云 AccessKeyId 
aliddns_ak=
# (*)阿里云 AccessKeySecret 
aliddns_sk=

# (*)域名:test.mydomain.com 请不要填写@,否则报错。
无法使用根域名作为DDNS。
aliddns_subdomain=  #'test'
aliddns_domain= #'mydomain.com'

# (*)ip地址类型:'A' 或 'AAAA',代表ipv4 和 ipv6
aliddns_iptype=A # 'A' 或 'AAAA',代表ipv4 和 ipv6

# TTL 默认10分钟 = 600秒 
aliddns_ttl=600 #"600"

#--------------------------------------------------------------

machine_ip=""
ddns_ip=""
aliddns_record_id=""

if [ "$aliddns_subdomain" = "@" ]
then
  aliddns_name=$aliddns_domain
else
  aliddns_name=$aliddns_subdomain.$aliddns_domain
fi

now=`date`
echo "**************************************************"
echo "$now"
echo "$aliddns_name"

function getMachine_IPv4() {
    echo $(/usr/bin/wget -qO- -t1 -T2 http://ip.3322.net)
}

function getMachine_IPv6() {
    ipv6=`ip addr | grep "inet6.*global" | grep -v "deprecated" | awk '{print $2}' | awk -F"/" '{print $1}' | sed -n '1,1p'`
    echo $ipv6
}

function getDDNS_IP() {
    current_ip=`nslookup -query=$aliddns_iptype $aliddns_name | grep "Address" | grep -v "#53" | awk '{print $2}'`
    echo $current_ip
}

function 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
}

function enc() {
    echo -n "$1" | urlencode
}

function send_request() {
    local args="AccessKeyId=$aliddns_ak&Action=$1&Format=json&$2&Version=2015-01-09"
    local hash=$(echo -n "GET&%2F&$(enc "$args")" | openssl dgst -sha1 -hmac "$aliddns_sk&" -binary | openssl base64)
    curl -s "http://alidns.aliyuncs.com/?$args&Signature=$(enc "$hash")"
}

function get_recordid() {
    grep -Eo '"RecordId":"[0-9]+"' | cut -d':' -f2 | tr -d '"'
}

function query_recordid() {
    send_request "DescribeSubDomainRecords" "SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&SubDomain=$aliddns_name&Timestamp=$timestamp&Type=$aliddns_iptype"
}

function update_record() {
    send_request "UpdateDomainRecord" "RR=$aliddns_subdomain&RecordId=$1&SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&TTL=$aliddns_ttl&Timestamp=$timestamp&Type=$aliddns_iptype&Value=$(enc $machine_ip)"
}

function add_record() {
    send_request "AddDomainRecord&DomainName=$aliddns_domain" "RR=$aliddns_subdomain&SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&TTL=$aliddns_ttl&Timestamp=$timestamp&Type=$aliddns_iptype&Value=$(enc $machine_ip)"
}

if [ "$aliddns_iptype" = 'A' ]
then
    echo "ddns is IPv4."

    machine_ip=`echo "$(getMachine_IPv4)"`
    echo "machine_ip = $machine_ip"

    aliddns_record_id=$aliddnsipv4_record_id
else
    echo "ddns is IPv6."

    machine_ip=`echo "$(getMachine_IPv6)"`
    echo "machine_ip = $machine_ip"

    aliddns_record_id=$aliddnsipv6_record_id
fi

ddns_ip=`echo "$(getDDNS_IP)"`
echo "ddns_ip = $ddns_ip"

if [ "$machine_ip" = "" ]
then
    echo "machine_ip is empty!"
    exit 0
fi

if [ "$machine_ip" = "$ddns_ip" ]
then
    echo "skipping\n"
    exit 1
fi

echo "start update..."

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

if [ "$aliddns_record_id" = "" ]
then
    aliddns_record_id=`query_recordid | get_recordid`
    echo "----------------" $aliddns_record_id "\n"
    
    if [ "$aliddns_iptype" = 'A' ]
    then
        aliddnsipv4_record_id=$aliddns_record_id
    else
        aliddnsipv6_record_id=$aliddns_record_id
    fi
fi

#add support */%2A and @/%40 record
if [ "$aliddns_record_id" = "" ]
then
    echo "add record starting"

    aliddns_record_id=`add_record | get_recordid`

    if [ "$aliddns_record_id" = "" ]
    then
        echo "aliddns_record_id is empty. \n"
    else
        if [ "$aliddns_iptype" = 'A' ]
        then
            aliddnsipv4_record_id=$aliddns_record_id
        else
            aliddnsipv6_record_id=$aliddns_record_id
        fi

        echo "added record $aliddns_record_id \n"
    fi
else
    echo "update record starting"
    update_record $aliddns_record_id
    echo "updated record $aliddns_record_id \n"
fi