Is there a way to Import a long list of CIDR IP ranges into the Firewall in ROS?
For example:
If I wanted to Deny the following ranges in a Rule. Do I need to create 1 rule for each range? or is there a way to have the rule refer to a text file that I upload with all of the ranges? or Can I import all of them into the single rule?
There’s many ways to approach that. You could create a script and copy/paste it in, or upload it and import the script. You could write a script that parses lines in a file and inserts firewall rules based on them.
But if you need to apply the same rules to just a lot of IP ranges, the best approach is to create firewall address-lists and refer to src-address-list or dst-address-list in the one firewall rule.
and run it after you upload it. If you already have a list of just the networks it’d be easy to use Perl or any other scripting language to generate such a script.
FWIW the below seems to work for the RouterOS scripting language. Adjust the first two non-comment lines to your environment, and adjust the line below if you’re not using Windows line delimiters:
# set some parameters
:local fileName "address-ranges.txt";
:local addressList "myListName";
# set to "\r\n" below for Windows text files, "\n" for everything else
:local lineDelimiter "\r\n";
# get a hold on the file
:local filePointer [/file find name=$fileName];
# check if it exists
:if ($filePointer != "") do={
# file exists, start a counter for the entries inserted
:local added 0;
# read the configuration file into a buffer
:local buffer [/file get $filePointer contents];
# record the total buffer size for later use in do loop cycling through the lines
:local bufferSize [:len $buffer];
# create variable to hold buffer offset from start to current line end
:local lineEnd 0;
# create variable that will hold the current line
:local line "";
# create variable that keeps track of current position in buffer
:local position 0;
# go through buffer
:do {
# find next line end
:set lineEnd [:find $buffer $lineDelimiter $position];
# fetch the line
:set line [:pick $buffer $position $lineEnd];
# advance position skipping the newlines
:set position ($lineEnd + [:len $lineDelimiter]);
# verify that the line isn't empty
:if ([:len $line] > 0) do={
/ip firewall address-list add list="$addressList" address="$line";
:set added ($added + 1);
} else={
# invalid entry in file, log that fact
:log warning "Cannot insert address-list entry, corrupt line in file!"
}
} while=($lineEnd < $bufferSize);
# check how many entries were inserted - if any, log for informational purposes
:if ($added > 0) do={
:log info "Inserted $added new address-list entries."
} else={
# if 0, check if the buffer read from the file was larger than 0. if so we should have had inserts
:if ($bufferSize > 0) do={
:log warning "address-list insertion file had content, but none were inserted!"
}
}
} else={
# file could not be opened. log that fact
:log warning "Unable to open file $fileName!"
}
Sadly it does not work for me.
I tried it but 6 hours of waiting(one core is occupied by it still) and still nothing.
What am i dong wrong?
RouterOS - 6.30.2
It was for big enough files or i misunderstood it?
My list is surely MUCH bigger than 4kib.
It has 257969 lines (4341454 bytes).
Also i tried to make it with bash(to prepare rsc for import, on bash all works good) but it goes wrong(on mikrotik) and stops on hitting something in other ban list(which is dynamic list of attackers).