2012年10月26日 星期五

RouterOS Dynamic DNS

以下是freeDNS.afraid.org的Script

:global AFRinterface "ppp-out1"
:global AFRlastip
:global AFRip [ /ip address get [/ip address find dynamic=yes interface=$AFRinterface ] address ]
:if ( [:typeof $AFRlastip]="nothing" ) do={:global AFRlastip 0.0.0.0/0 }
:if ( [:typeof $AFRip]="nothing" ) do={:log info ("Afraid-DDNS: No ip address present on " . $AFRinterface . ", please check.")
    } else={
    :if ($AFRip != $AFRlastip) do={
    :log info "AFR-DDNS: Sending UPDATE!"
    :log info [ /tool fetch address="freedns.afraid.org" host="freedns.afraid.org" mode=http src-path="dynamic/update.php\?修改為自己域名的編碼"  keep-result=no]
    :log info [ /tool fetch address="freedns.afraid.org" host="freedns.afraid.org" mode=http src-path="dynamic/update.php\?修改為自己域名的編碼"  keep-result=no]
    :log info [ /tool fetch address="freedns.afraid.org" host="freedns.afraid.org" mode=http src-path="dynamic/update.php\?修改為自己域名的編碼"  keep-result=no]
    :global AFRlastip $AFRip
    } else={
    :log info "AFR-DDNS: No change"
    }
    }
    :log info "AFR-DDNS: End"


以下是DynDNS
#定義 DynDNS 相關設定,以下自行修改:
#============================================================
:local DDNSUser "輸入DynDNS帳號"
:local DDNSPassword "輸入DynDNS密碼"
:local DDNSHost "輸入申請的網址名稱"
:local DDNSInterface "輸入網路介面名稱"
#============================================================

#以下程式如不了解,建議不要修改:
#------------------------------------------------------------------------------------------
#定義 IP 變數
:local DDNSIP
:global DDNSLastIP

#定義變數預設值
:if ([ :typeof $DDNSLastIP ] = nil ) do={ $DDNSLastIP "0.0.0.0" }

#取得網路介面 IP 位址
:set DDNSIP [ /ip address get [/ip address find interface=$DDNSInterface ] address ]
:set DDNSIP [ :pick $DDNSIP 0 [ :find $DDNSIP "/" ] ]

#判斷是否要更新 IP
:if ([ :typeof $DDNSIP ] = nil ) do={
    :log error ("DDNS DynDNS: 網路介面 " . $DDNSInterface . " 未取得 IP 位址")
} else={
    :if ($DDNSIP != $DDNSLastIP) do={
#        更新 DDNS IP
        :local url "/nic/update?hostname=$DDNSHost&myip=$DDNSIP&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG"
        /tool fetch address=members.dyndns.org src-path=$url mode=http user=$DDNSUser password=$DDNSPassword dst-path=("DDNS_DynDNS_$DDNSInterface")
        :delay 1

#        檔案內容
        :local filestr [ /file get "DDNS_DynDNS_$DDNSInterface" contents];

#        刪除檔案
        :local filename [ /file find name="DDNS_DynDNS_$DDNSInterface"];
        /file remove $filename

#        變更是否成功
        :if ( [ :find $filestr "good"] = "0" || [ :find $filestr "nochg"] = "0" ) do={
            :log warning ("DDNS DynDNS: 網路介面 " . $DDNSInterface . " 更新 IP 位址 " . $DDNSIP)
            :set DDNSLastIP $DDNSIP
        } else={
            :log error ("DDNS DynDNS: 網路介面 " . $DDNSInterface . " 更新 IP 位址失敗 " . $DDNSIP)
        }
    }
}
#------------------------------------------------------------------------------------------

沒有留言: