Community discussions

MikroTik App
 
gutekpl
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 92
Joined: Wed Feb 20, 2019 6:31 pm

Two questions about DHCP leases script.

Mon Apr 22, 2019 8:50 pm

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?
 
NetWorker
Frequent Visitor
Frequent Visitor
Posts: 98
Joined: Sun Jan 31, 2010 6:55 pm

Re: Two questions about DHCP leases script.

Fri Apr 26, 2019 3:00 am

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.
 
gutekpl
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 92
Joined: Wed Feb 20, 2019 6:31 pm

Re: Two questions about DHCP leases script.

Fri Apr 26, 2019 10:17 am

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
 
NetWorker
Frequent Visitor
Frequent Visitor
Posts: 98
Joined: Sun Jan 31, 2010 6:55 pm

Re: Two questions about DHCP leases script.

Tue May 14, 2019 5:15 pm

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";} 
 
gutekpl
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 92
Joined: Wed Feb 20, 2019 6:31 pm

Re: Two questions about DHCP leases script.

Tue May 14, 2019 7:28 pm

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 :)
 
NetWorker
Frequent Visitor
Frequent Visitor
Posts: 98
Joined: Sun Jan 31, 2010 6:55 pm

Re: Two questions about DHCP leases script.

Tue May 14, 2019 7:59 pm

Lol, right. But what about this:
global telegramMessage "$leaseActMAC connected as guest and received $leaseActIP address."
How are you setting those variables?
 
gutekpl
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 92
Joined: Wed Feb 20, 2019 6:31 pm

Re: Two questions about DHCP leases script.

Tue May 14, 2019 10:04 pm

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
 
NetWorker
Frequent Visitor
Frequent Visitor
Posts: 98
Joined: Sun Jan 31, 2010 6:55 pm

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

Tue May 14, 2019 11:26 pm

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?
 
gutekpl
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 92
Joined: Wed Feb 20, 2019 6:31 pm

Re: Two questions about DHCP leases script.

Wed May 15, 2019 1:06 pm

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.
 
NetWorker
Frequent Visitor
Frequent Visitor
Posts: 98
Joined: Sun Jan 31, 2010 6:55 pm

Re: Two questions about DHCP leases script.

Wed May 15, 2019 5:27 pm

Glad you got it working!
 
elico
Member Candidate
Member Candidate
Posts: 143
Joined: Mon Nov 07, 2016 3:23 am

Re: Two questions about DHCP leases script.

Sun Jan 24, 2021 10:26 am

based on: viewtopic.php?f=9&t=171750&p=840220#p840220

I have just tested that it's possible to do the same as on-up with lease-script.
You don't need to set a global variable and just need to use the dont-require-permissions and use a script for the lease-script.
 /system script export
add dont-require-permissions=yes name=dhcpLeaseBound owner=admin policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source=\
    "if (\$leaseBound = \"1\") do={\r\
    \n    :log info \"\$\"lease-hostname\" (\$leaseActMAC) got address \$leaseActIP from \$\"leaseServerName\"\"\r\
    \n}"
You can set the script with "dont-require-permissions" to yes.
/system script set dhcpLeaseBound dont-require-permissions=yes
Then set the script for the dhcp lease with:
/ip dhcp-server set server-xyz lease-script=dhcpLeaseBound

Who is online

Users browsing this forum: alexantao, hmdslm and 27 guests