mikrotikrouter-os

Comparison script for bytes value mikrotik


i want to comparison bytes value (tx-bytes,rx-bytes) downloaded by user with my pattern for example (100MiB, 50MiB) but i can't write script for solve this , how can i do that ?

the place i want write script for that is : /interface wireless registration-table > tx-rx bytes value (an image of this Place)


Solution

  • As you state, you can query registration table using /interface wireless registration-table. Putting this on a scripting loop gives

    :foreach wirelessClient in [/interface wireless registration-table find true] do={}
    

    then, in this foreach loop, get each client's statistics using the id (example here with "mac-address" and "bytes" fields)

    :local macAddress [/interface wireless registration-table get [ find .id=$wirelessClient ] value-name=mac-address];
    :local bytes [/interface wireless registration-table get [ find .id=$wirelessClient ] value-name=bytes];
    

    The "bytes" data returned is in format "RX,TX" (i.e "545124,25422"). So you have to do a little trick to extract the RX string: look for the "," character and extract string before this comma.

    :local posComma [ :find $bytes "," -1]
    :local RXbytes [:pick $bytes 0 $posComma]
    

    Then, it's easy to check if that client has downloaded more than 50M

      :local status "Below 50M";
      :if ($RXbytes > 50000000) do={ 
        :set $status "Above 50M"
      }
      :put "Client: $macAddress $bytes $RXbytes -> $status";
    

    Put all together in a script and execute it, you will get something like

    [foo@bar] > import test.rsc 
    Client: D4:CA:6D:B2:AE:EF 202134,92985 202134 -> Below 50M
    Client: 48:9D:24:64:E6:08 8057465,2022134 8057465 -> Below 50M
    Client: 2C:56:DC:32:09:F0 3282973,888408 3282973 -> Below 50M
    Client: 48:59:29:F6:DF:5D 139664,248984 139664 -> Below 50M
    Client: A4:5E:60:EB:AE:33 159890028,12433385 159890028 -> Above 50M
    

    For the complete Mikrotik scripting manual, check http://wiki.mikrotik.com/wiki/Manual:Scripting