码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2019-10-12 22:52:51      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:ipv4   xxx   dtp   dde   action   zip   des   rem   signature   

可定时任务检查域名解析,调用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解析

标签:ipv4   xxx   dtp   dde   action   zip   des   rem   signature   

原文地址:https://www.cnblogs.com/elvi/p/11663910.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!