Hi all,
i wrote a script, which aggregates 4 tables to display the registered Clients in CAPsMAN with DHCP hostname and the identity of the access-point.
Looks like this:
[user@router] > /system script run wlan
MAC IP Client AP Signal TX Rate RX Rate Uptime Packets Bytes Interface
FF:FF:FF:FC:89:2E 192.168.1.250 Client01 AccessPoint01 -58 173.3Mbps-20MHz/2S/SGI 12Mbps 1d05:21:12.980 189458,116674 52138685,23855249 cap13
FF:FF:FF:11:3E:9C 192.168.1.251 Client02 AccessPoint02 -54 173.3Mbps-20MHz/2S/SGI 12Mbps 1d05:16:53.180 201567,125334 53921014,16576701 cap14
Here the script:
:global fill do={
:local VAR $1;
:local LEN $2;
:local CHAR $3;
:local lenVAR [:len $VAR];
:for i from=1 to=($LEN-[:len $VAR]) step=1 do={
:set VAR ("$VAR" . $CHAR);
}
:return $VAR;
}
:put ([$fill "MAC" 20 " "].[$fill "IP" 16 " "].[$fill "Client" 12 " "].[$fill "AP" 15 " "].[$fill "Signal" 7 " "].[$fill "TX Rate" 23 " "].[$fill "RX Rate" 23 " "].[$fill "Uptime" 16 " "].[$fill "Packets" 20 " "].[$fill "Bytes" 20 " "]."Interface");
:foreach i in=[/caps-man registration-table find .id] do={
:local locMAC [/caps-man registration-table get $i mac-address];
:local locInt [/caps-man registration-table get $i interface];
:local locSignal [/caps-man registration-table get $i rx-signal];
:local loctxRate [/caps-man registration-table get $i tx-rate];
:local locrxRate [/caps-man registration-table get $i rx-rate];
:local locUptime [/caps-man registration-table get $i uptime];
:local locPack [/caps-man registration-table get $i packets];
:local locBytes [/caps-man registration-table get $i bytes];
:local locRadMAC [/caps-man interface get [/caps-man interface find name=$locInt] radio-mac];
:local locCAP [/caps-man radio get [/caps-man radio find radio-mac=$locRadMAC] remote-cap-identity];
:local locIP [/ip dhcp-server lease get [/ip dhcp-server lease find mac-address=$locMAC] address];
:local locName [/ip dhcp-server lease get [/ip dhcp-server lease find mac-address=$locMAC] host-name];
:put ([$fill $locMAC 20 " "].[$fill $locIP 16 " "].[$fill $locName 12 " "].[$fill $locCAP 15 " "].[$fill $locSignal 7 " "].[$fill $loctxRate 23 " "].[$fill $locrxRate 23 " "].[$fill $locUptime 16 " "].[$fill $locPack 20 " "].[$fill $locBytes 20 " "]."$locInt");
}
Best regards
Bernhard
that is some awesome work Bernhard!
Thank you for sharing this!
Works perfectly on the old capsman. I’ll try to adapt it to work on the new capsman and if i succeed, i’ll post it here
here goes the updated script:
:global fill do={
:local VAR $1;
:local LEN $2;
:local CHAR $3;
:for i from=1 to=($LEN-[:len $VAR]) do={
:set VAR ("$VAR" . $CHAR);
}
:return $VAR;
}
# Header
:put ([$fill "MAC-ADDRESS" 20 " "] . [$fill "IP" 16 " "] . [$fill "HOSTNAME" 16 " "] . [$fill "SSID" 16 " "] . [$fill "AP-INTERFACE" 16 " "] . [$fill "SIGNAL" 8 " "] . [$fill "UPTIME" 16 " "] . "LAST-ACTIVITY");
# Iterate over registered clients
:foreach idx in=[/interface wifi registration-table find] do={
# Core fields
:local mac [/interface wifi registration-table get $idx mac-address];
:local iface [/interface wifi registration-table get $idx interface];
:local ssid [/interface wifi registration-table get $idx ssid];
:local signal [/interface wifi registration-table get $idx signal];
:local uptime [/interface wifi registration-table get $idx uptime];
:local lastAct [/interface wifi registration-table get $idx last-activity];
# DHCP lookup with error handling
:local ip "";
:local hn "";
:do {
:set ip [/ip dhcp-server lease get [/ip dhcp-server lease find mac-address=$mac] address];
} on-error={
:set ip "No-DHCP";
}
:do {
:set hn [/ip dhcp-server lease get [/ip dhcp-server lease find mac-address=$mac] host-name];
} on-error={
:set hn "Unknown";
}
# Output row
:put ([$fill $mac 20 " "] . [$fill $ip 16 " "] . [$fill $hn 16 " "] . [$fill $ssid 16 " "] . [$fill $iface 16 " "] . [$fill $signal 8 " "] . [$fill $uptime 16 " "] . $lastAct);
}
heres an updated version that truncates the hostnames so columns stay alligned:
:global fill do={
:local VAR $1;
:local LEN $2;
:local CHAR $3;
:local lenVAR [:len $VAR];
:for i from=1 to=($LEN-$lenVAR) do={
:set VAR ("$VAR" . $CHAR);
}
:return $VAR;
}
:global truncate do={
:local STR $1;
:local MAXLEN $2;
:if ([:len $STR] > $MAXLEN) do={
:return ([:pick $STR 0 ($MAXLEN-3)] . "...");
} else={
:return $STR;
}
}
# Header with better spacing
:put ([$fill "#" 4 " "] . [$fill "MAC-ADDRESS" 18 " "] . [$fill "IP" 16 " "] . [$fill "HOSTNAME" 20 " "] . [$fill "SSID" 20 " "] . [$fill "AP-INTERFACE" 18 " "] . [$fill "SIGNAL" 8 " "] . [$fill "UPTIME" 12 " "] . "LAST-ACTIVITY");
# Add separator line
:put ([$fill "-" 4 " "] . [$fill "-" 18 " "] . [$fill "-" 16 " "] . [$fill "-" 20 " "] . [$fill "-" 20 " "] . [$fill "-" 18 " "] . [$fill "-" 8 " "] . [$fill "-" 12 " "] . "-------------");
# Initialize counter
:local counter 0;
# Iterate over registered clients
:foreach idx in=[/interface wifi registration-table find] do={
:set counter ($counter + 1);
# Core fields
:local mac [/interface wifi registration-table get $idx mac-address];
:local iface [/interface wifi registration-table get $idx interface];
:local ssid [/interface wifi registration-table get $idx ssid];
:local signal [/interface wifi registration-table get $idx signal];
:local uptime [/interface wifi registration-table get $idx uptime];
:local lastAct [/interface wifi registration-table get $idx last-activity];
# DHCP lookup with error handling
:local ip "";
:local hn "";
:do {
:set ip [/ip dhcp-server lease get [/ip dhcp-server lease find mac-address=$mac] address];
} on-error={
:set ip "No-DHCP";
}
:do {
:set hn [/ip dhcp-server lease get [/ip dhcp-server lease find mac-address=$mac] host-name];
} on-error={
:set hn "Unknown";
}
# Truncate long fields to fit columns
:local hnTrunc [$truncate $hn 20];
:local ssidTrunc [$truncate $ssid 20];
:local ifaceTrunc [$truncate $iface 18];
# Output row with counter and better alignment
:put ([$fill $counter 4 " "] . [$fill $mac 18 " "] . [$fill $ip 16 " "] . [$fill $hnTrunc 20 " "] . [$fill $ssidTrunc 20 " "] . [$fill $ifaceTrunc 18 " "] . [$fill $signal 8 " "] . [$fill $uptime 12 " "] . $lastAct);
}
# Add footer with total count
:put "";
:put ("Total clients: " . $counter);
P.S. i am no dev, i used an LLM