Hi, new to Mikrotik, browsed through forum last couple of weeks, learned a lot about scripting (and still learning).
One of complaints from different users is Mikrotik lacks list of number of connections per each IP.
“/ip firewall connection print count-only” will show number of connections for all clients connected but nothing per client or IP.
Since all IPs on my network behind Mikrotik are assigned by DHCP (192.168.50.0/24), I’ve used “/ip dhcp-serve lease”
to obtain all IPs connected
output is as follows:
297
192.168.50.252 Iphone-5 connections:297
28
192.168.50.253 HPNOTE125 connections:28
any ideas how to get only (as below), without additional lines for connection numbers
192.168.50.252 Iphone-5 connections:297
192.168.50.253 HPNOTE125 connections:28
Yes. And I did misspeak. It actually does store the result as a variable. What is happening, and that I don’t know how to turn off, is to prevent it from also displaying the value to the console. Do you simply want the data, maybe in a file? That would possible, because all the data is in variables.
Okay, this is not perfect, but does display the data the way you want. Note that variables are limited to 4096 bytes. So, you may have to loop this across a couple of different variables.
{
# Declare main variable to hold all the data we care about
:local Data;
/ip dhcp-server lease
:foreach i in=[find] do={
# declare and set values
:local adr "$[get value-name=address $i]";
:local host "$[get value-name=host-name $i]";
:local con [/ip firewall connection print count-only where src-address~"$adr"];
# Store values in variable
:set Data ($Data . "$adr $host connections: $con\r\n");
}
# script is finished so add a spacer from all the noise print count-only makes
:put ("\r\n");
# print the data to the screen
:put ($Data);
}
Thanks, I’d like to avoid writing to a file. Still a new user and red a lot on how not to use local storage as much as possible, although I’m creating a backup of router each day in a file, anyway…
since [/ip firewall connection print count-only where src-address~“$adr”] prints number of connections I’m using it in second line, so output is like this, until I don’t figure out how to get rid of doubles
Using src-address~“$adr” is not exact enough because it will also count IP addresses in one go like 192.168.88.1 192.168.88.11-19 and 192.168.88.100-199
To avoid this you need to add a ‘separator’ the first character after the last digit of IP address and that is here the “:” in front of the port-number and then you will have accurate results.
The “.” concatenates the “:” with the src-address and if for example you are looking for 192.168.88.1 it will make it 192.168.88.1:
The line to change in the script of creatin:
:local con [/ip firewall connection print count-only where src-address~"$adr".":"];