Community discussions

MikroTik App
 
Jeanluck
Member Candidate
Member Candidate
Topic Author
Posts: 280
Joined: Tue Apr 19, 2011 7:07 pm

Very long time to execute simple script, why??

Sat Apr 15, 2017 6:38 pm

Hi,
I write a script for know how many ips has a single MAC. But every iteration take 3-4 seconds in my core.
Why?? I have 3000 macs, and the script take 2 hours


:log info "Start"
#1
:foreach i in=[/ip arp find disabled=no] do={
:local MacNull "00:00:00:00:00:00"
:local Mac "00:00:00:00:00:00"
:local LenMac 0
:local NumIPs 0
:local IP "0.0.0.0"

set Mac [/ip arp get $i mac-address]
set LenMac [:len [$Mac]]

#2
if ( $LenMac > 0 and $Mac != $MacNull) do={
:set NumIPs [/ip arp print count-only where mac-address=$Mac]
#3
if ( $NumIPs > 1) do={
:log warning "MAC:$Mac has $NumIPs IPs"
#3
}
#2
}
#1
}
:log info "Endt"
 
User avatar
docmarius
Forum Guru
Forum Guru
Posts: 1222
Joined: Sat Nov 06, 2010 12:04 pm
Location: Timisoara, Romania
Contact:

Re: Very long time to execute simple script, why??

Sun Apr 16, 2017 2:13 am

[/ip arp print count-only where mac-address=$Mac] actually runs through your complete MAC list.
So each iteration actually has 3000 reads through your router's command interpretor.
If we assume 1 msec each this will give you 3 seconds/iteration.
For a total of 3000 iterations, you will get 2.5 hours.

You can speed this up by loading your MAC list into memory and process them there.
:local arptable "";
:foreach i in=[/ip arp find disabled=no] do={
  :local macaddr [/ip arp get $i mac-address];
  :set $arptable ($arptable, $macaddr);
}
Now process the table as you like:
:for i from 1 to ([:len $arptable] - 1) do={
  :local macaddr [:pick $arptable $i];
  :local ent [:find $arptable $macaddr -1];
  :if ($ent != $i) do={
    :log info "MAC $macaddr found with multiple IPs"
  }
}
 
Jeanluck
Member Candidate
Member Candidate
Topic Author
Posts: 280
Joined: Tue Apr 19, 2011 7:07 pm

[Solved] Re: Very long time to execute simple script, why??

Sun Apr 16, 2017 5:47 pm

Great!!!! It works perfectly, and only take 30 seconds VS 2 hours!!
Thank you!!

Who is online

Users browsing this forum: No registered users and 5 guests