Community discussions

 
cicserver
Member
Member
Topic Author
Posts: 300
Joined: Sun Jul 24, 2011 12:04 pm

Gett MAC addresses of MOBILE Devices from DHCP Lease

Thu Jul 30, 2015 8:25 pm

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 :) )
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Gett MAC addresses of MOBILE Devices from DHCP Lease

Thu Jul 30, 2015 8:34 pm

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"));
 };
Last edited by boen_robot on Fri Jul 31, 2015 2:50 pm, edited 1 time in total.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
cicserver
Member
Member
Topic Author
Posts: 300
Joined: Sun Jul 24, 2011 12:04 pm

Re: Gett MAC addresses of MOBILE Devices from DHCP Lease

Thu Jul 30, 2015 8:45 pm

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 !
 
User avatar
bholler
Trainer
Trainer
Posts: 82
Joined: Wed Feb 09, 2005 10:22 pm
Location: Nigeria
Contact:

Re: Gett MAC addresses of MOBILE Devices from DHCP Lease

Fri Jul 31, 2015 2:23 pm

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
};
Mikrotik Certified Trainer Partner, MTCNA, MTCTCE, MTCWE, MTCRE. YIM: oseniabiola Skye: habholler1, Tel.+2348060319130, +2348182556717, Email: abiola@trisatcom.net
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Gett MAC addresses of MOBILE Devices from DHCP Lease

Fri Jul 31, 2015 2:47 pm

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
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)
 
User avatar
bholler
Trainer
Trainer
Posts: 82
Joined: Wed Feb 09, 2005 10:22 pm
Location: Nigeria
Contact:

Re: Gett MAC addresses of MOBILE Devices from DHCP Lease

Fri Jul 31, 2015 3:29 pm

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
Mikrotik Certified Trainer Partner, MTCNA, MTCTCE, MTCWE, MTCRE. YIM: oseniabiola Skye: habholler1, Tel.+2348060319130, +2348182556717, Email: abiola@trisatcom.net
 
oxigeno20
Frequent Visitor
Frequent Visitor
Posts: 87
Joined: Tue May 23, 2006 5:29 pm
Location: Argentina

Re: Gett MAC addresses of MOBILE Devices from DHCP Lease

Fri Aug 07, 2015 4:34 pm

 
User avatar
bholler
Trainer
Trainer
Posts: 82
Joined: Wed Feb 09, 2005 10:22 pm
Location: Nigeria
Contact:

Re: Gett MAC addresses of MOBILE Devices from DHCP Lease

Thu Sep 06, 2018 1:13 pm

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
Mikrotik Certified Trainer Partner, MTCNA, MTCTCE, MTCWE, MTCRE. YIM: oseniabiola Skye: habholler1, Tel.+2348060319130, +2348182556717, Email: abiola@trisatcom.net
 
User avatar
ADahi
Member Candidate
Member Candidate
Posts: 217
Joined: Thu Sep 21, 2017 7:16 pm
Location: Iraq, Ninavah
Contact:

Re: Gett MAC addresses of MOBILE Devices from DHCP Lease

Thu Sep 06, 2018 6:01 pm

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:

Who is online

Users browsing this forum: No registered users and 17 guests