Page 1 of 1

Two questions about DHCP leases script.

Posted: Mon Apr 22, 2019 8:50 pm
by gutekpl
I have a simple script which informs me via Telegram whenever I have new device connected via guest wifi.

global telegramMessage "$leaseActMAC connected as guest and received $leaseActIP address."
/system script run SendToTelegram

My guest DHCP lease is set to 6h and I get 2 messages per device connected - one when it gets new DHCP lease and second after 6 hours, when lease is purged. Can I somehow filter that to get only info about new leases and skip purging existing ones?

Second question is MAC to device name translation. Let's assume that I am assigning static leases to my guests - Is it possible to somehow replace MAC adress with device name which I type in static lease and display it in that message?

Re: Two questions about DHCP leases script.

Posted: Fri Apr 26, 2019 3:00 am
by NetWorker
You didn't mention how you're triggering the lease script so I can't answer your first question. But in case you're triggering from the dhcp-server, you could just check if said mac address is still registered and if it isn't not to run the telegram script. That way it will only notify of the new lease.

Second question:
:global hostname
:local newleaseip x.x.x.x
# where x.x.x.x is the ip of the guest that has just been assigned a lease
# this would also work with his/her mac

:set newleaseip [/ip dhcp-server lease get [find where address=$newleaseip] host]
Hostname will be under the global variable "hostname" for you to use in any other script you like.

Also, post your script if you want a more precise answer.

Re: Two questions about DHCP leases script.

Posted: Fri Apr 26, 2019 10:17 am
by gutekpl
Here is complete script, but it is only responsible for sending it via telegram, so I did not posted it before as I think it doesn't matter.
:global telegramMessage
:local botid
:local chatid

set botid "xyz"
set chatid "123" 

if ($telegramMessage != "") do={
   /tool fetch url="https://api.telegram.org/bot$botid/sendMessage\?chat_id=$chatid&text=$telegramMessage" keep-result=no
   set telegramMessage ""
}
And yes, I trigger it from DHCP server, I pasted it directly into dhcp lease script:

global telegramMessage "$leaseActMAC connected as guest and received $leaseActIP address."
/system script run SendToTelegram

Re: Two questions about DHCP leases script.

Posted: Tue May 14, 2019 5:15 pm
by NetWorker
Alright, sorry for not getting back to you sooner.

First of all, you haven't shown me how you parse the MAC which is what interests me from the other script. I apologize for not making that clear.

In any case, I always recommend (and to the best of my knowledge so does Mikrotik) keeping all scripts under /system scripts and call them up there. This eases troubleshooting when something ain't working. Also, sometimes strange things happen with permissions if you run certain scripts from certain parts of routerOS.

So in your case I would simply place
:execute "myscriptname";
in the dhcp-lease script window.

Further, use a short delay in the script and check if the MAC you're parsing on to the telegram script exists:
:global leasemac;
:delay 2;

:if ([:len [/ip dhcp-server lease find where mac-address=$leasemac]!=0) do={
	:execute "sendtotelegram";} 

Re: Two questions about DHCP leases script.

Posted: Tue May 14, 2019 7:28 pm
by gutekpl
The thing is I am not parsing that mac anyhow, Its builtiin feature which I use. If I was parsing it already then probably my knowledge would be enough to modify it by myself :)

Re: Two questions about DHCP leases script.

Posted: Tue May 14, 2019 7:59 pm
by NetWorker
Lol, right. But what about this:
global telegramMessage "$leaseActMAC connected as guest and received $leaseActIP address."
How are you setting those variables?

Re: Two questions about DHCP leases script.

Posted: Tue May 14, 2019 10:04 pm
by gutekpl
They are being set automaticaly during dhcp registration process

lease-script (string; Default: "") 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

https://wiki.mikrotik.com/wiki/Manual:IP/DHCP_Server

Re: Two questions about DHCP leases script.  [SOLVED]

Posted: Tue May 14, 2019 11:26 pm
by NetWorker
Wow, that's news for me. I hadn't read that bit. Cool stuff.

Right, so for question number two, did you try using the hostname variable? I know not all devices report host name correctly. Other than that, if you are indeed using static leases you could try to comment each lease and using the MAC, read out the comment put it in a variable and send that.

Back to question one, did you try using the leaseBound variable with an if to check? I'm thinking that when purging a lease that variable might already be set to 0?

Re: Two questions about DHCP leases script.

Posted: Wed May 15, 2019 1:06 pm
by gutekpl
Hah, I read that manual which I pasted above searching for mac address variable, but turns out that other variables I needed now are also there!
if ($leaseBound = "1") do={
global telegramMessage "$"lease-hostname" ($leaseActMAC) got address $leaseActIP from DHCP_guest"
/system script run SendToTelegram
}
So now I get only 1 message during creating lease and I also have client name there. Great brainstorm, thanks.

If You can also take a look here: viewtopic.php?f=9&t=148459
Idea with starting scheduler from script sounds nice, but I don't know how to manage that.

Re: Two questions about DHCP leases script.

Posted: Wed May 15, 2019 5:27 pm
by NetWorker
Glad you got it working!