I have tried to compile a script that would monitor the total number of packets according to individual IP’s and block those IP’s in the firewall.
I still have to add a line to reset the queue counters after the script has run, however the beginnings of the script is below, I have tried to run this script and it doesn’t work.
This is my first script, so please feel free to tell me what I am doing wrong.
To troubleshoot a script, I start by enclosing the whole script in brackets and pasting into the terminal (right-click, paste). This allows you to use local variables and also see errors in the script. These brackets can be removed when script is ready for production:
{
script...
....
}
I re-worked the script a bit. Instead of looping through all 254 IP addresses, it finds all simple queues that contain 192.168.1 and then checks packet totals.
One note on target-addresses: Because there can be multiple ones, the addresses are returned as an array. The script assumes that there is only one target-address per queue, and it is converted to a string. If queues have multiple addresses, the script will need to be changed.
:local interval 10;
:local threshold 2000;
:local recipients {"user@example.com"}
/queue simple
# find simple queues that contain base IP
:foreach i in=[find target-addresses~"192.168.1"] do={
# convert target-addresses array to a string
:local ip [:tostr [get $i target-addresses]]
:local packets [get $i total-packets]
:if ($packets / $interval > $threshold) do={
/ip firewall filter add action=reject chain=forward src-address=$ip disabled=no
:foreach j in=$recipients do={
:put ("Sending email to " . $j)
/tool e-mail send ...
}
}
}
Thanks for the help skot, I will use your way of testing scripts in future, will certainly be better.
Thanks for the advice on target-addresses as well.
I like the way the re-worked script is, however there are certain queues that I don’t want to be part of this. Is there a way to exclude some queues in the re-worked script, or would it be better to try another way of achieving the same goal?
I was going to use :for from= to= to achieve this as the queues I want to skip are 192.168.1.1 and 192.168.1.200-254