找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
广告投放联系QQ68610888
查看: 1356|回复: 6

[k3] dnspod更新shell脚本

[复制链接]
发表于 2020-2-26 09:34 | 显示全部楼层 |阅读模式
lean在2020年2月22号的commit中删除了luci-app-dnspod(https://github.com/coolsnowwolf/lede/commit/75a885d4252040f4ee88b05ed5f1f3ada6ed3681)
下面是我一直使用的dnspod脚本(原作者网站已经打不开,如有违规请联系删除):
ddnspod.sh

  1. #!/bin/sh

  2. #################################################
  3. # AnripDdns v5.08
  4. # Dynamic DNS using DNSPod API
  5. # Original by anrip<mail@anrip.com>, http://www.anrip.com/ddnspod
  6. # Edited by ProfFan
  7. #################################################

  8. # OS Detection
  9. case $(uname) in
  10.   'Linux')
  11.     echo "Linux"
  12.     arIpAddress() {
  13.         local extip
  14.         extip=$(ip -o -4 addr list | grep -Ev '\s(docker|lo)' | awk '{print $4}' | cut -d/ -f1 | grep -Ev '(^127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^172\.1[6-9]{1}[0-9]{0,1}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^172\.2[0-9]{1}[0-9]{0,1}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^172\.3[0-1]{1}[0-9]{0,1}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^192\.168\.[0-9]{1,3}\.[0-9]{1,3}$)')
  15.         if [ "x${extip}" = "x" ]; then
  16.                 extip=$(ip -o -4 addr list | grep -Ev '\s(docker|lo)' | awk '{print $4}' | cut -d/ -f1 )
  17.         fi
  18.         echo $extip
  19.     }
  20.     ;;
  21.   'FreeBSD')
  22.     echo 'FreeBSD'
  23.     exit 100
  24.     ;;
  25.   'WindowsNT')
  26.     echo "Windows"
  27.     exit 100
  28.     ;;
  29.   'Darwin')
  30.     echo "Mac"
  31.     arIpAddress() {
  32.         ifconfig | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}'
  33.     }
  34.     ;;
  35.   'SunOS')
  36.     echo 'Solaris'
  37.     exit 100
  38.     ;;
  39.   'AIX')
  40.     echo 'AIX'
  41.     exit 100
  42.     ;;
  43.   *) ;;
  44. esac

  45. # Get script dir
  46. # See: http://stackoverflow.com/a/29835459/4449544
  47. rreadlink() ( # Execute the function in a *subshell* to localize variables and the effect of `cd`.

  48.   target=$1 fname= targetDir= CDPATH=

  49.   # Try to make the execution environment as predictable as possible:
  50.   # All commands below are invoked via `command`, so we must make sure that `command`
  51.   # itself is not redefined as an alias or shell function.
  52.   # (Note that command is too inconsistent across shells, so we don't use it.)
  53.   # `command` is a *builtin* in bash, dash, ksh, zsh, and some platforms do not even have
  54.   # an external utility version of it (e.g, Ubuntu).
  55.   # `command` bypasses aliases and shell functions and also finds builtins
  56.   # in bash, dash, and ksh. In zsh, option POSIX_BUILTINS must be turned on for that
  57.   # to happen.
  58.   { \unalias command; \unset -f command; } >/dev/null 2>&1
  59.   [ -n "$ZSH_VERSION" ] && options[POSIX_BUILTINS]=on # make zsh find *builtins* with `command` too.

  60.   while :; do # Resolve potential symlinks until the ultimate target is found.
  61.       [ -L "$target" ] || [ -e "$target" ] || { command printf '%s\n' "ERROR: '$target' does not exist." >&2; return 1; }
  62.       command cd "$(command dirname -- "$target")" # Change to target dir; necessary for correct resolution of target path.
  63.       fname=$(command basename -- "$target") # Extract filename.
  64.       [ "$fname" = '/' ] && fname='' # !! curiously, `basename /` returns '/'
  65.       if [ -L "$fname" ]; then
  66.         # Extract [next] target path, which may be defined
  67.         # *relative* to the symlink's own directory.
  68.         # Note: We parse `ls -l` output to find the symlink target
  69.         #       which is the only POSIX-compliant, albeit somewhat fragile, way.
  70.         target=$(command ls -l "$fname")
  71.         target=${target#* -> }
  72.         continue # Resolve [next] symlink target.
  73.       fi
  74.       break # Ultimate target reached.
  75.   done
  76.   targetDir=$(command pwd -P) # Get canonical dir. path
  77.   # Output the ultimate target's canonical path.
  78.   # Note that we manually resolve paths ending in /. and /.. to make sure we have a normalized path.
  79.   if [ "$fname" = '.' ]; then
  80.     command printf '%s\n' "${targetDir%/}"
  81.   elif  [ "$fname" = '..' ]; then
  82.     # Caveat: something like /var/.. will resolve to /private (assuming /var[url=home.php?mod=space&uid=162986]@[/url] -> /private/var), i.e. the '..' is applied
  83.     # AFTER canonicalization.
  84.     command printf '%s\n' "$(command dirname -- "${targetDir}")"
  85.   else
  86.     command printf '%s\n' "${targetDir%/}/$fname"
  87.   fi
  88. )

  89. DIR=$(dirname -- "$(rreadlink "$0")")

  90. # Global Variables:

  91. # Token-based Authentication
  92. arToken=""
  93. # Account-based Authentication
  94. arMail=""
  95. arPass=""

  96. # Load config

  97. #. $DIR/dns.conf

  98. # Get Domain IP
  99. # arg: domain
  100. arDdnsInfo() {
  101.     local domainID recordID recordIP
  102.     # Get domain ID
  103.     domainID=$(arApiPost "Domain.Info" "domain=${1}")
  104.     domainID=$(echo $domainID | sed 's/.*{"id":"\([0-9]*\)".*/\1/')

  105.     # Get Record ID
  106.     recordID=$(arApiPost "Record.List" "domain_id=${domainID}&sub_domain=${2}")
  107.     recordID=$(echo $recordID | sed 's/.*\[{"id":"\([0-9]*\)".*/\1/')

  108.     # Last IP
  109.     recordIP=$(arApiPost "Record.Info" "domain_id=${domainID}&record_id=${recordID}")
  110.     recordIP=$(echo $recordIP | sed 's/.*,"value":"\([0-9\.]*\)".*/\1/')

  111.     # Output IP
  112.     case "$recordIP" in
  113.       [1-9][0-9]*)
  114.         echo $recordIP
  115.         return 0
  116.         ;;
  117.       *)
  118.         echo "Get Record Info Failed!"
  119.         return 1
  120.         ;;
  121.     esac
  122. }

  123. # Get data
  124. # arg: type data
  125. arApiPost() {
  126.     local agent="AnripDdns/5.07(mail@anrip.com)"
  127.     local inter="https://dnsapi.cn/${1:?'Info.Version'}"
  128.     if [ "x${arToken}" = "x" ]; then # undefine token
  129.         local param="login_email=${arMail}&login_password=${arPass}&format=json&${2}"
  130.     else
  131.         local param="login_token=${arToken}&format=json&${2}"
  132.     fi
  133.     wget --quiet --no-check-certificate --output-document=- --user-agent=$agent --post-data $param $inter
  134. }

  135. # Update
  136. # arg: main domain  sub domain
  137. arDdnsUpdate() {
  138.     local domainID recordID recordRS recordCD recordIP myIP
  139.     # Get domain ID
  140.     domainID=$(arApiPost "Domain.Info" "domain=${1}")
  141.     domainID=$(echo $domainID | sed 's/.*{"id":"\([0-9]*\)".*/\1/')

  142.     # Get Record ID
  143.     recordID=$(arApiPost "Record.List" "domain_id=${domainID}&sub_domain=${2}")
  144.     recordID=$(echo $recordID | sed 's/.*\[{"id":"\([0-9]*\)".*/\1/')

  145.     # Update IP
  146.     myIP=$(arIpAddress)
  147.     recordRS=$(arApiPost "Record.Ddns" "domain_id=${domainID}&record_id=${recordID}&sub_domain=${2}&record_type=A&value=${myIP}&record_line=默认")
  148.     recordCD=$(echo $recordRS | sed 's/.*{"code":"\([0-9]*\)".*/\1/')
  149.     recordIP=$(echo $recordRS | sed 's/.*,"value":"\([0-9\.]*\)".*/\1/')

  150.     # Output IP
  151.     if [ "$recordIP" = "$myIP" ]; then
  152.         if [ "$recordCD" = "1" ]; then
  153.             echo $recordIP
  154.             return 0
  155.         fi
  156.         # Echo error message
  157.         echo $recordRS | sed 's/.*,"message":"\([^"]*\)".*/\1/'
  158.         return 1
  159.     else
  160.         echo "Update Failed! Please check your network."
  161.         return 1
  162.     fi
  163. }

  164. # DDNS Check
  165. # Arg: Main Sub
  166. arDdnsCheck() {
  167.     local postRS
  168.     local lastIP
  169.     local hostIP=$(arIpAddress)
  170.     echo "Updating Domain: ${2}.${1}"
  171.     echo "hostIP: ${hostIP}"
  172.     lastIP=$(arDdnsInfo $1 $2)
  173.     if [ $? -eq 0 ]; then
  174.         echo "lastIP: ${lastIP}"
  175.         if [ "$lastIP" != "$hostIP" ]; then
  176.             postRS=$(arDdnsUpdate $1 $2)
  177.             if [ $? -eq 0 ]; then
  178.                 echo "postRS: ${postRS}"
  179.                 return 0
  180.             else
  181.                 echo ${postRS}
  182.                 return 1
  183.             fi
  184.         fi
  185.         echo "Last IP is the same as current IP!"
  186.         return 1
  187.     fi
  188.     echo ${lastIP}
  189.     return 1
  190. }

  191. # DDNS
  192. #echo ${#domains[@]}
  193. #for index in ${!domains[@]}; do
  194. #    echo "${domains[index]} ${subdomains[index]}"
  195. #    arDdnsCheck "${domains[index]}" "${subdomains[index]}"
  196. #done

  197. . $DIR/dns.conf
复制代码
dns.conf
  1. # For security reasons, it is recommended that you use token-based auth instead
  2. # arMail="test@gmail.com"
  3. # arPass="123"

  4. # Combine your token ID and token together as follows
  5. arToken="****,*****"

  6. # Place each domain you want to check as follows
  7. # you can have multiple arDdnsCheck blocks
  8. arDdnsCheck "****" "****"
复制代码

请将上面两个文件放在同一目录下,运行ddnspod.sh即可更新路由器地址到dnspod。

每三十分钟自动运行一次(放在计划任务中):

  1. */30 * * * * /root/dnspod/ddnspod.sh
复制代码





我的恩山、我的无线 The best wifi forum is right here.
发表于 2020-2-26 09:59 | 显示全部楼层
dnspod老是出幺蛾子

点评

我用着很稳定啊  详情 回复 发表于 2020-2-26 15:59
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| 发表于 2020-2-26 15:59 | 显示全部楼层
whqd123 发表于 2020-2-26 09:59
dnspod老是出幺蛾子

我用着很稳定啊

点评

怎么我运行出错呀,大神 root@OpenWrt:~# sh /root/dnspod/ddnspod.sh : not foundd/ddnspod.sh: line 2: : not foundd/ddnspod.sh: line 9: /root/dnspod/ddnspod.sh: line 11: syntax error: unexpected word  详情 回复 发表于 2020-2-28 17:25
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2020-2-28 17:25 | 显示全部楼层

怎么我运行出错呀,大神
root@OpenWrt:~# sh /root/dnspod/ddnspod.sh
: not foundd/ddnspod.sh: line 2:
: not foundd/ddnspod.sh: line 9:
/root/dnspod/ddnspod.sh: line 11: syntax error: unexpected word (expecting "in")
root@OpenWrt:~#

点评

我也是这样,怎么解决呢?。。。  详情 回复 发表于 2020-3-15 11:40
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2020-3-15 11:40 | 显示全部楼层
chishu 发表于 2020-2-28 17:25
怎么我运行出错呀,大神
root@OpenWrt:~# sh /root/dnspod/ddnspod.sh
: not foundd/ddnspod.sh: line  ...

我也是这样,怎么解决呢?。。。
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2020-3-15 12:08 | 显示全部楼层
动态DNS里面的DNSPOD 容易出问题,需要切换一下服务商才能重新解析,不知道为什么。。。
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2020-3-18 08:04 | 显示全部楼层
你的crontab 设置有问题,应该0 */30 * * *    这样才是30分钟一次
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

有疑问请添加管理员QQ86788181|手机版|小黑屋|Archiver|恩山无线论坛(常州市恩山计算机开发有限公司版权所有) ( 苏ICP备05084872号 )

GMT+8, 2024-4-20 06:48

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

| 江苏省互联网有害信息举报中心 举报信箱:js12377 | @jischina.com.cn 举报电话:025-88802724 本站不良内容举报信箱:68610888@qq.com 举报电话:0519-86695797

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