Giter Club home page Giter Club logo

aliyun-ddns-shell's Introduction

阿里云域名动态IP解析Shell小脚本

当你手里面有一个闲置的域名,有一个漂浮不定的外网ip,你又想在外网ip变更后自动解析到域名上,此情此景此脚本可能会帮上你。

更新日志

  • 2020-09-29 支持IPV6解析(支持设置解析记录类型:A、NS、MX、TXT、CNAME、SRV、AAAA、CAA、REDIRECT_URL、FORWARD_URL)
  • 2021-09-06 支持设置解析线路

使用步骤:

  • 在阿里云域名管理中解析自己的域名
    • 记录类型:A
    • 主机记录:按你需要输入
    • 解析路线:默认(随你)
    • 记录值:随便输入一个Ip地址(运行脚本后会自动更新到正确的)
    • TTL值:10分钟
  • 需要通过阿里云域名解析Api操作,所以需要申请阿里云的Acesskeys
  • 运行自动解析域名脚本
    • git clone https://github.com/risfeng/aliyun-ddns-shell.git
    • 给aliyun-ddns.sh脚本赋可执行权:chmod +x aliyun-ddns-shell/src/aliyun/aliyun-ddns.sh
    • 运行脚本,根据提示进行配置(第一次运行需要配置)
    • 如需要使用钉钉自定义机器人推送成功失败消息,请新建自定义钉钉机器人,配置access_token和消息加签密钥
    • 脚本支持2种运行方式:
      • 直接运行脚本根据提示选择需要使用的功能
      > aliyun-ddns.sh (mac: > bash aliyun-ddns.sh)
      1.配置并执行脚本
      2.仅配置
      3.仅执行脚本
      4.恢复出厂设置
      5.清理日志文件
      0.退出
      
      • 带参数运行:
      aliyun-ddns.sh -config -run     配置并执行脚
      aliyun-ddns.sh -run             执行脚本(前提需要有配置文件)
      aliyun-ddns.sh -config          仅配置信息
      aliyun-ddns.sh -restore         恢复出厂设置(会清除配置文件等)
      aliyun-ddns.sh -clearn          清理日志文件
      aliyun-ddns.sh -version         显示脚本说明及版本信息
      
  • 实时监听外网IP变更后自动解析到域名
    • 利用定时任务服务:crond
    • 检测是否安装:crond -V 有输出版本号即已安装。
    • 如未安装:yum install vixie-cron crontabs -y 如有疑问请自行查找资料解决
    • crond服务常用命令
    service crond status   # 查看服务运行状态  
    service crond start    # 启动服务
    service crond stop     # 停止服务 
    service crond restart  # 重启服务  
    service crond reload   # 不中断服务,重新载入配置 
    crontab -e             # 编辑配置文件 
    crontab -l             # 列出某个用户的任务计划
    
    • 配置任务定时任务
      • 建议不要把任务执行频率设置小于等10,因为域名解析记录生效时间最短理论上是10分钟,往往都会超过10分,建议15-20分钟。
      • 开始配置
      crontab -e
      # 按i进入标记模式
      # 输入:
      */20 * * * * XXXXXX/aliyun-ddns-shell/src/aliyun/aliyun-ddns.sh -run >> XXXX/aliyun-ddns-shell/src/aliyun/crontab-log.log
      # 说明:
      # */20 * * * * :每20分钟执行一次 需要执行都脚本全路径 >> 执行日志输出位置全路径
      # 按 esc 后 输入:wq 回车 保存并退出
      
      • 重新加载配置:service crond reload
      • 注意观察任务是否成功执行,如有疑问请自行百度

效果图

启动页面 配置页面 运行页面

欢迎star给予支持

aliyun-ddns-shell's People

Contributors

boc-chi-no avatar jason23347 avatar liziwl avatar risfeng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

aliyun-ddns-shell's Issues

自定义获取wan ip的命令执行起来似乎有些问题?

在这行

var_local_wan_ip=`${var_local_wan_ip}`

我自定义获取wan ip的命令后

curl -s http://ip4.ouc.edu.cn/Default2.aspx | grep '访客IP地址: '  | sed 's/:/\n/g' | sed '1d' | sed 's/</\n/g' | sed '2d' | sed '2d' | sed '1,1 s/^.//g'

运行程序只能得到curl的结果,而不是后面的ip地址,结果附在最后

因此我只能手动改掉这一行,目前可以正常使用

var_local_wan_ip=`curl -s http://ip4.ouc.edu.cn/Default2.aspx | grep '访客IP地址: '  | sed 's/:/\n/g' | sed '1d' | sed 's/</\n/g' | sed '2d' | sed '2d' | sed '1,1 s/^.//g'` 
#var_local_wan_ip=`${var_local_wan_ip}`

附上curl的结果供参考

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>
</title></head>
<body>
    <form method="post" action="./Default2.aspx" id="form1">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="L07yESAT50CeA13U36h4ZrAtKEvwVaTD08lyqE+1H+/MSG9mIdDClItr060ShQ/na1++C44a3iy4Am4h0v9kG4QZDu9nVjl60a8rJkNBv7n0HCMvctexxEtouPdMl9R+Q9IJqq5JM41JYnl8ylzVbdWW2HPY6mndtuAor4t+Qj0=" />
<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="9BD98A7D" />
    <div>
        <pre id="best-content-2392302861" accuse="aContent" class="best-text mb-10" style="margin: 10px 0px; padding: 0px; font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; white-space: pre-wrap; word-wrap: break-word; font-size: 16px; line-height: 29px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; min-height: 55px;"><span id="Label1">访客IP地址: 10.190.10.125</span></pre>
    </div>
    </form>
</body>
</html>

原版程序的运行结果

root@***-145VM:~# sh /mnt/sdb1/App/aliddns/aliyun-ddns.sh -run
=========== 2022年 11月 23日 星期三 21:03:59 CST ===========

#############################################################
# Aliyun Domain DNS Update Shell Script
#
# Dynamic DNS Update Using Aliyun API
#
# Version: v1.0.0
# BuildDate: 20200307
# Author: risfeng<[email protected]>
# GitHub: https://github.com/risfeng/aliyun-ddns-shell
#
# Usage: please refer to https://github.com/risfeng/aliyun-ddns-shell/blob/master/README.md
#
#############################################################

[Info]    2022-11-23 21:03:59 检测到配置文件,自动加载现有配置信息。可通过菜单选项【恢复出厂设置】重置.
[Info]    2022-11-23 21:03:59 正在尝试检测外网:ping www.baidu.com 1 times......
[Success] 2022-11-23 21:03:59 检测外网成功!
[Info]    2022-11-23 21:03:59 正在获取本机外网ip......
[Info]    2022-11-23 21:03:59 本机外网ip:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>
</title></head>
<body>
    <form method="post" action="./Default2.aspx" id="form1">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="0qnytskylSD8Vy2l2Oa8+l96+6Myq4GX071uGNeVqOJBb1RYCnyLqYcqmqDEBpsb0W1fHltE8CSVKjEZZ7Ruy07S6vsuYBboFn2lbh29t77HgLAOrbAkIqBQbAdvGVBp0zp+u8iX5HXzE7xpf1xYRTolbuzzQ3ZOuNxesJpl5HM=" />
<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="9BD98A7D" />
    <div>
        <pre id="best-content-2392302861" accuse="aContent" class="best-text mb-10" style="margin: 10px 0px; padding: 0px; font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; white-space: pre-wrap; word-wrap: break-word; font-size: 16px; line-height: 29px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; min-height: 55px;"><span id="Label1">访客IP地址: 10.190.10.125</span></pre>
    </div>
    </form>
</body>
</html>
[Info]    2022-11-23 21:03:59 正在获取******的ip......
[Info]    2022-11-23 21:03:59 域名******的当前ip:
[Info]    2022-11-23 21:03:59 正在更新域名解析记录值......
[Info]    2022-11-23 21:03:59 正在获取record_id......
[Info]    2022-11-23 21:03:59 获取到到record_id=797644783185760256
[Info]    2022-11-23 21:03:59 正在更新解析记录:[******]的ip为[
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>
</title></head>
<body>
    <form method="post" action="./Default2.aspx" id="form1">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="0qnytskylSD8Vy2l2Oa8+l96+6Myq4GX071uGNeVqOJBb1RYCnyLqYcqmqDEBpsb0W1fHltE8CSVKjEZZ7Ruy07S6vsuYBboFn2lbh29t77HgLAOrbAkIqBQbAdvGVBp0zp+u8iX5HXzE7xpf1xYRTolbuzzQ3ZOuNxesJpl5HM=" />
<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="9BD98A7D" />
    <div>
        <pre id="best-content-2392302861" accuse="aContent" class="best-text mb-10" style="margin: 10px 0px; padding: 0px; font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; white-space: pre-wrap; word-wrap: break-word; font-size: 16px; line-height: 29px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; min-height: 55px;"><span id="Label1">访客IP地址: 10.190.10.125</span></pre>
    </div>
    </form>
</body>
]......
[Info]    2022-11-23 21:03:59 已经更新record_id=797644783185760256的记录
[Success] 2022-11-23 21:03:59 域名[******](新IP:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>
</title></head>
<body>
    <form method="post" action="./Default2.aspx" id="form1">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="0qnytskylSD8Vy2l2Oa8+l96+6Myq4GX071uGNeVqOJBb1RYCnyLqYcqmqDEBpsb0W1fHltE8CSVKjEZZ7Ruy07S6vsuYBboFn2lbh29t77HgLAOrbAkIqBQbAdvGVBp0zp+u8iX5HXzE7xpf1xYRTolbuzzQ3ZOuNxesJpl5HM=" />
<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="9BD98A7D" />
    <div>
        <pre id="best-content-2392302861" accuse="aContent" class="best-text mb-10" style="margin: 10px 0px; padding: 0px; font-family: &quot;PingFang SC&quot;, &quot;Lantinghei SC&quot;, &quot;Microsoft YaHei&quot;, arial, 宋体, sans-serif, tahoma; white-space: pre-wrap; word-wrap: break-word; font-size: 16px; line-height: 29px; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; min-height: 55px;"><span id="Label1">访客IP地址: 10.190.10.125</span></pre>
    </div>
    </form>
</body>
)记录更新成功。

无法泛解析

*.text.xx.com

Specified signature is not matched with our calculation. server string to sign is:GET ...xxxx

获取record_id为空,可能没有获取到有效的解析记录(record_id=)

运行出现如下错误
获取record_id为空,可能没有获取到有效的解析记录(record_id=)

[Info] 2020-04-16 23:01:24 检测到配置文件,自动加载现有配置信息。可通过菜单选项【恢复出厂设置】重置.
[Info] 2020-04-16 23:01:24 正在尝试检测外网:ping www.baidu.com 1 times......
[Success] 2020-04-16 23:01:24 检测外网成功!
[Info] 2020-04-16 23:01:24 正在获取本机外网ip......
[Info] 2020-04-16 23:01:24 本机外网ip:180.106.51.27
[Info] 2020-04-16 23:01:24 正在获取cloud.wx2share.com的ip......
[Info] 2020-04-16 23:01:24 域名cloud.wx2share.com的当前ip:0.0.0.0
[Info] 2020-04-16 23:01:24 正在更新域名解析记录值......
[Info] 2020-04-16 23:01:24 正在获取record_id......
[Warning] 2020-04-16 23:01:24 获取record_id为空,可能没有获取到有效的解析记录(record_id=)
[Failed] 2020-04-16 23:01:24 域名解析记录更新失败!
[Info] 2020-04-16 23:01:24 正在推送消息到钉钉......

些许诡异-不同的IP误判为相同

IP:124.229.236.43 和 IP:124.229.236.4。判断显示IP相同。

#############################################################

Aliyun Domain DNS Update Shell Script

Dynamic DNS Update Using Aliyun API

Version: v1.0.0

BuildDate: 20200307

Author: risfeng[email protected]

GitHub: https://github.com/risfeng/aliyun-ddns-shell

Usage: please refer to https://github.com/risfeng/aliyun-ddns-shell/blob/master/README.md

#############################################################

[Info] 2022-05-17 21:20:01 检测到配置文件,自动加载现有配置信息。可通过菜单选项【恢复出厂设置】重置.
[Info] 2022-05-17 21:20:01 正在尝试检测外网:ping www.baidu.com 1 times......
[Success] 2022-05-17 21:20:01 检测外网成功!
[Info] 2022-05-17 21:20:01 正在获取本机外网ip......
[Info] 2022-05-17 21:20:01 本机外网ip:124.229.236.4
[Info] 2022-05-17 21:20:01 正在获取‘’‘域名隐了’‘’的ip......
[Info] 2022-05-17 21:20:01 域名‘’‘域名隐了’‘’的当前ip:124.229.236.43
[Info] 2022-05-17 21:20:01 当前外网ip:[124.229.236.4]与‘’‘域名隐了’‘’(124.229.236.43)的ip相同.
[Success] 2022-05-17 21:20:01 本地ip与域名解析ip未发生任何变动,无需更改,程序退出.
=========== Tue 17 May 2022 09:40:01 PM CST ===========

TXT 记录更新不成功

执行脚本后显示更新成功,但没有更新。 初TXT类型之外,其他的A记录正常更新IP,
(因为TXT并不是IP,而是一个字符串?: AVDsoONpIlEdsMZTMZw4ERqQbAnVBQw-lpurWNaaSsc)
配置文件如下:
var_check_online_url="www.baidu.com"
var_check_online_retry_times=3
var_first_level_domain="quantowin.com"
var_second_level_domain="_acme-challenge"
var_domain_record_type="TXT"
var_domian_ttl="600"
var_access_key_id="XXXXX"
var_access_key_secret="XXXXXX"
var_local_wan_ip="cat /opt/acme/dnstxt"
var_domian_server_ip="curl -s http://119.29.29.29/d?dn="
var_enable_message_push=false
var_push_message_access_token=""
var_push_message_secret=""

执行日志:
[Info] 2023-08-01 14:19:14 检测到配置文件,自动加载现有配置信息。可通过菜单选项【恢复出厂设置】重置.
[Info] 2023-08-01 14:19:14 正在尝试检测外网:ping www.baidu.com 1 times......
[Success] 2023-08-01 14:19:14 检测外网成功!
[Info] 2023-08-01 14:19:14 正在获取本机外网ip......
[Info] 2023-08-01 14:19:14 本机外网ip:AVDsoONpIlEdsMZTMZw4ERqQbAnVBQw-lpurWNaaSsc
[Info] 2023-08-01 14:19:14 正在获取_acme-challenge.quantowin.com的ip......
[Info] 2023-08-01 14:19:14 域名_acme-challenge.quantowin.com的当前ip:
[Info] 2023-08-01 14:19:14 正在更新域名解析记录值......
[Info] 2023-08-01 14:19:14 正在获取record_id......
[Info] 2023-08-01 14:19:14 获取到到record_id=702846738585269248
]...... 2023-08-01 14:19:14 正在更新解析记录:[_acme-challenge.quantowin.com]的ip为[AVDsoONpIlEdsMZTMZw4ERqQbAnVBQw-lpurWNaaSsc
[Success] 2023-08-01 14:19:14 阿里云UpdateDomainRecord接口请求处理成功,返回消息:
[Info] 2023-08-01 14:19:14 已经更新record_id=702846738585269248的记录
)记录更新成功。08-01 14:19:14 域名[_acme-challenge.quantowin.com](新IP:AVDsoONpIlEdsMZTMZw4ERqQbAnVBQw-lpurWNaaSsc

uuid and ipv6 error

这个脚本写道1000行了, 厉害。 两个问题反馈以下:
1:uuid 可以用 cat /proc/sys/kernel/random/uuid 获得,而不需要特别安装 uuidgen
2:如果记录是四个A 的ipv6记录,用 curl 获取的外网地址只能是ipv4,这儿有错,应该用 curl -6 https://ipv6.icanhazip.com

IPV6直接解析一级域名和 解析签名错误

使用@.一级域名无法解析AAAA ipv6 地址;
返回:解析ipv6地址签名错误:
SignatureDoesNotMatch消息:Specified signature is not matched with our calculation. server string to sign is:GET&%2F&AccessKeyId%3***************%26Action%3DUpdateDomainRecord%26Format%3Djson%26RR%3Dstore%26RecordId%3D20504173030569984%26SignatureMethod%3DHMAC-SHA1%26SignatureNonce%3D91c79ecd-2d40-4aa8-87de-3ee628174274%26SignatureVersion%3D1.0%26TTL%3D600%26Timestamp%3D2020-10-03T05%253A49%253A40Z%26Type%3DAAAA%26Value%3D2408%25***************************%253Afe12%253A3456%26Version%3D2015-01-09(ackey ip 用*代替了)

ubuntu中运行aliyun-ddns.sh -run提示未找到命令

ubuntu中运行aliyun-ddns.sh -run提示未找到命令
只能通过 .aliyun-ddns.sh 或者 base aliyun-ddns.sh 来执行脚本,然后选择3.仅执行脚本 来更新
这样没法设置定时任务了,有其他方法吗,还是我哪里做的不对?

兼容arm小盒子

我遇到了两个问题
第一个
echo -ne "$2" | openssl dgst -$1 -hmac "$3" -binary | base64
我的小盒子没有base64 也不好装 改成如下
echo -ne "$2" | openssl dgst -$1 -hmac "$3" -binary | openssl base64

第二个
for((i=1;i<=$var_check_online_retry_times;i++)); do
我的shell 环境不支持 这个for语法改成如下
for i in {1..3};do

显示的:域名[*********](新IP:********************)记录更新成功,但是解析并没有发生变化

[Info] 2021-01-26 11:34:59 正在更新解析记录:[]的ip为[]......
[Warning] 2021-01-26 11:34:59 阿里云UpdateDomainRecord接口请求处理失败,返回代码:SignatureDoesNotMatch消息:Specified signature is not matched with our calculation. server string to sign is:GET&%2F&AccessKeyId%3D
**********************%26Action%3DUpdateDomainRecord%26Format%3Djson%26RR%3Draspberry%26RecordId%3D21145253099501568%26SignatureMethod%3DHMAC-SHA1%26SignatureNonce%3D1bd07010-8c1b-47c5-bd99-a244bae57594%26SignatureVersion%3D1.0%26TTL%3D600%26Timestamp%3D2021-01-26T03%253A35%253A05Z%26Type%3DAAAA%26Value%3D2409%253A8a20%253A5e61%253A4919%253Ad5c9%253A4be7%253Aa5ac%253A921f%26Version%3D2015-01-09
[Info] 2021-01-26 11:34:59 已经更新record_id=21145253099501568的记录

获取record_id报错

-e [Info] 2021-11-19 21:06:32 正在获取record_id......
curl: option -n%00Uafv7aR9XXMTJqYavIbDxGcUpdQ%3D%00: is unknown
curl: try 'curl --help' or 'curl --manual' for more information
-e [Warning] 2021-11-19 21:06:32 获取record_id为空,可能没有获取到有效的解析记录(record_id=)

请问该怎么解决

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.