找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
广告投放联系QQ68610888
查看: 2587|回复: 9

[虚拟机相关] unraid 命令ddns ipv6自动更新

[复制链接]
本帖最后由 chengmeiqq 于 2022-11-2 23:36 编辑

新建文件   ardnspod
  1. #!/bin/sh
  2. #

  3. #############################################################
  4. # AnripDdns v6.2.0
  5. #
  6. # Dynamic DNS using DNSPod API
  7. #
  8. # Author: Rehiy, https://github.com/rehiy
  9. #                https://www.anrip.com/?s=dnspod
  10. # Collaborators: ProfFan, https://github.com/ProfFan
  11. #
  12. # Usage: please refer to `ddnspod.sh`
  13. #
  14. #############################################################

  15. export arToken

  16. # Define arLog to output to stderr, if not defined in ddnspod.sh

  17. if ! type arLog >/dev/null 2>&1; then
  18.     arLog() {
  19.         >&2 echo $@
  20.     }
  21. fi

  22. # The url to be used for querying public ipv6 address. We set a default here.

  23. if [ -z "$arIp6QueryUrl" ]; then
  24.     arIp6QueryUrl="https://6.ipw.cn"
  25. fi

  26. # The error code to return when a ddns record is not changed

  27. if [ -z "$arErrCodeUnchanged" ]; then
  28.     arErrCodeUnchanged=0 # By default, report unchanged event as success
  29. fi

  30. # Get IPv4

  31. arWanIp4() {

  32.     local hostIp

  33.     local lanIps="^$"

  34.     lanIps="$lanIps|(^10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$)"
  35.     lanIps="$lanIps|(^127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$)"
  36.     lanIps="$lanIps|(^169\.254\.[0-9]{1,3}\.[0-9]{1,3}$)"
  37.     lanIps="$lanIps|(^172\.(1[6-9]|2[0-9]|3[0-1])\.[0-9]{1,3}\.[0-9]{1,3}$)"
  38.     lanIps="$lanIps|(^192\.168\.[0-9]{1,3}\.[0-9]{1,3}$)"
  39.     lanIps="$lanIps|(^100\.(6[4-9]|[7-9][0-9])\.[0-9]{1,3}\.[0-9]{1,3}$)"  # 100.64.x.x - 100.99.x.x
  40.     lanIps="$lanIps|(^100\.1([0-1][0-9]|2[0-7])\.[0-9]{1,3}\.[0-9]{1,3}$)" # 100.100.x.x - 100.127.x.x

  41.     case $(uname) in
  42.         'Linux')
  43.             hostIp=$(ip -o -4 route get 100.64.0.1 | grep -oE 'src [0-9\.]+' | awk '{print $2}' | grep -Ev "$lanIps")
  44.         ;;
  45.         Darwin|FreeBSD)
  46.             hostIp=$(ifconfig | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}' | grep -Ev "$lanIps")
  47.         ;;
  48.     esac

  49.     if [ -z "$hostIp" ]; then
  50.         return 1
  51.     fi

  52.     if [ -z "$(echo $hostIp | grep -E '^[0-9\.]+$')" ]; then
  53.         return 1
  54.     fi

  55.     echo $hostIp

  56. }

  57. # Get IPv6

  58. arWanIp6() {

  59.     local hostIp

  60.     local lanIps="(^$)|(^::1$)|(^fe[8-9,A-F])"

  61.     case $(uname) in
  62.         'Linux')
  63.             hostIp=$(ip -o -6 route get 100::1 | grep -oE 'src [0-9a-fA-F:]+' | awk '{print $2}' | grep -Ev "$lanIps")
  64.         ;;
  65.     esac

  66.     if [ -z "$hostIp" ]; then
  67.         if type curl >/dev/null 2>&1; then
  68.             hostIp=$(curl -6 -s $arIp6QueryUrl)
  69.         elif ! wget --help 2>&1 | grep -qs BusyBox; then
  70.             hostIp=$(wget -6 -q -O- $arIp6QueryUrl)
  71.         else
  72.             hostIp=$(wget -q -O- $arIp6QueryUrl)
  73.         fi
  74.     fi

  75.     if [ -z "$hostIp" ]; then
  76.         arLog "> arWanIp6 - Can't get ip address"
  77.         return 1
  78.     fi

  79.     if [ -z "$(echo $hostIp | grep -E '^[0-9a-fA-F:]+$')" ]; then
  80.         arLog "> arWanIp6 - Invalid ip address"
  81.         return 1
  82.     fi

  83.     echo $hostIp

  84. }

  85. # Dnspod Bridge
  86. # Args: interface data

  87. arDdnsApi() {

  88.     local agent="AnripDdns/6.2.0(wang@rehiy.com)"

  89.     local dnsapi="https://dnsapi.cn/${1:?'Info.Version'}"
  90.     local params="login_token=$arToken&format=json&lang=en&$2"

  91.     if type curl >/dev/null 2>&1; then
  92.         curl -4 -s -A $agent -d $params $dnsapi
  93.     elif ! wget --help 2>&1 | grep -qs BusyBox; then
  94.         wget -4 -q -O- --no-check-certificate -U $agent --post-data $params $dnsapi
  95.     else
  96.         wget -q -O- --no-check-certificate -U $agent --post-data $params $dnsapi
  97.     fi

  98. }

  99. # Fetch Record Id
  100. # Args: domain subdomain recordType

  101. arDdnsLookup() {

  102.     local errMsg

  103.     local recordId

  104.     # Get Record Id
  105.     recordId=$(arDdnsApi "Record.List" "domain=$1&sub_domain=$2&record_type=$3")
  106.     recordId=$(echo $recordId | sed 's/.*"id":"\([0-9]*\)".*/\1/')

  107.     if ! [ "$recordId" -gt 0 ] 2>/dev/null ;then
  108.         errMsg=$(echo $recordId | sed 's/.*"message":"\([^"]*\)".*/\1/')
  109.         arLog "> arDdnsLookup - $errMsg"
  110.         return 1
  111.     fi

  112.     echo $recordId
  113. }

  114. # Update Record Value
  115. # Args: domain subdomain recordId recordType [hostIp]

  116. arDdnsUpdate() {

  117.     local errMsg

  118.     local lastRecordIp
  119.     local recordRs
  120.     local recordCd
  121.     local recordIp

  122.     # if code for unchanged event is specified to be different from updated event, we fetch the last record ip
  123.     if [ $arErrCodeUnchanged -ne 0 ]; then
  124.         recordRs=$(arDdnsApi "Record.Info" "domain=$1&record_id=$3")
  125.         recordCd=$(echo $recordRs | sed 's/.*{"code":"\([0-9]*\)".*/\1/')
  126.         lastRecordIp=$(echo $recordRs | sed 's/.*,"value":"\([0-9a-fA-F\.\:]*\)".*/\1/')

  127.         if [ "$recordCd" != "1" ]; then
  128.             errMsg=$(echo $recordRs | sed 's/.*,"message":"\([^"]*\)".*/\1/')
  129.             arLog "> arDdnsUpdate - error: $errMsg"
  130.             return 1
  131.         else
  132.             arLog "> arDdnsUpdate - last record ip: $lastRecordIp"
  133.         fi
  134.     fi

  135.     # update ip
  136.     if [ -z "$5" ]; then
  137.         recordRs=$(arDdnsApi "Record.Ddns" "domain=$1&sub_domain=$2&record_id=$3&record_type=$4&record_line=%e9%bb%98%e8%ae%a4")
  138.     else
  139.         recordRs=$(arDdnsApi "Record.Ddns" "domain=$1&sub_domain=$2&record_id=$3&record_type=$4&value=$5&record_line=%e9%bb%98%e8%ae%a4")
  140.     fi

  141.     # parse result
  142.     recordCd=$(echo $recordRs | sed 's/.*{"code":"\([0-9]*\)".*/\1/')
  143.     recordIp=$(echo $recordRs | sed 's/.*,"value":"\([0-9a-fA-F\.\:]*\)".*/\1/')

  144.     if [ "$recordCd" != "1" ]; then
  145.         errMsg=$(echo $recordRs | sed 's/.*,"message":"\([^"]*\)".*/\1/')
  146.         arLog "> arDdnsUpdate - error: $errMsg"
  147.         return 1
  148.     elif [ $arErrCodeUnchanged -eq 0 ]; then
  149.         arLog "> arDdnsUpdate - success: $recordIp" # both unchanged and updated event
  150.         echo $recordIp
  151.         return 0
  152.     elif [ "$recordIp" = "$lastRecordIp" ]; then
  153.         arLog "> arDdnsUpdate - unchanged: $recordIp" # unchanged event
  154.         echo $recordIp
  155.         return $arErrCodeUnchanged
  156.     else
  157.         arLog "> arDdnsUpdate - updated: $recordIp" # updated event
  158.         echo $recordIp
  159.         return 0
  160.     fi

  161. }

  162. # DDNS Check
  163. # Args: domain subdomain [6|4]

  164. arDdnsCheck() {

  165.     local hostIp

  166.     local recordId
  167.     local recordType

  168.     arLog "=== Check $2.$1 ==="
  169.     arLog "Fetching Host Ip"

  170.     if [ "$3" = "6" ]; then
  171.         recordType=AAAA
  172.         hostIp=$(arWanIp6)
  173.         if [ $? -ne 0 ]; then
  174.             arLog $hostIp
  175.             return 1
  176.         else
  177.             arLog "> Host Ip: $hostIp"
  178.             arLog "> Record Type: $recordType"
  179.         fi
  180.     else
  181.         recordType=A
  182.         hostIp=$(arWanIp4)
  183.         if [ $? -ne 0 ]; then
  184.             arLog "> Host Ip: Auto"
  185.             arLog "> Record Type: $recordType"
  186.         else
  187.             arLog "> Host Ip: $hostIp"
  188.             arLog "> Record Type: $recordType"
  189.         fi
  190.     fi

  191.     arLog "Fetching RecordId"
  192.     recordId=$(arDdnsLookup "$1" "$2" "$recordType")

  193.     if [ $? -ne 0 ]; then
  194.         arLog $recordId
  195.         return 1
  196.     else
  197.         arLog "> Record Id: $recordId"
  198.     fi

  199.     arLog "Updating Record value"
  200.     arDdnsUpdate "$1" "$2" "$recordId" "$recordType" "$hostIp"

  201. }
复制代码

新建文件 ddnspod.sh

  1. #!/bin/sh
  2. #

  3. # Import ardnspod functions
  4. . /your_real_path/ardnspod

  5. # Combine your token ID and token together as follows
  6. arToken="12345,7676f344eaeaea9074c123451234512d"

  7. # Web endpoint to be used for querying the public IPv6 address
  8. # Set this to override the default url provided by ardnspod
  9. # arIp6QueryUrl="https://6.ipw.cn"

  10. # Return code when the last record IP is same as current host IP
  11. # Set this to a value other than 0 to distinguish with a successful ddns update
  12. # arErrCodeUnchanged=0

  13. # Place each domain you want to check as follows
  14. # you can have multiple arDdnsCheck blocks

  15. # IPv4:
  16. arDdnsCheck "test.org" "subdomain"

  17. # IPv6:
  18. arDdnsCheck "test.org" "subdomain6" 6
复制代码

上传文件到unraid你自己文件夹 给777权限!!

ardnspod    ddnspod.sh   把这两个文件复制到unraid

修改 ddnspod.sh

. /your_real_path/ardnspod 改成你自己的文件夹,比如我这边是改成 ./mnt/user/Ssd/User/ddns/ardnspod
arToken="12345,7676f344eaeaea9074c123451234512d"      改成自己的 ID跟token

# IPv6:
arDdnsCheck "test.org" "ys" 6       改成你自己域名比如:ys.test.org 就可以访问了!

上传到unraid文件夹


安装插件“User Scripts”


添加自定义命令脚本


添加命令:请修改成自己的文件夹
  1. /mnt/ssd/Ssd/User/ddns/ddnspod.sh
复制代码

一个小时更新一次!


本帖子中包含更多资源

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

×
本帖最后由 diy97 于 2022-11-4 15:47 编辑

这个不错,有阿里的吗?
回复

使用道具 举报

试试这个咋样
回复

使用道具 举报

来自手机 | 显示全部楼层
装个ddnsgo不就好了
回复

使用道具 举报

本帖最后由 withero 于 2022-11-20 00:20 编辑

更新不了。
=== Check test.baidu.cn ===
Fetching Host Ip
> Host Ip: 18.13.66.15
> Record Type: A
Fetching RecordId
> arDdnsLookup -

到这里就没信息了,不懂

回复

使用道具 举报

 楼主| | 显示全部楼层
withero 发表于 2022-11-20 00:18
更新不了。
=== Check test.baidu.cn ===
Fetching Host Ip
  1. curl https://6.ipw.cn
复制代码
  1. curl https://speed.neu6.edu.cn/getIP.php
复制代码

执行下这个命令看看 能不能正常获取到IPV6   IP
回复

使用道具 举报

本帖最后由 withero 于 2022-11-22 12:47 编辑

我是在Padavan上用,用wget方式。作者也在处理,不知道,好像是BusyBox有关。
回复

使用道具 举报

这个不错!思考一下啊
回复

使用道具 举报

来自手机 | 显示全部楼层
每天认真看帖,思考提高
回复

使用道具 举报

谢谢分享
回复

使用道具 举报

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

本版积分规则

关闭

欢迎大家光临恩山无线论坛上一条 /1 下一条

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

GMT+8, 2024-5-14 08:50

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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

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