[教學][Scripts]Dynamic DNS Update Script for (DynDNS-O)

RouterOS有一個內建的dns-update工具
是用來把IP更新到DDNS上的

這篇文章主要是針對RouterOS更新到DynDNS上所寫的
(這是官方的Scripts.會自行查詢那個interface是主要NAT後,就會自動設成用這個interface update ip to DDNS)
把主要的Script改一下即可。

GUI Mode 進入
RouterOS > Menu > System > Scripts [Add Scripts]

CLI Mode 進入
[admin@MikroTik] > system script add name=ddns-dyndns

相關的Script如下

# Define User Variables
:global ddnsuser "你的DynDNS帳號"
:global ddnspass "你的DynDNS密碼"
:global ddnshost "你的完整網址名(例如:abcd.ath.cx)"
 
# Define Global Variables
:global ddnsip
:global ddnslastip
:if ([ :typeof $ddnslastip ] = nil ) do={ :global ddnslastip "0" }
 
:global ddnsinterface
:global ddnssystem ("mt-" . [/system package get system version] )
 
# Define Local Variables
:local int
 
# Loop thru interfaces and look for ones containing
# default gateways without routing-marks
:foreach int in=[/ip route find dst-address=0.0.0.0/0 active=yes ] do={
  :if ([:typeof [/ip route get $int routing-mark ]] != str ) do={
     :global ddnsinterface [/ip route get $int interface]
  }
}
 
# Grab the current IP address on that interface.
:global ddnsip [ /ip address get [/ip address find interface=$ddnsinterface ] addr$
 
# Did we get an IP address to compare?
:if ([ :typeof $ddnsip ] = nil ) do={
   :log info ("DynDNS: No ip address present on " . $ddnsinterface . ", please che$
} else={
  :if ($ddnsip != $ddnslastip) do={
    :log info "DynDNS: Sending UPDATE!"
    :local str "/nic/update?hostname=$ddnshost&myip=$ddnsip&wildcard=NOCHG&mx=NOCH$
    /tool fetch address=members.dyndns.org src-path=$str mode=http user=$ddnsuser \
        password=$ddnspass dst-path=("/DynDNS.".$ddnshost)
    :delay 1
    :local str [/file find name="DynDNS.$ddnshost"];
    /file remove $str
    :global ddnslastip $ddnsip
  }
}

新增後還要在 scheduler 中設定每分鐘運行一次
GUI
RouterOS > Menu > System > Scheduler [Add Scheduler]
Name: ddns-update
Start Time: startup
Interval: 00:01:00
On Event: /system script run ddns-dnsdiy

CLI
[admin@MikroTik] > system scheduler add name=ddns-update interval=00:01 on-event="/system script run ddns-dyndns\r\n"

關於 cyrus