阿里云DNS api接口 shell 更改DNS解析
2021-01-30 07:15
标签:ipv4 xxx dtp dde action zip des rem signature 可定时任务检查域名解析,调用alidns.sh更新DNS解析 阿里云DNS api接口 shell 更改DNS解析 标签:ipv4 xxx dtp dde action zip des rem signature 原文地址:https://www.cnblogs.com/elvi/p/11663910.html
#!/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 /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解析
文章链接:http://soscw.com/index.php/essay/49037.html