基于腾讯云/阿里云解析为RouterOS配置DDNS服务

Jinpeng
Jinpeng
管理员
40
文章
0
粉丝
MikroTik基于腾讯云/阿里云解析为RouterOS配置DDNS服务已关闭评论673阅读模式

上次使用萌咖佬的华为云API为RouterOS配置了ddns脚本,可谓是一步到位。今天JinPeng带着各位来通过阿里云解析或者腾讯云解析为RouterOS配置ddns脚本,让你除了华为云解析外有更多的ddns选择。

基于腾讯云/阿里云解析为RouterOS配置DDNS服务

获取阿里云或者腾讯云的AccessKeyId和AccessKeySecret的内容不多于赘述,直接上脚本。

注意事项:以下脚本不会自动创建子域名,要更新的子域名必须事先添加好。

服务器端源代码看这里:https://github.com/vibbow/routeros-ddns-script

ipv4 ddns脚本

# 域名
:local domainName "sub.example.com";
# wan接口名称
:local wanInterface "ether1";
# 要使用的服务 (aliyun/dnspod)
:local service "aliyun";
# API接口 Access ID
:local accessID "";
# API接口 Access Secret
:local accessSecret "";


# 腾讯云 (dnspod) 设置
#
# 一般情况下无需设置此内容
# 服务器会自动识别 domainID 和 recordID
#
# 如一直提示 "当前域名无权限,请返回域名列表。"
# 则需要手动设置
:local domainID "";
:local recordID "";


# ==== 以下内容无需修改 ====
# =========================

:local publicIP;
:local dnsIP;
:local epicFail false;

# 获取当前外网IP
:do {
  :local interfaceIP [ /ip address get [ find interface=$wanInterface ] address ];
  :set $interfaceIP [ :pick $interfaceIP 0 [ :find $interfaceIP "/" ] ];

  :if ($interfaceIP ~ "^(10|100|172|192)\\.") \
  do={
    :local fetchResult [/tool fetch url="http://ip.3322.net/" mode=http as-value output=user];
    :set $publicIP ($fetchResult->"data")
    :set $publicIP [ :pick $publicIP 0 [ :find $publicIP "\n" ] ];
    :set $publicIP [ :toip $publicIP ]
  } \
  else={ \
    :set $publicIP [ :toip $interfaceIP ];
  }
} \
on-error {
  :set $epicFail true;
  :log error ("DDNS: Get public IP failed.");
}

# 获取当前解析的IP
:do {
  :set $dnsIP [ :resolve $domainName ];
} \
on-error {
  :set $epicFail true;
  :log error ("DDNS: Resolve domain " . $domainName . " failed.");
}

# 如IP有变动,则更新解析
:if ($epicFail = false && $publicIP != $dnsIP) \
do={
    :local callUrl ("https://ddns.vsean.net/ddns.php");
    :local postData ("service=" . $service . "&domain=" . $domainName . "&access_id=" . $accessID . "&access_secret=" . $accessSecret . "&domain_id=" . $domainID . "&record_id=" . $recordID);
    :local fetchResult [/tool fetch url=$callUrl mode=https http-method=post http-data=$postData as-value output=user];
    :log info ("DDNS: " . $fetchResult->"data");
}

ipv6 ddns脚本

# 域名
:local domainName "sub.example.com";
# wan接口名称
:local wanInterface "bridge";
# 要使用的服务 (aliyun/dnspod)
:local service "aliyun";
# API接口 Access ID
:local accessID "";
# API接口 Access Secret
:local accessSecret "";


# 腾讯云 (dnspod) 设置
#
# 一般情况下无需设置此内容
# 服务器会自动识别 domainID 和 recordID
#
# 如一直提示 "当前域名无权限,请返回域名列表。"
# 则需要手动设置
:local domainID "";
:local recordID "";


# ==== 以下内容无需修改 ====
# =========================

:local publicIP;
:local dnsIP;
:local epicFail false;

# 获取当前接口IPv6地址
:do {
  :local interfaceIP [ /ipv6 address get [ :pick [ find interface=$wanInterface global ] 0 ] address ]
  :set $interfaceIP [ :pick $interfaceIP 0 [ :find $interfaceIP "/" ] ];

  :set $publicIP [ :toip6 $interfaceIP ];
} \
on-error {
  :set $epicFail true;
  :log error ("DDNSv6: Get public IP failed.");
}

# 获取当前解析的IP
:do {
  :set $dnsIP [ :resolve $domainName ];
} \
on-error {
  :set $epicFail true;
  :log error ("DDNSv6: Resolve domain " . $domainName . " failed.");
}

# 如IP有变动,则更新解析
:if ($epicFail = false && $publicIP != $dnsIP) \
do={
    :local ddns6Domain "ddns.vsean.net"
    :local ddns6IP [ :resolve $ddns6Domain ]
    :local callUrl ("https://[" . $ddns6IP . "]/ddns.php");
    :local postData ("service=" . $service . "&domain=" . $domainName . "&access_id=" . $accessID . "&access_secret=" . $accessSecret . "&domain_id=" . $domainID . "&record_id=" . $recordID);
    :local fetchResult [/tool fetch url=$callUrl host=$ddns6Domain mode=https http-method=post http-data=$postData as-value output=user];
    :log info ("DDNSv6: " . $fetchResult->"data");
}

文章末尾固定信息

weinxin
我的微信
微信号已复制
我的微信
微信扫一扫
 
Jinpeng
  • 本文由 Jinpeng 发表于2024年6月9日 09:17:49
  • 转载请务必保留本文链接:https://www.ixmu.net/123.html