DynDNS Script plus resolver

From CTWUG Wiki
Jump to: navigation, search

This script will allow your RB to update your DynDNS account should your ISP IP address change. For the variables, the inverted comma's must remain.

# Set needed variables
:local username "Insert Username here"
:local password "Insert account password here"
:local hostname "DynDNS hostname"
:global systemname [/system identity get name]
:if ($systemname  = "Site1" ) do= {
:set hostname "yourdomain1.dyndns.org"
}
:if ($systemname  = "Site2" ) do= {
:set hostname "yourdomain2.dyndns.org"
}
:if ($systemname  = "Site3" ) do= {
:set hostname "yourdomain3.dyndns.org"
}
: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"
: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 available at http://www.dyndns.com/developers/specs/syntax.html
:if (($currentIP != $previousIP) || ($dyndnsForce = true)) do={
   :set dyndnsForce false
   :set previousIP $currentIP
   /tool fetch user=$username password=$password mode=http address="members.dyndns.org" src-path="/nic/update?hostname=$hostname&myip=$currentIP" dst-path="/dyndns.txt"
   :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")
}


Please note that the scheduler should not be set too frequently else DynDNS will blacklist your account for abuse. I have tested that a 10min interval should be more than sufficient.


Once your dns account has been updated, your nat rule ip ( for the port forwarding ) will need to be updated as well should you want to access any devices you are forwarding to. The script below will do such.


:local result [/file get dyndns.checkip.html contents] 
:local resultLen [:len $result]
:local startLoc [:find $result ": " -1]
:set startLoc ($startLoc + 2)
:local endLoc [:find $result "</body>" -1]
:local resultIP [:pick $result $startLoc $endLoc]
:local natID [/ip firewall nat find comment="insert comment for your NAT/Port Forward rule"];
:local currentIP [/ip firewall nat get $natID dst-address];
:if ($resultIP != $currentIP) do={
  /ip firewall nat set $natID dst-address=$resultIP;
  :log info "NAT ip updated";
}

Thanks and hope this helps you guys.