run scrip on dhcp renew

Hi!
I have a script (ddns update) configured on the dhcp server which runs nicely when an IP is leased. But on lease renew it doesn’t run, which I would like it to.

The plan is to drop the dynamic hostnames from the dns after the TTL (the DNS doesn’t know the client is still there) so it needs to be informed on renewals.

Can you run the script on renewal of the lease??

And in addition … adding works with

/tool/dns-update zone=$domain name=$hostname  address=$leaseActIP    dns-server=192.168.1.100  ttl=$ttl

but as I understand, the DHCP server is responcible for removing the name at the DNS server. but there does not seems to be an option for that in the /tool/dns-update command (or is there?)

I’m a bit confused. Normally, you use DDNS on a /ip/dhcp-client script (not “DHCP server” mention in top post)…

First, /tool/dns-update uses the RFC scheme to update a DNS server, but few/none “cloud” DNS providers use that method. So /tool/dns-update pretty much work only with a classic BIND9 server (or unbound or perhaps self-hosted/“enterprise” DNS).

A “script-less” approach to DDNS update is just use /ip/cloud to enable DDNS. This will get you a DNS name like xxxx0b119yyy.sn.mynetname.net. And then you can manually add a “CNAME” record on custom DNS domain that point to the sn.mynetname.net name. So RouterOS will do the DDNS IP detection, and your own DNS domain just “aliases” the Mikrotik name to your own. This allow your custom DNS name to resolve, indirectly, via the CNAME to the Mikrotik’s DDNS name, which get the public IP from the router.

Otherwise, the DDNS provider you are using is needed, since different services have different APIs you need in a script. And typically /tool/fetch is needed to do the DDNS update for most services.

Hi,
Maybe a mix-up in terms then. I refer to Dynamic Updates where the DHCP server informs the DNS about which host leased which IP (and for how long).
Which works fine with the “/tool/dns-update” command.

ip dhcp-server
add address-pool=dhcppool1 dhcp-option-set=MyOptSet interface=\
    bridge-lan lease-script=" :if ( [ :len \$leaseActIP ] <= 0 ) do={ :error \"empty lease address\" }\
    \n:global DhcpScriptBound \$leaseBound\
    \n\
    \n:if ( \$leaseBound = 1 ) do=\\\
    \n{\
    \n  :local ttl\
    \n  :local domain\
    \n  :local hostname\
    \n  :local fqdn\
    \n  :local leaseId\
    \n  :local comment\
    \n\
    \n  /ip dhcp-server\
    \n    :set ttl [ get [ find name=\$leaseServerName ] lease-time ]\
    \n  /ip dhcp-server  network \
    \n    :set domain [ get [ find \$leaseActIP in address ] domain ]\
    \n  \
    \n  /ip dhcp-server lease\
    \n    :set leaseId [ find address=\$leaseActIP ]\
    \n    :set hostname [ get \$leaseId host-name ]\
    \n\
    \n  :local hrs [:pick \$ttl 0 2];\
    \n  :local min [:pick \$ttl 3 5];\
    \n  :local sec [:pick \$ttl 6 8];\
    \n  :set ttl [ ((\$hrs*3600) + (\$min*60) + \$sec) ];\
    \n\
    \n:global DhcpDebug \$leaseActIP;\
    \n\
    \n /tool/dns-update zone=\$domain name=\$hostname  address=\$leaseActIP    \
    dns-server=192.168.1.1  ttl=\$ttl\
    \n\
    \n}" lease-time=1h name=dhcpserver-lan

My point is I can add the host to the external DNS server but I can’t remove it when it expires.

Ah, you want to register any DHCP lease in DNS…

I believe you need an else={ … } in the :if ( $leaseBound = 1 ) { … } statement for the “expired” case. Or, add another :if ($leaseBound = 0) do={} work too for expired dhcp leases.

But your bigger problem is likely that /tool/dns-update does not support a “remove”, or even setting NXDOMAIN or something. So you can update record to 0.0.0.0 or something on expiration, but not remove the name AFAIK.

Thnx!
I was aware of needing the else block but did not built any because I lacked a remove command.
I tried an update to “0.0.0.0” but in the script above the hostname isn’t always (strange, sometimes, not always) found so it doesn’t work.
Also, when trying to ping a host that resolves to 0.0.0.0 the client uses 127.0.0.1 instead.
The testing continues

I continued the idea by expanding an existing script which is here :

http://forum.mikrotik.com/t/yet-another-dhcp-to-dns-script/107005/1