Community discussions

MikroTik App
 
agnostic
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 61
Joined: Fri Mar 21, 2014 8:23 pm

lease-hostname lease script variable not working

Sat Aug 10, 2019 1:43 am

hello i put the following script to dhcp lease scripts according to this manual https://wiki.mikrotik.com/wiki/Manual:IP/DHCP_Server

/tool fetch url=("http://10.10.10.1/add.php?clienthostnam ... easeActMAC")

which is working perfect from server side BUT this weird happens:
at log output window i get mac correctly and as clienthostname i get -hostname which of course is parsed to database as client name and is wrong.
i understand that i must parse variables another way so i try to parse it as follows:

/tool fetch url=("http://10.10.10.1/add.php?clienthostname=" . $lease-hostname . "&mac=" . $leaseActMAC) and it gives me an empty client name but mac address is ok which of course again it parses only mac address on database and leaves hostname empty.
even if i change the order of mac and client name the client name remains empty.

would it be me and the way i use to handle this specific variable or it does not want to give host name?
 
User avatar
sebastia
Forum Guru
Forum Guru
Posts: 1857
Joined: Tue Oct 12, 2010 3:23 am
Location: Antwerp, BE

Re: lease-hostname lease script variable not working

Sat Aug 10, 2019 4:43 am

hey, try $"lease-hostname" instead
 
achmad
just joined
Posts: 2
Joined: Sun Apr 02, 2023 11:54 pm

Re: lease-hostname lease script variable not working

Sun Apr 02, 2023 11:56 pm

try this $[:put [/ip/dhcp-server/lease/get value-name=host-name [find where mac-address=$leaseActMAC]]]
 
achmad
just joined
Posts: 2
Joined: Sun Apr 02, 2023 11:54 pm

Re: lease-hostname lease script variable not working

Sun Apr 02, 2023 11:57 pm

$[:put [/ip/dhcp-server/lease/get value-name=host-name [find where mac-address=$leaseActMAC]]]
 
makrob
just joined
Posts: 4
Joined: Sun Dec 10, 2023 12:44 pm

Re: lease-hostname lease script variable not working

Sun Jun 02, 2024 8:58 pm

hey, try $"lease-hostname" instead
Thanks, this $"lease-hostname" worked for me.
 
guywifi
newbie
Posts: 25
Joined: Wed Nov 21, 2012 5:40 pm

Re: lease-hostname lease script variable not working

Tue Jun 10, 2025 9:50 pm

Hello,

I have this script setup to email us when something gets an IP address from the router. I'd like to add the DHCP pool's total size and how many IP's are used, but I am having a hard time finding what those variables would be. I am not really finding anything pertaining to IP Pools. Does anyone have those handy?

:local recipient "youremail@email.com"
/ip dhcp-server lease
:if ($leaseBound = 1) do={
:do {
:tool e-mail send to=$recipient subject="5th and Main New Device Alert:  $"lease-hostname"" body="The following MAC address $leaseActMAC received an IP address $leaseActIP from the $leaseServerName"
:log info "Sent DHCP alert for MAC $leaseActMAC"
} on-error={:log error "Failed to send alert email to $recipient"}}
 
lurker888
Member
Member
Posts: 425
Joined: Thu Mar 02, 2023 12:33 am

Re: lease-hostname lease script variable not working

Tue Jun 10, 2025 10:21 pm

If you want other statistics, just get them using regular Mikrotik commands. For example I have a pool named "local-pool" and to get information about it I would use:
:local usedAddrs [ /ip/pool/get local-pool used ]
:local totalAddrs [ /ip/pool/get local-pool total ]
:put "Of $totalAddrs addresses, $usedAddrs are used."
This results in the output:
Of 154 addresses, 6 are used.
 
guywifi
newbie
Posts: 25
Joined: Wed Nov 21, 2012 5:40 pm

Re: lease-hostname lease script variable not working

Tue Jun 10, 2025 10:52 pm

If you want other statistics, just get them using regular Mikrotik commands. For example I have a pool named "local-pool" and to get information about it I would use:
:local usedAddrs [ /ip/pool/get local-pool used ]
:local totalAddrs [ /ip/pool/get local-pool total ]
:put "Of $totalAddrs addresses, $usedAddrs are used."
This results in the output:
Of 154 addresses, 6 are used.
Thanks for the reply! That is not working for me. But maybe I am putting it in my script in the wrong location? Where should this go.
 
lurker888
Member
Member
Posts: 425
Joined: Thu Mar 02, 2023 12:33 am

Re: lease-hostname lease script variable not working

Tue Jun 10, 2025 11:11 pm

Should basically work anywhere. Other than that, show the script and maybe I can spot the error. Of course you have to replace the pool name with yours, and ":put" is the equivalent of "print" in most languages, so prints output to the console; to log the stuff use ":log info" as in the other places in your script.
 
guywifi
newbie
Posts: 25
Joined: Wed Nov 21, 2012 5:40 pm

Re: lease-hostname lease script variable not working

Tue Jun 10, 2025 11:15 pm

Should basically work anywhere. Other than that, show the script and maybe I can spot the error. Of course you have to replace the pool name with yours, and ":put" is the equivalent of "print" in most languages, so prints output to the console; to log the stuff use ":log info" as in the other places in your script.
Sure, here is what I've got. This was my latest attempt, nothing failed but nothing in the email either. I did try putting this into the body and that did fail.

Thanks again for your help, I appreciate it.
:local recipient "email@email.com"
/ip dhcp-server lease
:if ($leaseBound = 1) do={
:do {
:tool e-mail send to=$recipient subject="5th and Main New Device Alert:  $"lease-hostname"" body="The following MAC address $leaseActMAC received an IP address $leaseActIP from the $leaseServerName"
:local usedAddrs [ /ip/pool/get "Public DHCP Pool" used ]
:local totalAddrs [ /ip/pool/get "Public DHCP Pool" total ]
:put "Of $totalAddrs addresses, $usedAddrs are used."
:log info "Sent DHCP alert for MAC $leaseActMAC"
} on-error={:log error "Failed to send alert email to $recipient"}}
 
lurker888
Member
Member
Posts: 425
Joined: Thu Mar 02, 2023 12:33 am

Re: lease-hostname lease script variable not working

Tue Jun 10, 2025 11:50 pm

Well...
* you're still using ":put" - again, that writes to the console
* if you want this in the e-mail, then you'll have to put it in the e-mail body - if by any chance you have not placed the ":local" declaration *before* sending the e-mail, well, of course that will fail: the variables don't exist yet. Place the declarations *before* their usage do before the line where you send the e-mail
* it's often wise to debug things in steps - before sending it in an e-mail I would simply log it by replacing ":put" with ":log info" to ensure that getting the pool data actually works and *then* put it into the e-mail
* are you receiving the e-mail at all? (with or without the additional content)