小米路由器2使用DNSPod的API做动态域名解析

一. 配置文件:

dnspod.conf:

ACCOUNT="ID"
PASSWORD="PASSWORD"
DOMAIN="ftp.abc.com"
RECORD_LINE="默认"

将配置文件替换成你自己的DNSPod账号密码和域名。

二. 脚本文件:

dnspod.sh

if [ "$#" != 1 ];then
    echo "param error."
    exit 0
fi
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=$(curl -s http://119.29.29.29/d?dn=mi.tanst.net)
    current_ip=$(curl -s icanhazip.com)

    echo $resolve_ip
    echo $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}"
    curl -k https://www.xdty.org/mail.php -X POST -d "event=ip($current_ip) changed&name=$SUBDOMAIN&email=$ACCOUNT"
}

main(){

    dnspod_load_config $1
    dnspod_is_record_updated
    dnspod_domain_get_id
    dnspod_update_record_ip
}

main $1

三. 运行:

复制dnspod.shdnspod.conf/etc/dnspod(任意目录)下
添加执行权限:

chmod +x dnspod.sh

然后运行

./dnspod.sh dnspod.conf

四. 添加定时任务:

注意将dnspod.sh文件权限设置为775

查看当前用户的crontab,输入 crontab -l
编辑crontab,输入 crontab -e
删除crontab,输入 crontab -r;
每天凌晨4点执行一次:

0 4 * * * /etc/dnspod/dnspod.sh /etc/dnspod/dnspod.conf

每隔1小时执行一次:

0 */1 * * * /etc/dnspod/dnspod.sh /etc/dnspod/dnspod.conf

注意:

路由器系统更新后需要重新添加定时任务

评论已关闭