Page 1 of 1

Call a webhook on dhcp allocation

Posted: Mon May 27, 2019 12:54 pm
by andret
Hi

Does anyone have any ideas on how to implement a web call at the time a DHCP IP address is allocated?

For example, I might have a device configured on a static IP reservation so when it connects to the network it will always get this specific IP address.

I want to know when the device connects/disconnects by having a script or feature request a web URL, e.g. http://192.168.1.1/dhcp.php?ip=192.168. ... on=connect

For now the only way I can think is to use a script that runs periodically and checks of the device is active and calls a get request to the URL but it would be nicer for automation (IoT) purposes if this could be actioned in a cleaner way using a webhook or similar method that could easily be integrated into Home Assistant / Node-Red.

Thanks

Re: Call a webhook on dhcp allocation

Posted: Mon May 27, 2019 1:38 pm
by sid5632
Doesn't seem too difficult. All the information about DHCP lease-scripting is here: https://wiki.mikrotik.com/wiki/Manual:IP/DHCP_Server
You just need to write/test it using a "tool fetch" command.

Re: Call a webhook on dhcp allocation

Posted: Mon May 27, 2019 1:46 pm
by andret
Doesn't seem too difficult. All the information about DHCP lease-scripting is here: https://wiki.mikrotik.com/wiki/Manual:IP/DHCP_Server
You just need to write/test it using a "tool fetch" command.
Thanks... I somehow read right over the 'lease-script' section... can't believe it :)

Re: Call a webhook on dhcp allocation

Posted: Mon May 27, 2019 2:06 pm
by andret
So this was quite simple...

On mikrotik created new script named 'dhcp_lease_script' with the following content
:global leaseBound
:global leaseServerName
:global leaseActMAC
:global leaseActIP

/tool fetch url="http://192.168.42.1/dhcp.php?bound=$leaseBound&mac=$leaseActMAC&ip=$leaseActIP"
Then configured the dhcp server lease-script using:
/ip dhcp-server set dhcp1 lease-script=dhcp_lease_script
Can see the URL requests on the web server side so its all good.

Thanks again

Re: Call a webhook on dhcp allocation

Posted: Mon May 27, 2019 9:10 pm
by andret
It seems to be working intermittently - I guess there are some conditions which need to be met when a lease is renewed?