電訊茶室's Archiver

角色 發表於 2013-1-3 01:05

【RouterOS】——Dynamic DNS

[i=s] 本帖最後由 角色 於 2013-3-9 21:05 編輯 [/i]

Sources of information: [url]http://wiki.mikrotik.com/wiki/Dynamic_DNS_Update_Script_for_dynDNS[/url]

for example: DynDNS

1) add a script called dynDNS:[code]# Define User Variables
:global ddnsuser "DYNDNSUSER"
:global ddnspass "DYNDNSPASS"
:global ddnshost "DYNDNSHOST"

# 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 ] address ]

# Did we get an IP address to compare?
:if ([ :typeof $ddnsip ] = nil ) do={
   :log info ("DynDNS: No ip address present on " . $ddnsinterface . ", please check.")
} else={
  :if ($ddnsip != $ddnslastip) do={
    :log info "DynDNS: Sending UPDATE!"
    :local str "/nic/update?hostname=$ddnshost&myip=$ddnsip&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG"
    /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
  }
}[/code]2) Add a scheduler entry to run every minute[code]/system scheduler add name=dynDNS interval=00:01 on-event="/system script run dynDns\r\n"[/code]

角色 發表於 2013-1-5 16:00

是否有人用过MikroTik的DNS呢?怎样用都不成功。:L

bubblestar 發表於 2013-1-5 17:45

我用這個的。[code]# Set needed variables
:local username "ddns_username"
:local password "ddns_password"
:local hostname "ddyns.hostname"

:global dyndnsForce
:global previousIP

# print some debug info
:log info ("UpdateDynDNS: username = $username")
:log info ("UpdateDynDNS: password = $password")
:log info ("UpdateDynDNS: hostname = $hostname")
:log info ("UpdateDynDNS: previousIP = $previousIP")

# get the current IP address from the internet (in case of double-nat)
/tool fetch mode=http address="checkip.dyndns.org" src-path="/" dst-path="/dyndns.checkip.html"
:delay 1
:local result [/file get dyndns.checkip.html contents]

# parse the current IP result
:local resultLen [:len $result]
:local startLoc [:find $result ": " -1]
:set startLoc ($startLoc + 2)
:local endLoc [:find $result "</body>" -1]
:local currentIP [:pick $result $startLoc $endLoc]
:log info "UpdateDynDNS: currentIP = $currentIP"

# Remove the # on next line to force an update every single time - useful for debugging,
# but you could end up getting blacklisted by DynDNS!

#:set dyndnsForce true

# Determine if dyndns update is needed
# more dyndns updater request details http://www.dyndns.com/developers/specs/syntax.html

:if (($currentIP != $previousIP) || ($dyndnsForce = true)) do={
   :set dyndnsForce false
   :set previousIP $currentIP
   :log info "$currentIP or $previousIP"
   /tool fetch user=$username password=$password mode=http address="members.dyndns.org" \
      src-path="nic/update?system=dyndns&hostname=$hostname&myip=$currentIP&wildcard=no" \
      dst-path="/dyndns.txt"
   :delay 1
   :local result [/file get dyndns.txt contents]
   :log info ("UpdateDynDNS: Dyndns update needed")
   :log info ("UpdateDynDNS: Dyndns Update Result: ".$result)
   :put ("Dyndns Update Result: ".$result)
} else={
   :log info ("UpdateDynDNS: No dyndns update needed")
}
[/code]

角色 發表於 2013-1-5 18:18

如果我用Version 3.x, 而bubblestar就用Version 5.x。而我的routerOS就是5.22,那么应该用Version 5.x比较适合。

角色 發表於 2013-1-5 18:18

如果我用Version 3.x, 而bubblestar就用Version 5.x。而我的routerOS就是5.22,那么应该用Version 5.x比较适合。

角色 發表於 2013-1-5 19:06

[b]回復 [url=http://www.telecom-cafe.com/forum/redirect.php?goto=findpost&pid=29601&ptid=5148]3#[/url] [i]bubblestar[/i] [/b]

都是不能update DDNS。

怎样加呢?

1) 用edit把script做成一个DynDNS-5x.txt,然后ftp到router。
2) 在router, /file print能看到DynDNS-5x.txt, 输入适当的credential
3) /system add script DynDNS-5x.txt,
4)  /system run 0 (where 0 is given by /system script print)

bubblestar 發表於 2013-1-5 19:15

不知是否RouterOS 本身的問題,DDNS update 後,雖然見到新IP資料,但我差不多等了15分鐘左右,才能使用解決NAT loopback 問題,即是從local network,在Browser 內直接打 DDNS_Hostname 進入自己網內的NAS 資源。

感覺上RouterOS 的Update 沒有像一般Router一樣能即時反影新的設定。

bubblestar 發表於 2013-1-5 19:22

但用SSL 方式,[url]https://XXX.XXX.XXX:443[/url] 就可以馬上進入NAS 的資源,真不明所以。
而普通用 [url]http://xxx.xxx.xxx:8080[/url] 用了一會又很容易斷掉 connection.。

bubblestar 發表於 2013-1-6 12:57

假如是Dual Wan or Multiple Wan,如何把不同IP update 指向同一位址呢?

lttliang 發表於 2013-1-22 10:20

解决未呀?

角色 發表於 2013-1-22 13:26

现在还没有成功,因为没有时间。

bky16 發表於 2013-3-27 18:13

DDNS可以啦!

要确定係NEW TERMINAL度可以ping通域名
(这个经mrandrewchan CHING指导,原来係IP>DNS依度加8.8.8.8,唔好入去STATIC里面加)
行SCRIPT一定要搞掂依个,因为佢係透过DNS搵地址来UPDATE

其它,可以参照:
[url]http://www.telecom-cafe.com/forum/viewthread.php?tid=5427&pid=32082&page=5&extra=#pid32082[/url]

角色CHING可以update吓D教材啦!嘻嘻

角色 發表於 2013-3-28 21:24

[i=s] 本帖最後由 角色 於 2013-3-28 21:47 編輯 [/i]

[b]回復 [url=http://www.telecom-cafe.com/forum/redirect.php?goto=findpost&pid=29601&ptid=5148]3#[/url] [i]bubblestar[/i] [/b]

bubblestar兄这个也可以。但是是取最前面的WAN口的IP (WAN 1 IP),而不是Routerbaord的WAN IP (WAN 2 IP)。如果只有Routerboard自己,那么最前的WAN IP就是RouterBoard的WAN IP。这个script使用RouterBoard在最前面。

还有这个都是一样,第一先建立一个文件,把新的IP address记下来,下次的IP个这个IP不一样,就做一次update。如果一样就是bypass,根据不去dyndns的server check一下,是否与本身的IP address是一样?如果一样,不update,不一样就update。

图一:多个Routers
[attach]2343[/attach]

图二:只有一个RB
[attach]2344[/attach]

角色 發表於 2013-9-8 14:42

刚才再看这张thread,问题是那个script,放在哪里呢?

雯雯 發表於 2013-9-8 14:46

[b]回復 [url=http://www.telecom-cafe.com/forum/redirect.php?goto=findpost&pid=35066&ptid=5148]14#[/url] [i]角色[/i] [/b]

放在System -> Scripts裏.

角色 發表於 2013-9-8 15:16

不是放在Files?

角色 發表於 2013-9-8 15:34

按照#6帖的信息,然后manual行了一次都不行。

雯雯 發表於 2013-9-8 15:34

[b]回復 [url=http://www.telecom-cafe.com/forum/redirect.php?goto=findpost&pid=35068&ptid=5148]16#[/url] [i]角色[/i] [/b]

不是.

角色 發表於 2013-9-8 16:10

我用的不是DYNDNS主要domain,而是它的sub-domain,所以script需要某种改变才可以。

角色 發表於 2013-9-8 17:40

[b]回復 [url=http://www.telecom-cafe.com/forum/redirect.php?goto=findpost&pid=32084&ptid=5148]12#[/url] [i]bky16[/i] [/b]

我怎样做都不行!真的纳闷!

角色 發表於 2013-9-8 18:06

原来的我加的script从来都没有excute过,因为我在log看不到logged messagess。

角色 發表於 2013-9-8 21:07

终于把Dynamic DNS搞好了。主要是怎样execute问题。

1. 先用Windows Editor把script copied 到Editor里,然后save a file name. Extension可以用什么都可以,一般txt,或者rsc。
2. 怎样execute是一个非常重要的问题,就是用[code]/import file=script.rsc[/code]

角色 發表於 2013-9-8 21:14

[i=s] 本帖最後由 角色 於 2013-9-8 21:31 編輯 [/i]

在[url=http://www.telecom-cafe.com/forum/viewthread.php?tid=5148]#1[/url]帖的code,出现error message[code][admin@MikroTik] > /import file-name=dyndns-1.rsc
Opening script file dyndns-1.rsc

Script file loaded successfullyinput does not match any value of value-name
interrupted
[admin@MikroTik] >
[/code]

角色 發表於 2013-9-8 21:26

[i=s] 本帖最後由 角色 於 2013-9-8 21:32 編輯 [/i]

在[url=http://www.telecom-cafe.com/forum/redirect.php?goto=findpost&ptid=5148&pid=29601]#3[/url]帖的code,没有问题[code]
[admin@MikroTik] > /import file-name=dyndns-3.rsc
Opening script file dyndns-3.rsc

  status: finished

  status: finished

Dyndns Update Result: good 58.153.xxx.yyy
Script file loaded and executed successfully
[admin@MikroTik] >
[/code]

角色 發表於 2013-9-8 21:31

[i=s] 本帖最後由 角色 於 2013-9-8 21:40 編輯 [/i]

Another Script [url=http://www.telecom-cafe.com/forum/redirect.php?goto=findpost&ptid=5427&pid=32083]Link[/url] provided by member "bky16"[code]############
# Set needed variables
############
# Your DynDNS account user-name, password & host-name.
:local username "yourusername"
:local password "yourpassword"
:local hostname "yourhostname"

# Which interface are we checking if DHCP has changed? [interfaces | name column in winbox]
:local DHCPInterface "yourinterface"
############
# This is the name of the file where the last recorded IP will be stored, as well as the file name of the DynDNS response.
# Make sure if another script is using files, that these names don't conflict.
# You probably also want to be sure they aren't the same as any other files on the disk - because you'll be clobbering those files.
# Finally, while I'm not aware of any "reserved" file names on the disk, it would be wise to avoid something that might conflict.
# Prefixing them with something to associate them with this script would probably be a good thing.
# You can point these files at your SD-disk too, if you have an SD card installed and ready on your RB. Just prefix the path appropriately:
# something like this [:local vLastIPFileName "micro-sd/dyndns-lastip.txt"] (don't put a leading slash)
# A user reported a bug if you use the RB flash and prefix the path with a [/]
# So for RB based flash, it's probably safest to use just something like [:local vLastIPFileName "dyndns-lastip.txt"] and not [:local vLastIPFileName "/dyndns-lastip.txt"]
# You ***MUST*** use a .txt extension. Just an oddity of ROS. Move along now.
# Code ForDD
:local vLastIPFileName "dyndns-lastip.txt"
:local vDynDNSResponseFile "dyndns-resp.txt"
# /dyndns.txt

############
# END Set needed variables
############

:local vlen;
:local vRawIP;
:local vJustIP;
:local currentIP
:local previousIP
:local vlocalurl
:local vIPChanged false
:local dyndnsresult
:local result

# Check for file existance, if not create it
# We need to do this before we try to read or write to files...
:local check [/file find name=$vLastIPFileName]
:if ( $check = "" ) do= {
   /file print file=$vLastIPFileName
   :delay 2
   /file set [/file find name=$vLastIPFileName] contents=""
}

# Get the last saved IP from the file.
:set previousIP [/file get $vLastIPFileName contents]
#:put $previousIP


# print some debug info
:log info ("UpdateDynDNS: username = $username")
:log info ("UpdateDynDNS: password = $password")
:log info ("UpdateDynDNS: hostname = $hostname")
:log info ("UpdateDynDNS: previousIP = $previousIP")
:log info ("UpdateDynDNS: DHCPInterface = $DHCPInterface")


# This code gets the current IP from the interface.
# The interface we're checking should be set above.
# get the raw IP from the interface, which includes a mask.
:set vRawIP [:tostr [/ip address get [find interface=$DHCPInterface] address]]

#Strip the netmask off the vRawIP address.
:for i from=( [:len $vRawIP] - 1) to=0 do={
        :if ( [:pick $vRawIP $i] = "/") do={
                :set vJustIP [:pick $vRawIP 0 $i]
        }
}
:set currentIP $vJustIP

# Determine if dyndns update is needed
# more dyndns updater request details http://www.dyndns.com/developers/specs/syntax.html
# Update if the currentIP isn't equal to previousIP.
:if (($currentIP != $previousIP)) do={
        :log info "CurrentIP: $currentIP - PreviousIP: $previousIP"
        :set vlocalurl ("http://" . "$username" . ":" . "$password" . "@" . "members.dyndns.org" . "/nic/update?hostname=" . "$hostname" . "&myip=" . "$currentIP")   
        /tool fetch mode=http url=$vlocalurl dst-path=$vDynDNSResponseFile
        :set result [/file get $vDynDNSResponseFile contents]
        :set vIPChanged true
} else={
   :log info ("UpdateDynDNS: No dyndns update needed")
}

# Get the result status code and pull it out for use.
# IF IPChanged = true
#        Get result code
#                Check DynDNS result
#                if good or nochg, then
#                        :set previousIP $currentIP
#                        /file set [/file find name=$vLastIPFileName] contents="$currentIP"
#                        also post the result to the log file        
#                else badauth or !donator or notfqdn, nohost, abuse, dnserr, 911
#                        post to log file
#                        Don't update previousIP or vLastIPFileName file
:if ($vIPChanged=true) do={
        :set result [/file get $vDynDNSResponseFile contents]
        :local endLoc [:find $result " " -1]
        :set dyndnsresult [:pick $result -1 ($endLoc)]
        :put $dyndnsresult
                :if (($dyndnsresult = "good") || ($dyndnsresult = "nochg") ) do={
                        :set previousIP $currentIP
                        :log info ("UpdateDynDNS: Dyndns update needed")
                        :log info ("UpdateDynDNS: Dyndns Update Result: ".$result)
                        /file set [/file find name=$vLastIPFileName] contents="$currentIP"
                        } \
                else={
                        :log info ("UpdateDynDNS: Dyndns update needed")
                        :log info ("DynDNS Update FAILED")
                        :log info ("UpdateDynDNS: Dyndns Update Result: ".$result)
                }
}
[/code][code]
New Terminal> /system scheduler add name=dyndns interval=00:01 on-event="/system script run dyndns\r\n"[/code]Result 也 Okay[code][admin@MikroTik] > /import dyndns-bk.rsc
Opening script file dyndns-bk.rsc

  status: finished

good
Script file loaded and executed successfully
[admin@MikroTik] >
[/code]

角色 發表於 2013-9-9 11:33

现在bky16的script,一关电源,过了一晚,再试就没有把DNS更改。

那么估计还要修改一下。

角色 發表於 2013-9-13 22:20

现在正在测试bubblestar兄的script,看看关机后,再开是否可以把ip upgrade到最新的IP。

jerryzero 發表於 2013-9-19 10:09

我用1樓的SCRIPT去UPDATE DYNDNS, 幾個月前試都OK, 因為之前一直甪HKBB,所以IP都無轉過, 現在改用PCCW,就發現UPDATE唔到, 點睇SCRIPT是否成功.
角色C兄, 應該要把SCRIPT放到SCHEDULE去RUN, 我每幾分鐘RUN一次,
睇過SCHEDULE的確有RUN到, 但睇唔到個SCRIPT有無成功UPDATE..:'(

角色 發表於 2013-9-19 22:15

我也发现这个问题,问题是整个script都是看local file,而不是check DNS Server!所以等我有空的时候去学怎样去修改那段script,看看是否做到script去check DNS Server。

jerryzero 發表於 2013-9-20 19:06

[quote]我也发现这个问题,问题是整个script都是看local file,而不是check DNS Server!所以等我有空的时候去学怎 ...
[size=2][color=#999999]角色 發表於 2013-9-19 22:15[/color] [url=http://www.telecom-cafe.com/forum/redirect.php?goto=findpost&pid=35235&ptid=5148][img]http://www.telecom-cafe.com/forum/images/common/back.gif[/img][/url][/size][/quote]


    個script有check dyndns有無update到, 如果wan ip唔等於dyndns records就會update, 但係行完個script都無update到..

頁: [1] 2

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.