Page 1 of 1

Script dhcp2dns

Posted: Sat Oct 25, 2014 6:26 pm
by ovdeathiam
[/code2]Recently I had to configure dns entries for all hostnames in dhcp leases however scripts that I found usually recycle all static dns entries in a period of time, they need to manually set some parameters and dont take the advantage of the dhcp-server lease script feature.

I wrote this script and it seems to work well for multiple networks each with it's own domain setting set on a single router.
Can any of you guys take a look at it and check for any stability issues, or think of any scenarios in which it would not function properly?
Any ideas on how to optimise it are also welcome.

/ip dhcp-server lease-script

ros code

:if ($leaseBound=1) do={
    :foreach network in=[/ip dhcp-server network find] do={
        :local netmask [/ip dhcp-server network get value-name=netmask $network];
        :local leaseNetwork [:tostr ($leaseActIP & (~((0.0.0.1 << (32-$netmask))-1)))];

        :if ([/ip dhcp-server network find address ~ [:tostr $leaseNetwork]] = $network) do={
            :local leaseDomain [/ip dhcp-server network get value-name=domain $network];
            :local leaseHostname ([/ip dhcp-server lease get [/ip dhcp-server lease find where active-mac-address=$leaseActMAC]]->"host-name");
            :if ([:len $leaseHostname]!=0) do={
                /ip dns static add address=$leaseActIP name=($leaseHostname.".".$leaseDomain) ttl=5m;
            }
        }
    }
} else={
    /ip dns static remove [/ip dns static find where address ~ "$leaseActIP"];
}

Re: Script dhcp2dns

Posted: Mon Oct 27, 2014 5:15 pm
by macns
hi, I've been playing with this for a while, and I cannot get it to work,
The line
:if ([/ip dhcp-server network find address ~ [:tostr $leaseNetwork]] = $network) do={
is always false.

Re: Script dhcp2dns

Posted: Wed Oct 29, 2014 1:45 pm
by ovdeathiam
Is your netmask value set?

I forgot that netmask value is optional since we can just set the network address with netmask all in one value (i.e. 192.168.0.0/24).

I'll add getting the netmask value from network addr soon and repost the changed code. For now both netmask and domain values are obligatory for the script to work.

Re: Script dhcp2dns

Posted: Wed Oct 29, 2014 10:38 pm
by macns
yes I had that set from the start.
Maybe I missed something else, I'll take a look at it again when I have time.

btw, have you seen this thread?
http://forum.mikrotik.com/viewtopic.php?f=9&t=88746