DHCP Binding Triggers Script

I currently have a script that adds bound DHCP entries to an address list, this script is set to run every 30 seconds. Rather than have it scheduled to run every 30 seconds is it possible to have another script that parses the logs and searches for an entry with “Msg-Type = ack” in it then runs the first script? If so can someone assist with the creation of that script?

Hey, you could attach a script to dhcp server, to do something on ip assignment.

See https://wiki.mikrotik.com/wiki/Manual:IP/DHCP_Server#General → lease-script (string; Default: “”)

I tried this, no luck, did I do something wrong?

#Add Lease to Address List
#DCHP Script
 
 
:if ($leaseBound = "1") do={
    /ip firewall address-list add list=DHCP-WL address=($leaseActIP) timeout="03:00:00"
}

To start with (debug phase), try logging the values received.
You could also test the script by invoking it manually, and when happy paste/call it.

It worked actually, just only for new entries, I was reconnecting a current entry to the network and the lease was refreshing but it was not triggering the script. Do you know what tweaks I would need to make to get it to refresh current leases?

If it behaves like the dhcp client part, it will only be triggered on assignment, de-assignment & change (!=previous ip).
Hack: remove all current leases :wink:

My worry is once the timeout expires on the address list, will it get re-added to the address list if the lease is still active?

Instead of running the script at the DHCP script I just had to call the system script that I was running on a schedule before. Got it working as I need.

Thanks for the help.






#Add Lease to Address List
 
 
:if ($leaseBound = "1") do={
    /system script run 0
}

Good to hear. Note: you might want to run script by name, so you won’t brake it in future
/system script run

Edit: indeed a “typo”

/system script run :slight_smile:

When triggers those scripts to run? I removed a static entry from lease. Re-added and host bounded , received IP and still the scripts under /dhcp-server lease-script didn’t run


[admin@Mike[1]] /user ssh-keys private> /ip dhcp-server print detail
Flags: D - dynamic, X - disabled, I - invalid
 0    name="USER_LAN" interface=USER lease-time=10m address-pool=static-only bootp-support=dynamic bootp-lease-time=forever authoritative=after-2sec-delay use-radius=no
      lease-script=:foreach i in=[/ip dhcp-server lease find where server="USER_LAN"] do={ \\r\n:local ip [/ip dhcp-server lease get value-name=address $i]; \\r\n:local hostname [/ip dhcp-server lease get value-name=comment $i];
             \\r\n:system ssh user=konrad address=10.0.1.1 \\r\ncommand="check=\$(sudo pdnsutil list-zone hw.sonic 2> /dev/null | grep ^$hostname | awk '{print \$5}'); \r\nif [[ -n \$check ]]; then \\r\nif [ \$check != $ip ]; then echo
             \"Replacing with new\" $ip \"IP\"; \\r\nsudo pdnsutil replace-rrset hw.sonic $hostname A 60 $ip > /dev/null 2>&1; \\r\nelse echo $hostname \"already exists\"; fi; \\r\nelse echo \"Adding\" $hostname $ip; \\r\nsudo pdnsutil
             add-record hw.sonic $hostname A $ip > /dev/null 2>&1; fi"}

from what I see in the manual

Script that will be executed after lease is assigned or de-assigned. Internal “global” variables that can be used in the script:
leaseBound - set to “1” if bound, otherwise set to “0”
leaseServerName - dhcp server name
leaseActMAC - active mac address
leaseActIP - active IP address
lease-hostname - client hostname
lease-options - array of received options

I can modify my script to perform only for that Bound which connects yes ? Rather than make foreach loop on all entries. But first why the script isn’t triggered