#!/bin/bash
#########################################################################
# File Name: dnspodDDNS.sh
# Author: LookBack
# Email: 393411264@qq.com
# Created Time: 2015年03月28日 星期六 23时19分00秒
#########################################################################
#
# 利用DNSPOD官方提供的API,自动更新域名解析记录,
# 在Netgear 4300 openwrt和CentOS 6系统上测试完美执行,
# 在其他硬件的openwrt上应该也是可以执行的,
# 非openwrt系统的不保证脚本的兼容性。
# 如有任何BUG 请联系我 393411264@qq.com
#
#########################################################################
#=======================下方是需要修改的参数信息=============================================
#DNSPOD 帐号
DNSPODID="393411264@qq.com"
#DNSPOD 密码
DNSPODPW="www.05hd.com"
#DNSPOD 主域名
DNSPODPD="05h.com"
#DNSPOD 二级域名,可以是多个,如下设置DNSPODSDNM[0-n]。有几个就写几个,把多余的删除
DNSPODSDNM[0]="openwrt141"
DNSPODSDNM[1]="openwrt"
DNSPODSDNM[2]="netgear111"
#=========================下方内容如果不懂请勿修改============================================
CURL=$(which curl)
GetID() {
$CURL -X POST https://dnsapi.cn/Domain.List -ks -d "login_email=$1&login_password=$2&format=json" | \
awk -F',' '{for(i=1;i<=NF;i++) if($i~/"id":[0-9]+/ || $i~/"name":".*"/) {print $i}}' | \
awk '{if (NR%2==0){print $0} else {printf"%s ",$0}}' | sed 's/"domains":\[//' | \
awk -F':| |"' '{print $9,$4}' > /tmp/dnspodDomain
while read line; do
if [ "$(awk '{print$1}' <<< $line)A" = "${DNSPODPD}A" ]; then
DNSPODPDID=$(awk '{print$2}' <<< $line)
break
fi
done < /tmp/dnspodDomain
$CURL -X POST https://dnsapi.cn/Record.List -ks -d "login_email=$1&login_password=$2&format=json&domain_id=$DNSPODPDID" | \
sed 's/.*},"records":\[//' | awk -F',' '{for(i=1;i<=NF;i++) if($i~/"id":"[0-9]+"/ || $i~/"name":".*"/) {print $i}}' | \
awk '{if (NR%2==0){print $0} else {printf"%s ",$0}}' | awk -F'"' '{print $8,$4}' > /tmp/dnspodRecord
while read line; do
for i in `seq 1 ${#DNSPODSDNM[@]}`; do
i=$[$i-1]
[ "$(awk '{print$1}' <<< $line)A" = "${DNSPODSDNM[$i]}A" ] && DNSPODSDID[$i]=$(awk '{print$2}' <<< $line) && break
done
done < /tmp/dnspodRecord
}
StartRun() {
WanIP=$($CURL -s ip.cn | grep -Eo '([0-9]{1,3}[\.]){3}[0-9]{1,3}')
DNSPODIP=$($CURL -X POST https://dnsapi.cn/Record.Info -ks -d \
"login_email=$1&login_password=$2&format=json&domain_id=$3&record_id=${DNSPODSDID[0]}" | \
grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}')
#awk -F':|,|"' '{for(i=1;i<=NF;i++)if($i~/^([0-9]{1,3}\.){3}[0-9]{1,3}$/){print$i;break}}')
[ -z $WanIP ] && exit 1
[ -z $DNSPODIP ] && exit 1
:> /tmp/dnspodDomain && :>/tmp/dnspodRecord
}
UploadIP() {
[ "${3}A" != "${4}A" ]
if [ "A$?" = "A0" ]; then
for i in `seq 1 ${#DNSPODSDNM[@]}`; do
i=$[$i-1]
$CURL -X POST https://dnsapi.cn/Record.Modify -ks -d \
"login_email=$1&login_password=$2&format=json&domain_id=$5\
&record_id= ${DNSPODSDID[$i]}&sub_domain=${DNSPODSDNM[$i]}&value=$3\
&record_type=A&record_line=默认" &>/dev/null
echo "$(date "+%F %H:%M:%S") Domain ${DNSPODSDNM[$i]}.$DNSPODPD A Record Update To $3" >> /var/log/dnspod.log
done
fi
}
RUN() {
GetID $1 $2
StartRun $1 $2 $DNSPODPDID
UploadIP $1 $2 ${WanIP} ${DNSPODIP} $DNSPODPDID ${DNSPODSDID[*]}
}
RUN $DNSPODID $DNSPODPW $DNSPODPDID