Page 1 of 1

Gett MAC addresses of MOBILE Devices from DHCP Lease

Posted: Thu Jul 30, 2015 8:25 pm
by cicserver
Is there any possibility that I can get MAC Addresses of Mobile devices like ANDROID / IPHONE from the Mikrotik DHCP Leases ?

I want to get there HOSTNAME name and MAC Address (or mac addresses in ADDRESS LIST or file or whatever possible :) )

Re: Gett MAC addresses of MOBILE Devices from DHCP Lease

Posted: Thu Jul 30, 2015 8:34 pm
by boen_robot
You can get a list of leases with
[/ip dhcp-server lease print detail as-value]
which you can then store in a variable and/or loop over to get the specifics.

Filtering the list so that it contains only android devices is the trickier part... You can reasonably guess that the hostname will start with "android-", as this is what Android uses by default. A user could mask it with an app, but most users wouldn't bother changing whatever their default is (and that is if they know an app could change the name to begin with...), so you're 99.999% covered if you do
[/ip dhcp-server lease print detail as-value where host-name~"android\\-"]

So f.e. to write all android devices' MACs and hostnames to the router log:
:foreach dev in=[/ip dhcp-server lease print detail as-value where host-name~"android\\-"] do={
     /log info ("Device with MAC address" . ($dev->"active-mac-address") . " has the hostname " . ($dev->"host-name"));
 };

Re: Gett MAC addresses of MOBILE Devices from DHCP Lease

Posted: Thu Jul 30, 2015 8:45 pm
by cicserver
To write all android devices' MACs and hostnames to the router log:
:foreach dev in=[/ip dhcp-server lease print detail as-value where host-name~"android\\-"] do={
     /log info ("Device with MAC address" . ($dev->"active-mac-address") . " has the hostname " . ($dev->"host-name"));
 };
Thank you it worked !

Re: Gett MAC addresses of MOBILE Devices from DHCP Lease

Posted: Fri Jul 31, 2015 2:23 pm
by bholler
Hi Boen Robot,

Can you please help with a modification for active-address. and active-address to be populated in an address-list called mobile. i tried this but it did not work.

:foreach dev in=[/ip dhcp-server lease print detail as-value where host-name~"android\\-"] do={

/ip firewall address-list add [$dev->"active-address"] list=mobile-devices
};

Re: Gett MAC addresses of MOBILE Devices from DHCP Lease

Posted: Fri Jul 31, 2015 2:47 pm
by boen_robot
Use "(" and ")", not "[", and "]". Accessing the value in an associative array is considered part of an expression, not a command. Plus, AFAIK, "address" is a required argument in address-list's add.

So:
/ip firewall address-list add address=($dev->"active-address") list=mobile-devices

Re: Gett MAC addresses of MOBILE Devices from DHCP Lease

Posted: Fri Jul 31, 2015 3:29 pm
by bholler
Hi,
It works. Many thanks.

But the list could not update the address-list whenever scheduler runs the script. In the log, it reads "failure: already have such entry". Any modification to make update take effect.

Regards

Re: Gett MAC addresses of MOBILE Devices from DHCP Lease

Posted: Fri Aug 07, 2015 4:34 pm
by oxigeno20

Re: Gett MAC addresses of MOBILE Devices from DHCP Lease

Posted: Thu Sep 06, 2018 1:13 pm
by bholler
HI Oxygeno20,

Can you help me out on this script? It is not running on v6.42.7

:local SERVER “server250”;
:local INTERFACEFILTER “combo1-core-switch”;
 
# # AUTOR: NICOLAS DAITSCH
# # www.tech-nico.com
# # Modified by Oseni Abiola
 
:foreach i in=[/ip dhcp-server lease find dynamic=yes active-server=$SERVER]  do={
   :local DhcpDynIP [/ip dhcp-server lease get $i active-address];
   :local DhcpDynMAC [/ip dhcp-server lease get $i mac-address];
   :local DhcpDynHOST [/ip dhcp-server lease get $i host-name];
   :local IfMacExist [/ip firewall filter find src-mac-address="$DhcpDynMAC"];
   :local phoneNAME [:pick $DhcpDynHOST 0 6];
 
    :if ( ($phoneNAME=“Redmi”) || ($phoneNAME=“IPHONE”) || ($phoneNAME=“HUAWEI”) || ($phoneNAME=“iPhone”) || ($phoneNAME=“Galaxy”) || ($phoneNAME="androi") || ($phoneNAME="Window") || ($phoneNAME="Androi")  || ($phoneNAME="BLACKB") || ([:len $DhcpDynHOST]=0) ) do={
          :if ($IfMacExist != "") do={
#               :log error (“filtering Phone… ".$DhcpDynMAC. " It already exists“)
          } else= {
               /ip firewall filter add action=add-src-to-address-list address-list=mobile chain=forward in-interface=$INTERFACEFILTER src-address="$DhcpDynMAC" comment=$DhcpDynHOST;
                 :log warning (“Mobile Phone Captured" . $phoneNAME . " MAC: " .  $DhcpDynMAC);
          }
     }
}


Thank you for your reply.

Regards

Re: Gett MAC addresses of MOBILE Devices from DHCP Lease

Posted: Thu Sep 06, 2018 6:01 pm
by ADahi
HI Oxygeno20,

Can you help me out on this script? It is not running on v6.42.7

:local SERVER “server250”;
:local INTERFACEFILTER “combo1-core-switch”;
 
# # AUTOR: NICOLAS DAITSCH
# # www.tech-nico.com
# # Modified by Oseni Abiola
 
:foreach i in=[/ip dhcp-server lease find dynamic=yes active-server=$SERVER]  do={
   :local DhcpDynIP [/ip dhcp-server lease get $i active-address];
   :local DhcpDynMAC [/ip dhcp-server lease get $i mac-address];
   :local DhcpDynHOST [/ip dhcp-server lease get $i host-name];
   :local IfMacExist [/ip firewall filter find src-mac-address="$DhcpDynMAC"];
   :local phoneNAME [:pick $DhcpDynHOST 0 6];
 
    :if ( ($phoneNAME=“Redmi”) || ($phoneNAME=“IPHONE”) || ($phoneNAME=“HUAWEI”) || ($phoneNAME=“iPhone”) || ($phoneNAME=“Galaxy”) || ($phoneNAME="androi") || ($phoneNAME="Window") || ($phoneNAME="Androi")  || ($phoneNAME="BLACKB") || ([:len $DhcpDynHOST]=0) ) do={
          :if ($IfMacExist != "") do={
#               :log error (“filtering Phone… ".$DhcpDynMAC. " It already exists“)
          } else= {
               /ip firewall filter add action=add-src-to-address-list address-list=mobile chain=forward in-interface=$INTERFACEFILTER src-address="$DhcpDynMAC" comment=$DhcpDynHOST;
                 :log warning (“Mobile Phone Captured" . $phoneNAME . " MAC: " .  $DhcpDynMAC);
          }
     }
}


Thank you for your reply.

Regards

I can help you
look first add this in raw firewall for action=drop
:do {/ip firewall raw
add action=drop chain=prerouting src-address-list="BlockLeaseIPs"
}
or modify it as you need.
second add script to DHCP-server lease-script field
:do {
:local HostList {"androi"; "Androi"; "Galaxy"; "BLACKB"; "IPHONE"; "iPhone"; "HUAWEI"};

:if ($leaseBound=0) do={/ip firewall address-list remove number=[find where address=$leaseActIP && list="BlockLeaseIPs"]}

:if ($leaseBound=1) do={
 :local HostName [:pick [/ip dhcp-server lease get number=[find where address=$leaseActIP] host-name] 0 6];
 :if ( [:len $HostName]=0 ) do={
  :do {/ip firewall address-list add address=$leaseActIP list="BlockLeaseIPs"
  } on-error={:log error message=("add address=$leaseActIP list=BlockLeaseIPs exist.")}
 }
 :foreach ix in=[ $HostList ] do={
  :if ( $HostName=$ix ) do={
  :do {/ip firewall address-list add address=$leaseActIP list="BlockLeaseIPs"
  } on-error={:log error message=("add address=$leaseActIP list=BlockLeaseIPs exist.")}
  }
 }
}} on-error={:log error message="lease-script failed..."}
:wink: