DHCP server dynamic IP notification

Hi,

I have a small script (can’t recall from where I got it to be honest), but seems that after one of latest upgrades it stopped working:

/ip dhcp-server lease
:if ($leaseBound = 1 && [ get [ find where mac-address=$leaseActMAC ] dynamic ] = true) do={
	:do {
		:tool fetch url="https://api.telegram.org/botXXXXX/sendMessage\?chat_id=XXXX&text=The following MAC address [$leaseActMAC] received an IP address [$leaseActIP] from the DHCP Server [$leaseServerName]" keep-result=no
	:log info "Sent DHCP alert for MAC $leaseActMAC"
	} on-error={:log error "Failed to send alert via Telegram"}
}

can you please help me fix it ? I know that issue is with “if” loop, but I can’t figure out what exactly.

You do not read changelogs between versions?

DHCP scripts do not have anymore some right (also other items, eg. netwatch) that are needed for fetch.
See the other forum posts about that.

it’s not issue with fetch or it’s permission.

if I remove "&& [ get [ find where mac-address=$leaseActMAC ] dynamic ] = true) " then whole scripts works, but sends notification for all leases, not just dynamic.

You do not specify RouterOS version used, but on the last v7 versions is impossible to do some things directly inside dhcp script.


If a entry already exist for same MAC:
invalid internal item number

if the MAC already do not exist:
no such item

so, before do anything you must check if entry already do not exist on another dhcp-server, or the entry must already exist…

So the line must be like (untested)
:if (($leaseBound=1) and [get [find where (mac-address=$leaseActMAC) and (server=$leaseServerName)] dynamic]) do={

and that basically did the trick, thanks !