Creating your own dynamic dns solution with linode dns & mikrotik

Hi,

I'm using pppoe connection with dynamic IP and most of the time I need to connect to my lab. (need to pay 3€/month extra for a static IP)

My pppoe connection is terminated on a mikrotik router box which is capable of running scripts on boot time.

So I've decided to find myself a solution. There was a solution in mikrotik wiki page but I didn't want to use dyndns.

Step 1:

For the domain choose minimal TTL values.

Step 2:

Create an A record on the domain with some dummy IP.

Step 3:

Here is the mikrotik code, put that into system\scripts:

:local username "admin" #change this
:local password "your-password-goes-here" #change this
:local hostname "kozy-sol" #change this

:global previousIP
:global enforceUpdate

#to debug:
:set enforceUpdate true

# print some debug info 
:log info ("UpdateDNS: username = $username")
:log info ("UpdateDNS: hostname = $hostname")
:log info ("UpdateDNS: previousIP = $previousIP")

# get the current IP address from the internet
/tool fetch mode=http address="yourdomain.com" src-path="/currentip.php" dst-path="/ip.html" #change this
:local result [/file get ip.html contents]

# parse the current IP result
:local resultLen [:len $result]
:local startLoc [:find $result ": " -1]
:set startLoc ($startLoc + 2)
:local endLoc [:find $result "" -1]
:local currentIP [:pick $result $startLoc $endLoc]
:log info "UpdateDNS: currentIP = $currentIP"

# Determine if dns update is needed
:if (($currentIP != $previousIP) || ($enforceUpdate = true)) do={
    :set enforceUpdate false
    :set previousIP $currentIP
    /tool fetch user=$username password=$password mode=http address="yourdomain.com" src-path="/updateip.php?hostname=$hostname&ip=$currentIP" dst-path="/dnsupdate.txt" #change this
    :local result [/file get dnsupdate.txt contents]
    :log info ("UpdateDNS: Update needed")
    :log info ("UpdateDNS: Update Result: ".$result)
    :put ("Dns Update Result: ".$result)
} else={
    :log info ("UpdateDNS: No update needed")
}

Step 4:

Put this file as updateip.php

Step 5:

Put this file as currentip.php

<title>Current IP</title>Current IP Address: 

Please note that I'll update this tutorial with more description, better code and screenshot if anyone is interested.

For the first day, I wanna see if this is something that needed by anyone else.

Thanks for reading!

Resource:

https://wiki.mikrotik.com/wiki/Dynamic_ … behind_NAT">https://wiki.mikrotik.com/wiki/DynamicDNSUpdateScriptfordynDNSbehind_NAT

4 Replies

Cool. Suggestion:

# get the current IP address from the internet

There is no need for this, or the external script to get your IP address.

https://www.linode.com/api/dns/domain.resource.update for Target: "When Type=A or AAAA the token of '[remote_addr]' will be substituted with the IP address of the request.".

So, simply set target="[remoteaddr]" (literally - no substitution) and the API will assign the target to the remoteaddr of the request.

-Chris

@caker:

Cool. Suggestion:

# get the current IP address from the internet

There is no need for this, or the external script to get your IP address.

https://www.linode.com/api/dns/domain.resource.update for Target: "When Type=A or AAAA the token of '[remote_addr]' will be substituted with the IP address of the request.".

So, simply set target="[remoteaddr]" (literally - no substitution) and the API will assign the target to the remoteaddr of the request.

-Chris

I was also thinking of this revision :)

change you script for something like this:

:local token "";
:local domainid "";
:local resourceid "";
:local api "https://api.linode.com/v4/domains/";
:local serial [/system routerboard get serial-number];
:local rbname "rb-$serial"

/tool fetch http-method=put http-header-field="content-type:application/json,Authorization:Bearer $token" url="$api/$did/records/$rid" http-data="{\"type\":\"AAAA\",\"name\":\"$rbname\",\"target\":\"[remote_addr]\",\"ttl_sec\":\3600}";

I wrote this recently, and it's been working great. I have it in a cron job that every 30 minutes.

https://gist.github.com/christr/9de75da7fd1ffbd1aa4f0b33316fff8c

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct