爱就爱了

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6857|回复: 0

EdgeRouter 配置 dnspod 动态域名更新

[复制链接]
发表于 2018-8-17 16:22:14 | 显示全部楼层 |阅读模式
作者:mostone

测试路由器:UBNT EdgeRouter ER-X

路由器系统:EdgeRouter X v1.9.1.1

使用 Shell 脚本,配置为 pppoe 建立连接后被调用。

系统的 /etc/ppp/ip-up 脚本,会在 pppoe 连接建立后执行,并搜索 /config/scripts/ppp/ip-up.d/ 目录,执行目录下的所有脚本文件。

其中 /config/scripts 是路由器默认的配置目录,如果更新路由器固件,并不会删除其中的文件。

这款路由器支持 ssh 远程登录,可以使用 sftp 上传文件,放到 /config/scripts/ppp/ip-up.d/ 目录下即可,如果目录不存在,先建立目录层级。

脚本文件以下面方式运行(脚本文件名可以任意更改):

  (注:在使用之前,需要修改文件,设置正确的 ACCESS_TOKEN。参考:https://support.dnspod.cn/Kb/showarticle/tsid/227/

1.  $> /config/scripts/ppp/ip-up.d/ddnspod.sh -q mydomain.com www

    这用来查询 mydomain.com 在平台上的 domainId, www 的 recordId

2. $> /config/scripts/ppp/ip-up.d/ddnspod.sh --test 1111 2222 www 0 10.1.1.1

    这用来提交测试的 IP 地址, 1111 为上面取得的 domainId,2222 为上面取得的 recordId,www 为子域名(可以用 @ 来更新主域名),0 为 record_line_id,最后是要提交的新 IP 地址。

    record_line_id,如果是默认线路,值为 0,更多的可选值请参考:https://www.dnspod.cn/docs/domains.html#record-line

  1. "line_ids": {
  2.         "默认": 0,
  3.         "国内": "7=0",
  4.         "国外": "3=0",
  5.         "电信": "10=0",
  6.         "联通": "10=1",
  7.         "教育网": "10=2",
  8.         "移动": "10=3",
  9.         "百度": "90=0",
  10.         "谷歌": "90=1",
  11.         "搜搜": "90=4",
  12.         "有道": "90=2",
  13.         "必应": "90=3",
  14.         "搜狗": "90=5",
  15.         "奇虎": "90=6",
  16.         "搜索引擎": "80=0"
  17.     }
复制代码
    提交的线路 id 值应该与你的设置保持一致。
    如果 id 值中含有等号,请将 '=' 换成 '%3D',进行转义,如 "7%3D0"

3. 路由器重新建立 pppoe 连接时,自动被调用执行

    这种方式使用,需要修改文件的最后一行,设置由上面第一种方式取得的 domainId 和 recordId,如果有多个域名要更新,复制为多行,并修改相应的 domainId, recordId 即可。

    可以在 /var/log/ppp.log 中查看最后一次记录消息

ddnspod-ubnt.sh 代码:


  1. #!/bin/sh
  2. #################################################
  3. # MosDdns for EdgeRouter X v1.0
  4. # Dynamic DNS using DNSPod API
  5. # Original by mostone<mostone@hotmail.com>, http://www.anrip.com/ddnspod
  6. # Tested on EdgeRouter X v1.9.1.1
  7. #################################################

  8. # Usage:
  9. #  step 1. Set accessToken value at bellow.
  10. #  step 2. Input command in Shell: ddnspod-ubnt.sh -p domainName subdomainName
  11. #            this command will print domainId and recordId.
  12. #  step 3. Add updateDdns function call in the last of this file, replace your
  13. #            real domainId and recordId which get from step 1.
  14. #  step 4. copy this shell script file to ubnt router.
  15. #            path: /config/scripts/ppp/ip-up.d/
  16. #            this file will be call by /etc/ppp/ip-up.
  17. #
  18. #  Done.


  19. # Global Variables:
  20. # Combine your token Id and token together as follows
  21. ACCESS_TOKEN="11111,exxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx4"

  22. IS_PPP_UP=1
  23. if [ -z $PPP_LOCAL ]; then
  24.         IS_PPP_UP=0
  25. fi

  26. showMsg() {
  27.         echo $1
  28.         if [ $IS_PPP_UP -eq 1 ]; then
  29.                 echo $1 >> /var/log/ppp.log
  30.         fi
  31. }

  32. # arguments: apiInterface postParameters
  33. callApi() {
  34.     local agent="MosDdns/1.0(mostone@hotmail.com)"
  35.     local url="https://dnsapi.cn/${1:?'Info.Version'}"
  36.     local params="login_token=${ACCESS_TOKEN}&format=json&${2}"
  37.     curl --silent --request POST --user-agent $agent $url --data $params
  38. }

  39. # arguments: domainName subdomainName
  40. printDomainAndRecordIds() {
  41.     local response domainId recordId lastIp
  42.    
  43.     # Get domain Id
  44.     response=$(callApi "Domain.Info" "domain=${1}")
  45.     # echo "Domain.Info call respone: ${response}"
  46.     domainId=$(echo $response | sed 's/.*{"id":"\([0-9]*\)".*/\1/')
  47.     echo "    Domain[${1}] Id is:                ${domainId}"

  48.     # Get Record Id
  49.     response=$(callApi "Record.List" "domain_id=${domainId}&sub_domain=${2}")
  50.     # echo "Record.List call respone: ${response}"
  51.     recordId=$(echo $response | sed 's/.*\[{"id":"\([0-9]*\)".*/\1/')
  52.     echo "    Record[${2}] Id is:                ${recordId}"
  53.    
  54.     # Print last Ip
  55.     lastIp=$(echo $response | sed 's/.*,"value":"\([0-9\.]*\)".*/\1/')
  56.     echo "    Server side last Ip:                ${lastIp}"
  57. }

  58. # arguments: domainId recordId
  59. getLastIp() {
  60.     local response lastIp
  61.    
  62.     # get last Ip
  63.     response=$(callApi "Record.Info" "domain_id=${1}&record_id=${2}")
  64.     lastIp=$(echo $response | sed 's/.*,"value":"\([0-9\.]*\)".*/\1/')
  65.    
  66.     # validate Ip
  67.     case "$lastIp" in
  68.       [1-9][0-9]*)
  69.         echo $lastIp
  70.         return 0
  71.         ;;
  72.       *)
  73.         echo $response | sed 's/.*,"message":"\([^"]*\)".*/\1/'
  74.         return 1
  75.         ;;
  76.     esac
  77. }

  78. # arguments: domainId recordId subdomainName lineId newIp
  79. updateDdns() {
  80.     local response returnCode recordIp lastIp
  81.    
  82.     # get last Ip
  83.     lastIp=$(getLastIp $1 $2)
  84.     if [ $? -eq 1 ]; then
  85.         showMsg $lastIp
  86.         return 1
  87.     fi
  88.    
  89.     # same Ip check
  90.     if [ "$lastIp" = "$5" ]; then
  91.         showMsg "Server side last Ip is the same as current local Ip!"
  92.         return 1
  93.     fi
  94.         
  95.     # update Ip
  96.     response=$(callApi "Record.Ddns" "domain_id=${1}&record_id=${2}&sub_domain=${3}&record_line_id=${4}&value=${5}&record_type=A")
  97.     returnCode=$(echo $response | sed 's/.*{"code":"\([0-9]*\)".*/\1/')
  98.     recordIp=$(echo $response | sed 's/.*,"value":"\([0-9\.]*\)".*/\1/')
  99.    
  100.     showMsg "${response}"
  101.    
  102.     # Output Ip
  103.     if [ "$recordIp" = "$5" ]; then
  104.         if [ "$returnCode" = "1" ]; then
  105.             showMsg "New Ip post success: ${recordIp}"
  106.         else
  107.             # Echo error message
  108.             showMsg $(echo $response | sed 's/.*,"message":"\([^"]*\)".*/\1/')
  109.         fi
  110.     else
  111.         showMsg "Update Failed! Please check your network."
  112.     fi
  113. }

  114. # use -q to print domain and record Ids
  115. if [ "-q" = "$1" ]; then
  116.     printDomainAndRecordIds $2 $3
  117.     exit 0
  118. fi

  119. # use (--test domainId recordId subdomainName lineId newIp) to test update
  120. if [ "--test" = "$1" ]; then
  121.         updateDdns $2 $3 $4 $5 $6
  122.         exit 0
  123. fi

  124. # to sure be call by /etc/ppp/ip-up
  125. if [ $IS_PPP_UP -eq 0 ]; then
  126.     echo "Please copy script file to /config/scripts/ppp/ip-up.d/"
  127.     exit 0
  128. else
  129.         date > /var/log/ppp.log
  130.         echo -e "  PPP_LOCAL:${PPP_LOCAL} \n  PPP_REMOTE:${PPP_REMOTE}" >> /var/log/ppp.log
  131. fi


  132. # Place each domain you want to update as follows
  133. # you can have multiple domainList blocks
  134. #
  135. # updateDdns arguments: domainId recordId subdomainName lineId newIp
  136. #
  137. # lineId samples: "line_ids": {
  138. #        "默认": 0,
  139. #        "国内": "7=0",   请将 '=' 换成 '%3D', "7%3D0"
  140. #        "国外": "3=0",
  141. #        "电信": "10=0",
  142. #        "联通": "10=1", ......
  143. # lineId list refrence: https://www.dnspod.cn/docs/domains.html#record-line
  144. # (note: lineId if include '=', please replace with '%3D')
  145. updateDdns "1111111" "222222" "@" "0" $PPP_LOCAL
复制代码
补记:开了 VPN Server 之后,有客户端连接时,也会被执行,这时需要加一个 if 判断:
  1.     if [ "$PPP_IFACE" != "pppoe0" ]; then
  2.                 #echo -e "PPP_IFACE:[${PPP_IFACE}]\nnot pppoe0" >> /var/log/ppp.log
  3.                 exit 0
  4.     fi
复制代码
参考资料:https://github.com/anrip/ArDNSPod
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|平龙认个人分站 - 爱就爱了 ( 豫ICP备14029057号-2、4、5 )
豫公网安备 41010502002156号

GMT+8, 2024-4-19 06:00 , Processed in 0.050888 second(s), 30 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表