可定时任务检查域名解析,调用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 IDsk="lVNCxCVGciaJqUxxxxxxxxxxxx&" #阿里云Access Key Secret 后面多个 ×tamp=`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"` elseecho `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 "更新解析"#查询RecordIdRESULT=`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###############################传参 主机 域名 ipalidns() {# var: host domain iphost="$1"domain=$2ip=$3ip_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" fielif [ $# -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 ; } fielse 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##############################