Question regarding DHCP-DNS scripting

Whenever you search for DHCP dynamic DNS scripts you find extremely complex examples like this:

https://wiki.mikrotik.com/wiki/Setting_static_DNS_record_for_each_DHCP_lease

However, before finding that, I wrote my own that seems to be working fine:

:local domain;
:local hostname;
:local recordExists;

:set domain "lan.leadtree-engineering.io";
:set hostname "$"lease-hostname".$domain";
:set recordExists [:len [/ip dns static find name=$hostname]];

:log info "fqdn: $hostname";
:log info "ip: $leaseActIP";
:log info "registering: $leaseBound";
:log info "matching records: $recordExists";

:if (1 = $leaseBound && 0 = $recordExists) do={
    :log info "host doesn't exist and is registering, adding";
    /ip dns static add name=$hostname address=$leaseActIP;
}

:if (1 = $leaseBound && 0 < $recordExists) do={
    :log info "host already exists and is registering, updating";
    /ip dns static set [find name=$hostname] address=$leaseActIP;
}

:if (0 = $leaseBound && 0 < $recordExists) do={
    :log info "host already exists and is deregistering, removing";
    /ip dns static remove [find name=$hostname];
}

:if (0 = $leaseBound && 0 = $recordExists) do={
    :log info "hostname doesn't exist and is deregistering, there's nothing to do";
}

So, am I missing something? Is there a reason this isn’t the “right way”?

Thanks!

I have not tested your script, but some tips.

No need for semicolon ; at the end of each line, only between commands on same line.

Change this:

:log info "fqdn: $hostname";
:log info "ip: $leaseActIP";
:log info "registering: $leaseBound";
:log info "matching records: $recordExists";

to

:log info "fqdn=$hostname ip=$leaseActIP registering=$leaseBound matching_records=$recordExists"

So that logs are not spammed with more lines than needed.

If possible, always look how old are examples you find. The linked one was last modified in 2011, which was way before support for lease scripts was added. In other words, old scripts or other configs are often unnecessarily complex from today’s perspective.