DynDns & Mikrotik

Spesso abbiamo la necessità di raggiungere un device da remoto e non sempre il nostro apparato ha un ip pubblico statico, pensate ad esempio ad un access point dietro una adsl con ip pubblico dinamico.

Ecco che ci viene in aiuto il servizio (gratuito per un host) DynDns, http://dyndns.it/.
Registriamoci quindi al servizio ed andiamo a configurare l’apparato Mikrotik.

Ecco lo script da inserire nel vostro mikrotik per renderlo raggiungibile a prescindere dall’ip pubblico che gi viene assegnato:
/system script
add name=dyndns policy=reboot,read,write,policy,test,password,sniff,sensitive source=”# Set needed variablesr
    n:local username “YOURUSERNAME”r
    n:local password “YOURPASSWORD”r
    n:global hostname “YOURDYNDNSNAME”r
    nr
    n:global dyndnsForcer
    n:global previousIPr
    n:global currentIPr
    nr
    n# print some debug infor
    n# :log info (“UpdateDynDNS: username = $username”)r
    n# :log info (“UpdateDynDNS: password = $password”)r
    n# :log info (“UpdateDynDNS: hostname = $hostname”)r
    n# :log info (“UpdateDynDNS: previousIP = $previousIP”)r
    nr
    n# get the current IP address from the internet (in case of double-nat)r
    n/tool fetch mode=http address=”checkip.dyndns.org” src-path=”/” dst-path=”/dyndns.checkip.html”r
    n:local result [/file get dyndns.checkip.html contents]r
    nr
    n# parse the current IP resultr
    n:local resultLen [:len $result]r
    n:local startLoc [:find $result “: ” -1]r
    n:set startLoc ($startLoc + 2)r
    n:local endLoc [:find $result “</body>” -1]r
    n:global currentIP [:pick $result $startLoc $endLoc]r
    n# :log info “UpdateDynDNS: currentIP = $currentIP”r
    nr
    n# Remove the # on next line to force an update every single time – useful for debugging,r
    n#  but you could end up getting blacklisted by DynDNS!r
    nr
    n#:set dyndnsForce truer
    nr
    n# Determine if dyndns update is neededr
    n# more dyndns updater request details http://www.dyndns.com/developers/specs/syntax.htmlr
    n:if (($currentIP != $previousIP) || ($dyndnsForce = true)) do={r
    n    :set dyndnsForce falser
    n    /tool fetch user=$username password=$password mode=http address=”members.dyndns.org” \r
    n       src-path=”/nic/update?hostname=$hostname&myip=$currentIP” dst-path=”/dyndns.txt”r
    n    :local result [/file get dyndns.txt contents]r
    n    :log info (“UpdateDynDNS: Dyndns update needed”)r
    n    :log info (“UpdateDynDNS: Dyndns Update Result: “.$result)r
    n    :put (“Dyndns Update Result: “.$result)r
    n} else={r
    n#    :log info (“UpdateDynDNS: No dyndns update needed”)r
    n}r
    nr
    n:set previousIP $currentIP”


Modificate adeguatamente i seguenti valori con le vostre credenziali DynDns e con il vostro url creato in fase di configurazione di un host nel portale DynDns:

    n:local username “YOURUSERNAME”r
    n:local password “YOURPASSWORD”r
    n:global hostname “YOURDYNDNSNAME”r

Creaiamo poi uno scheduler che automatizzerà l’esecuzione dello script:
/system scheduler
add disabled=no interval=10m name=”DynDNS” on-event=”DynDNS” policy=
    ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api start-time=startup
Buon divertimento e buona Pasqua!!

Condividi Read More…
>