Shell DDNS 腾讯云动态域名解析

103 阅读1分钟

基于旧版API编写

代码

# 1.get you ipv4 address at the cip.cc site
ip_v4=`curl cip.cc|grep -oP "(?<=IP\s:\s)[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"`
###################  INFO  #######################
login_token='密钥' ## 密钥格式: id,token
domain='一级域名'
subdomain='二级域名'
record_type="A"
record_line="默认"
value=$ip_v4
mx=1
ttl=600
##################################################

# 2.get you domian list
domainlist=`curl -X POST https://dnsapi.cn/Record.List -d "login_token="$login_token"&domain="$domain|grep -oP "(?=\"records\":).*(?=}]?)"`
records=(${domainlist//\{/ })
records_array=()
for ((i=1;i<${#records[*]};i++));
do
  resu=`expr $i % 2`
  if [ $resu -eq 0 ]; then
        records_array[$i]=${records[ `expr $i - 1` ]}${records[$i]}
  elif [ $resu -eq 1 ]; then
        records_array[$i]="Placeholder"
  fi
done

## 3.subdomain
sub_record_json=''
for((i=0;i<=${#records_array[*]};i++));
do
echo ${records_array[$i]}|grep "$subdomain"
        if [ $? -eq 0 ]; then
        sub_record_json=${records_array[$i]}
        fi
        done

# 3.1 update subdocmain ip
record_id=$(echo $sub_record_json | grep -oP "(?<=\:\")[0-9]{10,}(?=\"\,)")
subdomain_ip_change_res=` curl -X POST https://dnsapi.cn/Record.Modify -d "login_token="$login_token"&domain="$domain"&sub_domain="$subdomain"&record_id="$record_id"&record_type="$record_type"&record_line="$record_line"&value="$value"&mx {1-20}="$mx"&ttl="$ttl `
# echo -e $subdomain_ip_change_res

小结

  • 腾讯云新版API参数太多了,而且参数格式改来改去的,真叫一个乱。旧版的一样,找了半天格式还是在其他模板看见的,原文档还说啥弃用服了。