ddwrt路由/linux动态解析ip(ddns)到dnspod配置
dnspod域名解析服务有提供出来api,可以通过curl命令与dnspod交互。需要在dnspod先绑定一个域名,并设置一个二级域名(A记录),如我的test.xdty.org指向到了127.0.0.1。而dnspod对动态域名的解析相同ip有一小时5次的限制,所以我只在ip改变后才会更新dns的解析。
使用busybox的ash解析sh脚本,内容如下:
1.保存脚本dnspod.sh和dnspod.conf到ddwrt /jffs/ddns路径下
可自行定义路径,但要保证在重启后还在。
使用ssh或putty登陆到路由后,运行如下命令
mkdir ddns
cd ddns
curl -k -s https://raw.githubusercontent.com/xdtianyu/scripts/master/ddns/dnspod.sh >dnspod.sh
chmod +x dnspod.sh
curl -k -s https://raw.githubusercontent.com/xdtianyu/scripts/master/ddns/dnspod.conf >dnspod.conf
修改dnspod.conf配置文件,其中domain为二级域名,account为dnspod的登陆邮箱,password为登陆密码。整个调用过程使用https加密post传输,不需要担心密码泄露。(我觉得dnspod直接使用密码做api调用没有namesilo的api key做的好)
2.本地运行
在命令行运行如下命令即可
由于使用了nslookup验证动态ip是否已经更新,所以可能需要30秒左右时间,稍等片刻。命令正常退出后可以登陆dnspod查看是不是已经成功更新了。
3.添加cron任务
在ddwrt web管理面板里 Cron 附加任务 添加如下内容即可
即每隔3分钟检验一次,如果ip有变动,脚本会运行更新。
附脚本内容如下:
dnspod.sh文件
# usage: ./dnspod.sh ddns.conf
ACCOUNT=""
PASSWORD=""
DOMAIN=""
SUBDOMAIN=""
RECORD_LINE=""
DOMAIN_ID=""
RECORD_LIST=""
i=0;
dnspod_load_config(){
cfg=$1;
content=`cat ${cfg}`;
ACCOUNT=`echo "${content}" |grep 'ACCOUNT'| sed 's/^ACCOUNT=["]\(.*\)["]/\1/'`;
PASSWORD=`echo "${content}" |grep 'PASSWORD'| sed 's/^PASSWORD=["]\(.*\)["]/\1/'`;
DOMAIN=`echo "${content}" |grep 'DOMAIN='| sed 's/^DOMAIN=["]\(.*\)["]/\1/'`;
RECORD_LINE=`echo "${content}" |grep 'RECORD_LINE'| sed 's/^RECORD_LINE=["]\(.*\)["]/\1/'`;
SUBDOMAIN=${DOMAIN%%.*}
DOMAIN=${DOMAIN#*.}
}
dnspod_is_record_updated(){
#resolve_ip=$(nslookup $SUBDOMAIN.$DOMAIN | tail -2 |grep Add | awk '{print $NF}')
resolve_ip=$(curl -s -k https://www.xdty.org/resolve.php -X POST -d "domain=$SUBDOMAIN.$DOMAIN")
current_ip=$(curl -s icanhazip.com)
echo $resolve_ip $current_ip
if [ "$resolve_ip" = "$current_ip" ]; then
echo "Record updated."
exit 0;
fi
}
dnspod_domain_get_id(){
options="login_email=${ACCOUNT}&login_password=${PASSWORD}";
out=$(curl -s -k https://dnsapi.cn/Domain.List -d ${options});
for line in $out;do
if [ $(echo $line|grep '<id>' |wc -l) != 0 ];then
DOMAIN_ID=${line%<*};
DOMAIN_ID=${DOMAIN_ID#*>};
#echo "domain id: $DOMAIN_ID";
fi
if [ $(echo $line|grep '<name>' |wc -l) != 0 ];then
DOMAIN_NAME=${line%<*};
DOMAIN_NAME=${DOMAIN_NAME#*>};
#echo "domain name: $DOMAIN_NAME";
if [ "$DOMAIN_NAME" = "$DOMAIN" ];then
break;
fi
fi
done
out=$(curl -s -k https://dnsapi.cn/Record.List -d "${options}&domain_id=${DOMAIN_ID}")
for line in $out;do
if [ $(echo $line|grep '<id>' |wc -l) != 0 ];then
RECORD_ID=${line%<*};
RECORD_ID=${RECORD_ID#*>};
#echo "record id: $RECORD_ID";
fi
if [ $(echo $line|grep '<name>' |wc -l) != 0 ];then
RECORD_NAME=${line%<*};
RECORD_NAME=${RECORD_NAME#*>};
#echo "record name: $RECORD_NAME";
if [ "$RECORD_NAME" = "$SUBDOMAIN" ];then
break;
fi
fi
done
echo "$RECORD_NAME:$RECORD_ID"
}
dnspod_update_record_ip(){
curl -k https://dnsapi.cn/Record.Ddns -d "login_email=${ACCOUNT}&login_password=${PASSWORD}&domain_id=${DOMAIN_ID}&record_id=${RECORD_ID}&sub_domain=${RECORD_NAME}&record_line=${RECORD_LINE}"
}
main(){
dnspod_load_config $1
dnspod_is_record_updated
dnspod_domain_get_id
dnspod_update_record_ip
}
main $1
dnspod.conf文件
PASSWORD="xxxxxxxxxx"
DOMAIN="xxxx.xxx.org"
RECORD_LINE="默认"
Tags: bash busybox ddns ddwrt dnspod dns解析 linux sh ubuntu
评论:19