阿里云DNS api接口 shell 更改DNS解析

Posted Elvin笔记分享

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了阿里云DNS api接口 shell 更改DNS解析相关的知识,希望对你有一定的参考价值。

可定时任务检查域名解析,调用alidns.sh更新DNS解析

#!/bin/bash
# alidns.sh
#https://www.cnblogs.com/elvi/p/11663910.html
#阿里云DNS api接口 shell 更改DNS解析

##############################
which dig &>/dev/null || { yum install -y bind-utils ; } || { echo "need to install dig";exit 1; }

##配置
host="abcd" #主机名
domain="qq.com" #域名
ak="LTAI4FoDtp4y7ENqxxxxxxxxxx"  #阿里云AccessKey ID
sk="lVNCxCVGciaJqUxxxxxxxxxxxx&"  #阿里云Access Key Secret  后面多个 &
timestamp=`date -u +"%Y-%m-%dT%H:%M:%SZ"`

##############################
#hash签名使用
urlencode1() {
    local length="${#1}"
    i=0
    out=""
    for i in $(awk "BEGIN { for ( i=0; i<$length; i++ ) { print i; } }")
    do
        local c="${1:$i:1}"
        case $c in
            [a-zA-Z0-9.~\'&\'=_-]) out="$out$c" ;;
            *) out="$out`printf \'%%%02X\' "\'$c"`" ;;
        esac
        i=$(($i + 1))
     done
     echo -n $out
}
urlencode2() {
    local length="${#1}"
    i=0
    out=""
    for i in $(awk "BEGIN { for ( i=0; i<$length; i++ ) { print i; } }")
    do
        local c="${1:$i:1}"
        case $c in
            [a-zA-Z0-9.~_-]) out="$out$c" ;;
            *) out="$out`printf \'%%%02X\' "\'$c"`" ;;
        esac
        i=$(($i + 1))
     done
     echo -n $out
}

##############################
#函数

send_request() {   
args="AccessKeyId=$ak&Action=$1&Format=json&$2&Version=2015-01-09"
StringToSign1="$(urlencode1 $args)"
StringToSign2="GET&%2F&$(urlencode2 $StringToSign1)"
hash=$(urlencode2 $(echo -n "$StringToSign2" | openssl dgst -sha1 -hmac $sk -binary | openssl base64))
RESULT=$(curl -k -s "https://alidns.aliyuncs.com/?$args&Signature=$hash")  ## 2> /dev/null)
echo $RESULT
}
query_recordid() {
 if [ "$host" = "@" ]; then 
echo `send_request "DescribeSubDomainRecords" "SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&SubDomain=$domain&Timestamp=$timestamp"`
 else
echo `send_request "DescribeSubDomainRecords" "SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&SubDomain=$host.$domain&Timestamp=$timestamp"`
 fi
}
update_record() {
    echo `send_request "UpdateDomainRecord" "RR=$host&RecordId=$1&SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&Timestamp=$timestamp&Type=A&Value=$ip"`
}
add_record() {
    echo `send_request "AddDomainRecord&DomainName=$domain" "RR=$host&SignatureMethod=HMAC-SHA1&SignatureNonce=$timestamp&SignatureVersion=1.0&Timestamp=$timestamp&Type=A&Value=$ip"`
}

add_host() {
#echo "新增解析"
RESULT=`add_record`
record_id=$(echo $RESULT | grep -o "RecordId\\":\\"[0-9]*\\"" | grep -o "[0-9]*")
[ "$record_id" = "" ] && { echo "$host.$domain  $ip  AddError";exit 1; }
echo "$host.$domain  $ip  AddHost $(date +\'%F %T\')"
}

up_host() {
#echo "更新解析"
#查询RecordId
RESULT=`query_recordid`
record_id=$(echo $RESULT | grep -o "RecordId\\":\\"[0-9]*" | grep -o "[0-9]*")
[ "$record_id" = "" ] && { echo "get record_id error";exit 1; }
#更新
RESULT=`update_record $record_id`
record_id=$(echo $RESULT | grep -o "RecordId\\":\\"[0-9]*\\"" | grep -o "[0-9]*")
[ "$record_id" = "" ] && { echo "$host.$domain  $ip  UpError";exit 1; }
echo "$host.$domain    $ip  UpHost $(date +\'%F %T\')"
}

##############################
#获取本地外网ip并更新dns

# #ip
# UA="Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 6.3; blog.elven.vip)"
# ip=$(curl -s -A "$UA" http://ipv4.icanhazip.com)
# ip_dns=$(dig @dns23.hichina.com ${host}.${domain} A +short)
# # echo ip  $ip
# # echo ip_dns  $ip_dns
# [ -n "$ip" ] || { echo "ip error"; }

# if [ -n "$ip_dns" -a "$ip_dns" = "$ip" ];then
    # echo "$host.$domain    $ip_dns"
# elif [ "$ip_dns" = "" ];then
    # add_host
# else
    # up_host
# fi

##############################
#传参  主机 域名 ip

alidns() {
# var:  host domain ip
host="$1"
domain=$2
ip=$3
ip_dns=$(dig @dns23.hichina.com ${host}.${domain} A +short)

if [ $# -eq 2 ];then
    if [ -n "$ip_dns" ];then
        echo "$host.$domain    $ip_dns"
    else
        echo "$host.$domain  no found"
    fi
elif [ $# -eq 3 ];then
    if [ "$ip" = "$ip_dns" ];then
        echo "$host.$domain    $ip_dns"
    else
        [ `echo $ip |grep -oE \'[0-9]{0,3}\\.[0-9]{0,3}\\.[0-9]{0,3}\\.[0-9]{0,3}\' |wc -l` -eq 0 ] && { echo "ip $ip error";exit 1; }
        [ "$ip_dns" = "" ] && { add_host ; } || { up_host ; }
    fi
else
    echo "eg:$0  www  abc.com  192.168.18.18"
fi
}

alidns "$@"

# 使用实例 增加或更新  *.abc.elven.vip ,通配符使用\'\'
# bash alidns.sh  \'*.abc\'  elven.vip  192.168.18.18
##############################
# api https://help.aliyun.com/knowledge_detail/39863.html
##############################

以上是关于阿里云DNS api接口 shell 更改DNS解析的主要内容,如果未能解决你的问题,请参考以下文章

基于阿里云 DNS API 实现的 DDNS 工具

使用脚本更新阿里云DNS记录

阿里云镜像使用阿里巴巴DNS镜像源——DNS配置教程

给阿里云 VPC 中的 Ubuntu ECS 配置自定义 DNS 服务器

python 改变阿里云dns记录

阿里云域名配置与解析