How to do Dynamic DNS updating through DynDNS

Last updated: March 20, 2013 at 18:31 pm – changed txt and rsc script links to Dropbox

Setting up DynDNS updating on a Mikrotik is a two-step process.  Putting the Script into the router, and setting the Scheduler to run the script at timed intervals.  The good thing is you have complete control over what the script is doing and when it is doing it.  This is the reason we picked up Mikrotik in the first place.  DynDNS updating wasn’t working correctly on other different routers and DVRs.  I never saw any other consumer-grade router where you could even set the update interval.  Consequently we had a lot of job sites that would lose connection until whenever the router/DVR decided to run another update.  Not the case with Mikrotik.

The Steps

First thing you should do is make sure you are logged in with a permanent login.  If you are logged in as ‘admin’ and you plan on changing your login to something else and deleting/disabling the ‘admin’ login, do that first.  The script sets up variables that are saved internally and assigns your current login as the ‘owner’.  If the owner of the script and variables are ‘admin’ and you delete the ‘admin’ login… you could have problems down the road.  I now change my login before I do anything else in the router.  Add your login and password, exit Winbox, log in with your new credentials.  Delete the ‘admin’ user if you want to.  (I always do…)

Go to System / Scripts.  Add a new Script.  Name it something simple like “dyndns”.  You will refer to the name of the script when you set it to run in the Scheduler.

Copy and paste this text file into the Source text box:
dyndns script

Or import this into an .rsc file and it will set up the script at 10 minute scheduler for you (download, make sure it gets saved with rsc file extension, read how to import an rsc file):

dyndns and scheduler script

Your DynDNS login, password, and hostname

You need to enter your login and password in the “:local username” and “:local password” sections.  This is the exact same login you use to get into your DynDNS account at www.dyndns.com.  It’s important to note that if you ever change your password on DynDNS’s site you will have to go back through ALL your jobsites that use DynDNS updating.  This isn’t a Mikrotik thing, it’s just how DynDNS works.  So pick a good password and stick with it.

The third section to change is “:local hostname”.  This is the URL you are assigning to your jobsite.  For us, we use customername.ourcompanyname.net.  Whatever it is that you are using goes in here.  Do NOT enter the “http://”.  It’s not needed.  You have to have the URL set up through DynDNS before you run this script, so go ahead and get that done…

Testing it for the first time

You definitely want to test the script first.  There could be a formatting error you missed that becomes a bear to track down later.  From the Script window where you pasted the text, click Run Script.  Now if you go to the Files window, you should see a new file called dyndns.txt.  If this file is there then your script is working.  If not, go back and check your script formatting, your login and password, and that your URL is correct.  Go back and click Run Script again.  This time another new file should appear, called dyndns.checkip.html.  You can also check your DynDNS account and see that your new URL was just updated at the time and date that you clicked the Run Script button.

Now that you have a working DynDNS updater…

… you need to tell the router when you want it to update.  Right now you just have a script.  It only runs when you click Run Script.  Nothing else is telling that script to run.

Go to System / Scheduler and add a new Schedule.  Name it something simple like “DDNS updater”.  Click on Start Time and select startup.  Go to Interval and change it to 00:10:00.  (In Mikrotik-speak, that formatting always means hours:minutes:seconds.)  That will make the router run your script every 10 minutes.

Go to the On Event box and type the exact name of the script you created.  If you named it dyndns, then just enter dyndns and nothing else.

Hit OK or Apply.  You can see the Run Count and the next scheduled event time from the Scheduler window.  It should now read 10 minutes from when you clicked OK or Apply.

Do not set the Interval to less than 10 minutes!

If you do, you will be violating DynDNS’s Terms of Service and they can blacklist you.  If that happens, all those jobsites you set up will stop updating…  You don’t want that.

That’s it!

Last updated: March 20, 2013 at 18:31 pm

Tagged:

Comments

  1. Springs

    Pictures and screen shots Jim. This is the second time now!

    I think the format of what I sent with your “spin” on it is the way to go.

  2. Springs

    Duhhhh….

    Use Netwatch.

    Use my runoce. Have netwatch enable it when the interface come up. Delay 00:01:00 then disable.

    Simple

  3. J.O.

    I had to add a # in front of a line to disregard it. Here is the line with the # added…
    # :log info “UpdateDynDNS: currentIP = $currentIP”

    I asked this question on IP and Jim helped me figure this out after comparing scripts.

    1. admin Article Author

      Updated. Formatting is still a little off. Let me know if you have any trouble doing copy/paste from it.

  4. cory

    I finally got my dyndns account up and working, confirmed, but this script still isn’t working. I’m guessing it is a formatting error???

    My script looks exactly as posted above, just replacing the red text in quotes, with my account info in quotes. Any ideas?

    1. admin Article Author

      go in Terminal and do a /system script export and copy/paste that script here (but change your IP, login, password first…)

      1. cory
        # dec/11/2012 15:25:49 by RouterOS 5.20
        # software id = UB2U-EHT1
        #
        /system script
        add name=dyndns policy=\
            ftp,reboot,read,write,policy,test,winbox,password,sniff,api source=":delay\
            \_00:00:59\r\
            \n\r\
            \n# Set needed variables\r\
            \n:local username \"myusername\"\r\
            \n:local password \"mypassword\"\r\
            \n:local hostname \"mydyndnshostaddress\"\r\
            \n:global systemname [/system identity get name]\r\
            \n\r\
            \n:global dyndnsForce\r\
            \n:global previousIP\r\
            \n:local resolvedIP [:resolve \$hostname]\r\
            \n\r\
            \n# print some debug info WHEN YOU F___ THIS UP... THIS WILL HELP YOU FIND\
            \_WHERE.\r\
            \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\
            \n#:log info (\"UpdateDynDNS: resolvedIP = \$resolvedIP\")\r\
            \n\r\
            \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\
            \n\r\
            \n# parse the current IP result\r\
            \n:local resultLen [:len \$result]\r\
            \n:local startLoc [:find \$result \": \" -1]\r\
            \n:set startLoc (\$startLoc + 2)\r\
            \n:local endLoc [:find \$result \"\" -1]\r\
            \n:local currentIP [:pick \$result \$startLoc \$endLoc]\r\
            \n#:log info \"UpdateDynDNS: currentIP = \$currentIP\"\r\
            \n\r\
            \n# Remove the # on next line to force an update every single time - usefu\
            l for debugging,\r\
            \n# but you could end up getting blacklisted by DynDNS!\r\
            \n#Edit: Not really needed anymore... the result is not equal... Update wi\
            ll happen.\r\
            \n\r\
            \n#:set dyndnsForce true\r\
            \n\r\
            \n# Determine if dyndns update is needed\r\
            \n# more dyndns updater request details http://www.dyndns.com/developers/s\
            pecs/syntax.html\r\
            \n#This is where we check the DNS record against actual result. Thanks to \
            jimstolz76\r\
            \n:if ((\$currentIP != \$resolvedIP) || (\$dyndnsForce = true)) do={\r\
            \n:set dyndnsForce false\r\
            \n:set previousIP \$currentIP\r\
            \n/tool fetch user=\$username password=\$password mode=http address=\"memb\
            ers.dyndns.org\" \\\r\
            \nsrc-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 (\"Thanks Springs! Update Result: \".\$result)\r\
            \n:put (\"Dyndns Update Result: \".\$result)\r\
            \n} else={\r\
            \n:log info (\"UpdateDynDNS: No dyndns update needed\")\r\
            \n}"
        add name=update_ntp policy=\
            ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api \
            source=":delay 60s\r\
            \n/system ntp client set primary-ntp=[:resolve 0.pool.ntp.org]\r\
            \n/system ntp client set secondary-ntp=[:resolve 1.pool.ntp.org]"
        
          1. cory

            Yes entering by just replacing the red text and keeping the quotes so and example would be:

            “coryburgess”
            “password0123″
            “coryshouse.bdidns.com”

            where “bdidns.com” is my registered dyndns zone and “coryshouse” would be the added host. I’ve confirmed the dyndns path is working via my mobile app for crestron. I have trippled checked the spelling for these three things.

          2. admin Article Author

            Try running the script from terminal. Go to /system scripts, then type ‘print brief’. Each script has a number. Type ‘run 3′ if the dyndns script is number 3.
            It should kick back a syntax error if something is formatted wrong.
            Also, if you print your scripts from terminal it will highlight the first character of a word it doesn’t understand. It’s incredibly useful for troubleshooting.

  5. Springs

    The spacing is wrong if you cut and paste.

    :if (($currentIP != $resolvedIP) || ($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 (“Thanks Springs! Update Result: “.$result)
    :put (“Dyndns Update Result: “.$result)
    /ip firewall nat set [find comment~"FUCKUPDATE"] dst-address=”$previousIP”
    } else={
    :log info (“UpdateDynDNS: No dyndns update needed”)
    }

  6. Pingback: Hairpin NAT – or how to use your DynDNS address internally or externally | Networking For Integrators

  7. antonio

    hello,
    I still have not tried the script, but I’m interested in installing 7 on my router, a question, but you might implement a notification email when changing or updating the ip

  8. antonio

    hello,
    I still have not tried the script, but I’m interested in installing 7 on my router, a question, but you might implement a notification email when changing or updating the ip

    1. admin Article Author

      Good catch, Pedro! You’re right, the txt file was wrong – it had an HTML tag that got removed by WordPress. The rsc script file was correct the whole time. Actually it was the same problem they had on IP – the HTML tag didn’t show in the post.

      The txt file is updated now, good to go.

  9. Pedro Diaz

    Jim on the explanation of how to implement the updater you have a section that reads:
    [quote]
    The third section to change is “:local hostname”. This is the URL you are assigning to your jobsite.
    [/quote]

    I see global hostname and not local hostname . I think this was from an older version of the script.

  10. Pedro Diaz

    OK, I’m having an issue trying to have this script in my router. If I drag and drop the rsc file created with Notepad into the files section the size of the file is 0 KB.
    If I try to create a new script, copy and paste the content of the text into the source window the script is not created, any ideas?

  11. Cory

    I used the new RSC file to import and try this again. The first time I ran the script I got a new HTML file in the FILES section, but no text file. I went and ran again and nothing new. I tried using the Dropbox link to copy paste and redo the script, but now nothing new happens.

    I then deleted the HTML file that was created and ran again…still nothing.

    I re-imported the RSC file and ran the script and now nothing at all. No HTML and no Text file and no update according to my dyndns account.

    I compared the formatting and text to that of the dropbox linked text file and all looks the same.

Leave a Reply

Your email address will not be published. Required fields are marked *


seven + = 14

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Warning: Illegal string offset 'solo_subscribe' in /home/jimsto5/public_html/wp-content/plugins/subscribe-to-comments/subscribe-to-comments.php on line 304

Subscribe without commenting