List number of connections per IP

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

{
/ip dhcp-server lease
:foreach i in=[find] do={
:local adr "$[get value-name=address $i]";
:local con [/ip firewall connection print count-only where src-address~"$adr"];
:put "$[get value-name=address $i]  $[get value-name=host-name $i]  connections:$con";
} 
}

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

What you’re looking for is Concatenation Operators.

Not sure I understand, probably doing something wrong, changed the code:

{
/ip dhcp-server lease
:foreach i in=[find] do={
:local adr "$[get value-name=address $i]";
:local con [/ip firewall connection print count-only where src-address~"$adr"];
:put "connections:$con";
} 
}

and now results is, I’m doubling somewhere output of $con but don’t know where:
1116
connections: 1116
12
connections: 12

Can you replace print with someting else because it will print just the number on a single line.

Why not put :put and the [/ip firewall connection print count-only where src-address~“$adr”]; on one line?

:put "connections: " . [/ip firewall connection print count-only where src-address~"$adr"];

Sadly that still put out two lines.

Unfortunately, same output, still not sure why

Hmmm, it looks like the output of print count-only can not be stored in a variable.


{
    /ip dhcp-server lease
    :foreach i in=[find] do={

        # declare and set variables
        :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"];

        # output values
        :put ("$adr $host connections: $con");
    }
}

This lines although it should be a variable, it prints number of connections:

:local con [/ip firewall connection print count-only where src-address~“$adr”];

trying to figure out how to bypass it

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…

I came up with following:

{
/ip dhcp-server lease
:foreach i in=[find] do={
:local adr "$[get value-name=address $i]";
:put "$[get value-name=address $i]  $[get value-name=host-name $i] connections:"; 
[/ip firewall connection print count-only where src-address~"$adr"];
:put " ";
} 
}

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 :slight_smile:

192.168.50.252 Iphone-5 connections:
388

192.168.50.253 HPNOTE125 connections:
34

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".":"];

Right, run my solution. It prints to the screen the way you wanted.

added additional lines to remove garbage :slight_smile:
:put (“\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n”);

Corrected, thank you.