Community discussions

MikroTik App
 
andret
newbie
Topic Author
Posts: 37
Joined: Fri Apr 17, 2009 11:08 am

Call a webhook on dhcp allocation

Mon May 27, 2019 12:54 pm

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
 
sid5632
Long time Member
Long time Member
Posts: 561
Joined: Fri Feb 17, 2017 6:05 pm

Re: Call a webhook on dhcp allocation

Mon May 27, 2019 1:38 pm

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.
 
andret
newbie
Topic Author
Posts: 37
Joined: Fri Apr 17, 2009 11:08 am

Re: Call a webhook on dhcp allocation

Mon May 27, 2019 1:46 pm

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 :)
 
andret
newbie
Topic Author
Posts: 37
Joined: Fri Apr 17, 2009 11:08 am

Re: Call a webhook on dhcp allocation

Mon May 27, 2019 2:06 pm

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
 
andret
newbie
Topic Author
Posts: 37
Joined: Fri Apr 17, 2009 11:08 am

Re: Call a webhook on dhcp allocation

Mon May 27, 2019 9:10 pm

It seems to be working intermittently - I guess there are some conditions which need to be met when a lease is renewed?