ARP Table To Dynamic Simple Queue - MikroTik Script RouterOS

Dynamic Simple Queue Script (ARP Table)

Ideas
• Using simple queues to limit any traffic (or could be a packet mark) from specific target IP
• Only make simple queues rule for any active IP on the network (taken from ARP table)
• Utilize MikroTik’sadvantage to runs script and task scheduling to do the job.

How to use
• Create your mangle
• Customize based on your specific need/network configuration•Drag-drop-import

How It Works
1. The script will make a parent queue(i)and a catch-all rule(ii)for our workaround with all parameter which we’ve already set.
2. Takes all IP addresses and MAC address with specified filter (interface) from /iparptable.
3. Removes all dynamic rules previously created, if available. Then creates simple queue rules using parameters we’ve already set.
4. Delay for specified time (default is 60 secs) and re-run the steps from number 2.5.The scheduler at /system scheduler checks and ensures that the script is running in the background.

# System Script
/system script
job remove [find script="limitDynamic"];
remove [find name="limitDynamic"];
add name="limitDynamic" source={
:local filterARP "br-local";
:local targetAddress "192.168.10.0/24";
:local limitClient "3M";
:local limitParent "30M";
:local parentLimit "0-Parent";
:local packetMark "client.p";
:local delay "60s";
:local enabled true;
:local enableLog true;
/queue simple
:if ($enabled) do={
:if ([find name=($parentLimit)] = "") do={	add name=($parentLimit) packet-mark=$packetMark target=$targetAddress max-limit=($limitParent."/".$limitParent);}
:if ([find name=($parentLimit."-all")] = "") do={ add name=($parentLimit."-all") parent=$parentLimit packet-mark=$packetMark target=$targetAddress max-limit=($limitParent."/".$limitParent); }
}
:while (true) do={
:local arp [:toarray [/ip arp print as-value where dynamic && interface=$filterARP ]];
:local queue [:toarray [/queue simple print as-value]];
:if ($enableLog) do={ :log warning message= "Removing any dynamic queue entry ...";}
:if ($enabled) do={ /queue simple remove [find (parent=$parentLimit) && (name!=($parentLimit."-all"))];}
:if ($enableLog) do={ :log warning message= "All dynamic queue removed."; :log warning message= "Adding new dynamic queue entry ...";}
:foreach a in=$arp do={
:local ip ($a->"address");
:local zz ($a->"mac-address");
:if ($enabled) do={/queue simple add name=($zz) target=$ip max-limit=($limitClient."/".$limitClient) parent=$parentLimit packet-mark=$packetMark place-before=($parentLimit."-all");}
}
:if ($enableLog) do={ :log warning message= "Finished adding"; :log warning message= "Running delay";}
:delay $delay;
}
}
:execute "/system script run limitDynamic;"

# system scheduler
/system scheduler
remove [find name="limitDynamic"];
add name=limitDynamic interval=10m on-event={ :if ([:len [/system script job find script=limitDynamic]] = 0 ) do={/system script run limitDynamic;}}
/file remove [find name="limit-dynamic-mum.rsc"];
Credit: https://github.com/ericksetiawan/dynamic-simple-queue
Complete PDF: https://mum.mikrotik.com//presentations/ID16/presentation_3482_1476686228.pdf