数码之家

 找回密码
 立即注册
搜索
查看: 751|回复: 0

需要DDNSIPV6的可以了解一下{:084:}{:084:}

[复制链接]
发表于 2022-4-8 15:42:27 | 显示全部楼层 |阅读模式

爱科技、爱创意、爱折腾、爱极致,我们都是技术控

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 ddb009 于 2022-4-8 15:49 编辑

  1. #!/bin/bash

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

  8. #################################################
  9. # 2018-11-06
  10. # support  LAN / WAN / IPV6 resolution

  11. # 2019-05-24
  12. # Support Ipv6 truly (Yes, it was just claimed to, but actually not = =!)
  13. # Add another way resolving IPv6, for machines without nvram.

  14. #if you have any issues, please let me know.
  15. # [url]https://blog.csdn.net/Imkiimki/article/details/83794355[/url]
  16. # Daleshen mailto:gf@gfshen.cn

  17. #################################################

  18. #Please select IP type
  19. IPtype=3  #1.WAN 2.LAN 3.IPv6
  20. #---------------------
  21. if [ $IPtype = '3' ]; then
  22.     record_type='AAAA'
  23. else
  24.     record_type='A'
  25. fi
  26. echo Type: ${record_type}

  27. # OS Detection
  28. case $(uname) in
  29.   'Linux')
  30.     echo "OS: Linux"
  31.     arIpAddress() {

  32.         case $IPtype in
  33.                 '1')
  34.                                 
  35.                 curltest=`which curl`
  36.                 if [ -z "$curltest" ] || [ ! -s "`which curl`" ]
  37.                 then
  38.                         #根据实际情况选择使用合适的网址
  39.                         #wget --no-check-certificate --quiet --output-document=- "http://checkipv6.dyndns.com/dyndns/getip" | grep "IP地址" | grep -E -o '([0-9]+\.){3}[0-9]+' | head -n1 | cut -d' ' -f1
  40.                         wget --no-check-certificate --secure-protocol=TLSv1_2 --quiet --output-document=- "http://checkipv6.dyndns.com/dyndns/getip" | grep -E -o '([0-9]+\.){3}[0-9]+' | head -n1 | cut -d' ' -f1
  41.                         #wget --no-check-certificate --secure-protocol=TLSv1_2 --quiet --output-document=- "ip.6655.com/ip.aspx" | grep -E -o '([0-9]+\.){3}[0-9]+' | head -n1 | cut -d' ' -f1
  42.                         #wget --no-check-certificate --secure-protocol=TLSv1_2 --quiet --output-document=- "ip.3322.net" | grep -E -o '([0-9]+\.){3}[0-9]+' | head -n1 | cut -d' ' -f1
  43.                 else
  44.                 curl -k -s "http://checkipv6.dyndns.com/dyndns/getip" | grep -E -o '([0-9]+\.){3}[0-9]+' | head -n1 | cut -d' ' -f1
  45.                 fi
  46.                 ;;

  47.                 '2')
  48.                
  49.                 ip -o -4 addr list | grep -Ev '\s(docker|lo)' | awk '{print $4}' | cut -d/ -f1
  50.                 ;;

  51.                 '3')
  52.                
  53.                 # 因为一般ipv6没有nat ipv6的获得可以本机获得
  54.                 #ifconfig $(nvram get wan0_ifname_t) | awk '/Global/{print $3}' | awk -F/ '{print $1}'
  55.                 ip addr show dev eth0 | sed -e's/^.*inet6 \([^ ]*\)\/.*$/\1/;t;d' | awk 'NR==1' #如果没有nvram,使用这条,注意将eth0改为本机上的网口设备 (通过 ifconfig 查看网络接口)
  56.                 ;;
  57.          esac

  58.     }
  59.     ;;
  60.   'FreeBSD')
  61.     echo 'FreeBSD'
  62.     exit 100
  63.     ;;
  64.   'WindowsNT')
  65.     echo "Windows"
  66.     exit 100
  67.     ;;
  68.   'Darwin')
  69.     echo "Mac"
  70.     arIpAddress() {
  71.         ifconfig | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}'
  72.     }
  73.     ;;
  74.   'SunOS')
  75.     echo 'Solaris'
  76.     exit 100
  77.     ;;
  78.   'AIX')
  79.     echo 'AIX'
  80.     exit 100
  81.     ;;
  82.   *)
  83.     echo 'Cant match OS'
  84.     exit 100
  85.     ;;
  86. esac

  87. echo "Address: $(arIpAddress)"

  88. # Get script dir
  89. # See: [url]http://stackoverflow.com/a/29835459/4449544[/url]
  90. rreadlink() ( # Execute the function in a *subshell* to localize variables and the effect of `cd`.

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

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

  103.   while :; do # Resolve potential symlinks until the ultimate target is found.
  104.       [ -L "$target" ] || [ -e "$target" ] || { command printf '%s\n' "ERROR: '$target' does not exist." >&2; return 1; }
  105.       command cd "$(command dirname -- "$target")" # Change to target dir; necessary for correct resolution of target path.
  106.       fname=$(command basename -- "$target") # Extract filename.
  107.       [ "$fname" = '/' ] && fname='' # !! curiously, `basename /` returns '/'
  108.       if [ -L "$fname" ]; then
  109.         # Extract [next] target path, which may be defined
  110.         # *relative* to the symlink's own directory.
  111.         # Note: We parse `ls -l` output to find the symlink target
  112.         #       which is the only POSIX-compliant, albeit somewhat fragile, way.
  113.         target=$(command ls -l "$fname")
  114.         target=${target#* -> }
  115.         continue # Resolve [next] symlink target.
  116.       fi
  117.       break # Ultimate target reached.
  118.   done
  119.   targetDir=$(command pwd -P) # Get canonical dir. path
  120.   # Output the ultimate target's canonical path.
  121.   # Note that we manually resolve paths ending in /. and /.. to make sure we have a normalized path.
  122.   if [ "$fname" = '.' ]; then
  123.     command printf '%s\n' "${targetDir%/}"
  124.   elif  [ "$fname" = '..' ]; then
  125.     # Caveat: something like /var/.. will resolve to /private (assuming /var[url=home.php?mod=space&uid=3057940]@[/url] -> /private/var), i.e. the '..' is applied
  126.     # AFTER canonicalization.
  127.     command printf '%s\n' "$(command dirname -- "${targetDir}")"
  128.   else
  129.     command printf '%s\n' "${targetDir%/}/$fname"
  130.   fi
  131. )

  132. DIR=$(dirname -- "$(readlink "$0")")

  133. # Global Variables:

  134. # Token-based Authentication
  135. arToken="XXXX,XXXXXXXXXXX"
  136. # Account-based Authentication
  137. arMail="XXXX@qq.com"
  138. arPass="XXXXX"

  139. # Load config

  140. #. $DIR/dns.conf

  141. # Get Domain IP
  142. # arg: domain
  143. arDdnsInfo() {
  144.     local domainID recordID recordIP
  145.     # Get domain ID
  146.     domainID=$(arApiPost "Domain.Info" "domain=${1}")

  147.     domainID=$(echo $domainID | sed 's/.*{"id":"\([0-9]*\)".*/\1/')

  148.     # Get Record ID
  149.     recordID=$(arApiPost "Record.List" "domain_id=${domainID}&sub_domain=${2}&record_type=${record_type}")

  150.     recordID=$(echo $recordID | sed 's/.*\[{"id":"\([0-9]*\)".*/\1/')

  151.     # Last IP
  152.     recordIP=$(arApiPost "Record.Info" "domain_id=${domainID}&record_id=${recordID}&record_type=${record_type}")

  153.     recordIP=$(echo $recordIP | sed 's/.*,"value":"\([0-9a-z\.:]*\)".*/\1/')

  154.     # Output IP
  155.     case "$recordIP" in
  156.       [1-9a-z]*)
  157.         echo $recordIP
  158.         return 0
  159.         ;;
  160.       *)
  161.         echo "Get Record Info Failed!"
  162.         return 1
  163.         ;;
  164.     esac
  165. }

  166. # Get data
  167. # arg: type data
  168. # see Api doc: [url]https://www.dnspod.cn/docs/records.html#[/url]
  169. arApiPost() {
  170.     local agent="AnripDdns/5.07([email]mail@anrip.com[/email])"
  171.     #local inter="https://dnsapi.cn/${1:?'Info.Version'}"
  172.     local inter="https://dnsapi.cn/${1}"
  173.     if [ "x${arToken}" = "x" ]; then # undefine token
  174.         local param="login_email=${arMail}&login_password=${arPass}&format=json&${2}"
  175.     else
  176.         local param="login_token=${arToken}&format=json&${2}"
  177.     fi
  178.     wget --quiet --no-check-certificate --secure-protocol=TLSv1_2 --output-document=- --user-agent=$agent --post-data $param $inter
  179. }

  180. # Update
  181. # arg: main domain  sub domain
  182. arDdnsUpdate() {
  183.     local domainID recordID recordRS recordCD recordIP myIP


  184.     # Get domain ID
  185.     domainID=$(arApiPost "Domain.Info" "domain=${1}")
  186.     domainID=$(echo $domainID | sed 's/.*{"id":"\([0-9]*\)".*/\1/')
  187.     #echo $domainID
  188.     # Get Record ID
  189.     recordID=$(arApiPost "Record.List" "domain_id=${domainID}&record_type=${record_type}&sub_domain=${2}")
  190.     recordID=$(echo $recordID | sed 's/.*\[{"id":"\([0-9]*\)".*/\1/')
  191.     #echo $recordID
  192.     # Update IP
  193.     myIP=$(arIpAddress)
  194.     recordRS=$(arApiPost "Record.Modify" "domain_id=${domainID}&sub_domain=${2}&record_type=${record_type}&record_id=${recordID}&record_line=默认&value=${myIP}")
  195.     recordCD=$(echo $recordRS | sed 's/.*{"code":"\([0-9]*\)".*/\1/')
  196.     recordIP=$(echo $recordRS | sed 's/.*,"value":"\([0-9a-z\.:]*\)".*/\1/')

  197.     # Output IP
  198.     if [ "$recordIP" = "$myIP" ]; then
  199.         if [ "$recordCD" = "1" ]; then
  200.             echo $recordIP
  201.             return 0
  202.         fi
  203.         # Echo error message
  204.         echo $recordRS | sed 's/.*,"message":"\([^"]*\)".*/\1/'
  205.         return 1
  206.     else
  207.         echo $recordIP #"Update Failed! Please check your network."
  208.         return 1
  209.     fi
  210. }

  211. # DDNS Check
  212. # Arg: Main Sub
  213. arDdnsCheck() {
  214.     local postRS
  215.     local lastIP
  216.     local hostIP=$(arIpAddress)
  217.     echo "Updating Domain: ${2}.${1}"
  218.     echo "hostIP: ${hostIP}"
  219.     lastIP=$(arDdnsInfo $1 $2)
  220.     if [ $? -eq 0 ]; then
  221.         echo "lastIP: ${lastIP}"
  222.         if [ "$lastIP" != "$hostIP" ]; then
  223.             postRS=$(arDdnsUpdate $1 $2)

  224.             if [ $? -eq 0 ]; then
  225.                 echo "update to ${postRS} successed."
  226.                 return 0
  227.             else
  228.                 echo ${postRS}
  229.                 return 1
  230.             fi
  231.         fi
  232.         echo "Last IP is the same as current, no action."
  233.         return 0
  234.     fi
  235.     echo ${lastIP}
  236.     return 1
  237. }

  238. # DDNS
  239. #echo ${#domains[@]}
  240. #for index in ${!domains[@]}; do
  241. #    echo "${domains[index]} ${subdomains[index]}"
  242. #    arDdnsCheck "${domains[index]}" "${subdomains[index]}"
  243. #done

  244. ...最后一句请下载压缩包
复制代码


DDNSIPV6.sh.rar

3.64 KB, 下载次数: 0, 下载积分: 家元 -55

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

本版积分规则

APP|手机版|小黑屋|关于我们|联系我们|法律条款|技术知识分享平台

闽公网安备35020502000485号

闽ICP备2021002735号-2

GMT+8, 2024-5-18 17:26 , Processed in 0.109200 second(s), 13 queries , Redis On.

Powered by Discuz!

© 2006-2023 smzj.net

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