Community discussions

MikroTik App
 
p3ter
just joined
Topic Author
Posts: 19
Joined: Fri Jul 16, 2021 3:17 pm

If "mac1" then "text1" type array.

Tue Jun 21, 2022 11:56 am

Noob question... I have a working DHCP script which sends a pushbullet notification when certain known devices join the network:
# Variables
:global Person1 "00:00:00:00:00:00";
:global Person2 "11:11:11:11:11:11";
:global apikey "s78e4thyfakekeyf89se47gh56ts87hft87"
:global leaseBound
:global leaseActMAC

# DHCP Bound
:if ($leaseBound =1) do={
 :if ([$leaseActMAC] = $Person1)  do={
  :tool fetch mode=https url="https://api.pushbullet.com/v2/pushes" http-method=post http-data="type=note&body=Person1 Arrived Home" user="$apikey"
    }
 :if ([$leaseActMAC] = $Person2)  do={
  :tool fetch mode=https url="https://api.pushbullet.com/v2/pushes" http-method=post http-data="type=note&body=Person2 Arrived Home" user="$apikey"
    }
}
I would like to optimize the above code so that instead of repeating if's/variables/static text once per device, I would like to have a single array like "mac1, name1, mac2, name2..." etc so that I can easily add a longer list of devices, and notify with a single code block, but I have struggled to find an example of how to do this. i.e. the principle
if ([$leaseActMAC] = {any mac[n] from array})
body={corresponding name[n] from array} Arrived Home"

Can anyone point at an example I can work from?

Thanks!
 
pe1chl
Forum Guru
Forum Guru
Posts: 10186
Joined: Mon Jun 08, 2015 12:09 pm

Re: If "mac1" then "text1" type array.

Tue Jun 21, 2022 12:14 pm

In RouterOS you can index arrays by a text value so you can simply build an array containing names that is indexed by their MAC.
(no need to have two arrays with a common integer index n)

The only thing that is often tricky in RouterOS is to initially (from boot) define the array in such a way that it starts empty but still is "defined" the first time it is used.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: If "mac1" then "text1" type array.

Tue Jun 21, 2022 4:35 pm

Instead of define array(s), make static leases on ip / dhcp-server / leases and add on the comment the name,
then search the MAC on fixed lease and read the name (the comment)...
 
p3ter
just joined
Topic Author
Posts: 19
Joined: Fri Jul 16, 2021 3:17 pm

Re: If "mac1" then "text1" type array.

Tue Jun 21, 2022 5:30 pm

Thanks both. @rextended I did not see a global variable for the lease comment?
I only see these:
https://help.mikrotik.com/docs/display/ROS/DHCP
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
  • lease-options - array of received options
 
p3ter
just joined
Topic Author
Posts: 19
Joined: Fri Jul 16, 2021 3:17 pm

Re: If "mac1" then "text1" type array.

Tue Jun 21, 2022 5:44 pm

Just found this post viewtopic.php?t=183632 which demonstrates 'get value-name=comment', I will take a look.

Who is online

Users browsing this forum: aoravent, Ellaham and 24 guests