User Tools

Site Tools


networking:router:mikrotik_dyndns

This is an old revision of the document!


Mikrotik RouterOS Dynamic DNS

Script

:!: For some reason, this script wouldn't import via the CLI. I had to copy and paste it into Winbox or the web UI.

# Set needed variables
:local username "sonoracomm"
:local password "105700"
:local hostname "gcooper.dynalias.org"

:global dyndnsForce ""
:global previousIP "$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)
#/tool e-mail send from=email@gmail.com server=173.194.77.108 to=me@gmail.com subject="Current IP is $currentIP"
#:log info "Email of new IP address sent"
} else={
:log info ("UpdateDynDNS: No dyndns update needed")
}

Create a Schedule

/system scheduler add comment="Update DynDNS" disabled=no interval=10m name=dyndns \
  on-event=dyndns policy=reboot,read,write,policy,test,password,sniff,sensitive \
  start-date=jan/01/1970 start-time=12:34:56;
networking/router/mikrotik_dyndns.1380255012.txt.gz · Last modified: 2013/09/26 22:10 by gcooper