本帖最後由 角色 於 2013-9-8 21:40 編輯
Another Script Link provided by member "bky16"- ############
- # 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)
- }
- }
複製代碼- New Terminal> /system scheduler add name=dyndns interval=00:01 on-event="/system script run dyndns\r\n"
複製代碼 Result 也 Okay- [admin@MikroTik] > /import dyndns-bk.rsc
- Opening script file dyndns-bk.rsc
- status: finished
- good
- Script file loaded and executed successfully
- [admin@MikroTik] >
複製代碼 |