Limit the speed of wi-fi client only if they download a lot

Hi,


I try to create a script to limit the speed on my wireless client only if they download a lot and only for a period of time.

I am thinking of a script that will be execute at eatch 15 minute and if a mac-address on the wireless registration table have more that $allowedbytes more on the TXBytes or RxBytes, we put an access-list with " AP-TX-LIMIT " on this mac.

At eatch start of the script I could remove all access-list to put all user on their true speed and check again.

Here is a start … can somebody help me for the array thing to remember somewhere the last value to be abble to check if the client have download more that X bytes and apply or not the bandwith limit for the next 15 min.

# define an array to put the mac, TxBytes and RxBytes

# clear all the access list with that have the comment "AddedbyPURscript"

:foreach i in=[ /int wir reg find ap=no] do={
:local iBytes [:toarray [/interface wireless registration-table get $i bytes]]
:local TxBytes [:tonum [:pick $iBytes 0 1]]
:local RxBytes [:tonum [:pick $iBytes 1 2]]
:log info ( "TEST ==> " . [/system identity get name] . " -- " . $i . " -- " . [int wir reg get $i mac-address] . " -- TX : " . $TxBytes . "  RX: " . $RxBytes )

#check if the RX or TX bytes is more that $allowedbytes of difference for eatch mac-address

# if the RX or TX bytes is more that $allowedbytes of difference limit the download speed

#interface wireless access-list add mac-address="" ap-tx-limit="512000" comment="AddedbyPURscript"

# Put the data in a global array for the next test

}

Thanks, Math

I have created a script that does the job … here it is !

We test it currently on 1 AP, seem to does the job …

So I share it with you !

This script run eatch 5 minutes on our test AP

#LOG THE START OF THE SCRIPT
:log info ( "PUR TEST START ===> " . [/system identity get name] )

#DEFINE ARRAY TO PUT; mac, TxBytes and RxBytes

:global regarray
:global txarray
:global rxarray

#DEFINE SOME LOCAL VARIABLE

:local allowedbytes 5242880
:local onpurspeed 512000
:local y


###### UNCOMMENT TO CLEAR ALL "PUR-Script" ACL ON WIRELESS #####
#:foreach i in=[ /int wir acc find comment="PUR-Script"] do={
#    /int wir acc remove $i
#}
################################################################

#MAKE THE TEST ON EATCH REGISTRATION

:foreach i in=[ /int wir reg find ap=no] do={

     :local macaddr  [int wir reg get $i mac-address]
     :local iBytes [:toarray [/interface wireless registration-table get $i bytes]]
     :local TxBytes [:tonum [:pick $iBytes 0 1]]
     :local RxBytes [:tonum [:pick $iBytes 1 2]]

#### uncomment next line for debug ####
#     :log info ( "REG INFO ==> " . [/system identity get name] . " -- " . $i . " -- " . $macaddr . " -- TX : " . $TxBytes . "  RX: " . $RxBytes )
#######################################

     :local arraypos [:put [:find $regarray $macaddr]]

#### uncomment next line for debug ####
#     :log info (  $arraypos . " / " . [:len $regarray] )
#######################################

     :local regtest [:put [:pick $regarray $arraypos]]
     :local oldTxBytes [:put [:pick $txarray $arraypos]]
     :local oldRxBytes [:put [:pick $rxarray $arraypos]]

#### uncomment next line for debug ####
#     :log info ( "OLD REG INFO ==> " . [/system identity get name] . " -- " . $arraypos. " -- " . $regtest . " -- TX : " . $oldTxBytes . "  RX: " . $oldRxBytes )
#######################################


#CHECK IF THE TX BYTES IS MORE THAT $allowedbytes of difference for eatch mac-address 

     :local testvalue (  [:tonum $oldTxBytes] + [:tonum $allowedbytes] )
     :if ( $testvalue < [:tonum $TxBytes] )  do={ 

#IF THE TX BYTES IS MORE THAT $allowedbytes OF DIFFERENCE ... LIMIT THE DOWNLOAD SPEED

          :local testvaluemeg  [:put ($testvalue / 1048576)]  
          :local TxBytesmeg [:put ($TxBytes / 1048576)]

          :local accid [/int wir acc find mac-address=$macaddr]
          :if ( $accid != "" ) do= { 

#IF WE FIND THAT ACL ALREADY APPLY TO CPE, CHECK IF IT'S PUR-SCRIPT ACL

               :if ( [/int wir acc get $accid comment] = "PUR-Script"  ) do= { 
               

#IF PUR-SCRIPT ACL ALREADY APPLY AND CLIENT MUST BE ON THE PUR, SIMPLY LEAVE THE ACL THERE
               
                    :log info (  "PUR ALERT ==> " . [/system identity get name]  . " ( " .  $macaddr . " ) already on the PUR and will stay ( " . $TxBytesmeg . " Meg / was allow " . $testvaluemeg . " Meg )" )
                }
           } else={

#IF PUR-SCRIPT ACL NOT ALREADY APPLY AND CLIENT MUST BE ON THE PUR, PUT THE ACL ON THE MAC-ADDRESS

               :log info (  "PUR ALERT ==> " . [/system identity get name]  . " ( " .  $macaddr . " ) has download to mutch ( " . $TxBytesmeg . " Meg / was allow " . $testvaluemeg . " Meg ) and will be affected by the PUR " )
               /interface wireless access-list add mac-address=$macaddr ap-tx-limit=$onpurspeed comment="PUR-Script"

           }       

      } else={

#ELSE THE CPE MUST NOT BE AFFECTED BY THE PUR SO REMOVE IT IF IT HAD AN ACL ON IT

          :foreach y in=[ /int wir acc find mac-address=$macaddr] do={
              :if (  [/int wir acc get $y comment] = "PUR-Script" ) do={
              :log info (  "PUR ALERT ==> " . [/system identity get name]  . " ( " .  $macaddr . " ) removed from PUR" ) 
               /int wir acc remove $y
          }
      }
    }
}

#CLEAN AND REPOPULATE THE ARRAY

:set regarray ""
:set txarray ""
:set rxarray ""

:foreach i in=[ /int wir reg find ap=no] do={
     :local macaddr  [int wir reg get $i mac-address]
     :local iBytes [:toarray [/interface wireless registration-table get $i bytes]]
     :local TxBytes [:tonum [:pick $iBytes 0 1]]
     :local RxBytes [:tonum [:pick $iBytes 1 2]]


#### uncomment next line for debug ####
#     :log info ( "TEST 2 ==> " . [/system identity get name] . " -- " . $i . " -- " . $macaddr . " -- TX : " . $TxBytes . "  RX: " . $RxBytes )
#######################################

     :set regarray ( [:toarray $macaddr] + $regarray)
     :set txarray ( [:toarray $TxBytes] + $txarray )
     :set rxarray ( [:toarray $RxBytes] + $rxarray )

}

#LOG THE END OF THE SCRIPT
:log info ( "PUR TEST STOP ===> " . [/system identity get name] )