I’m struggling with getting to run script that I found to import IP addresses from file and adding them to my address list.
Idea is to fetch file with all official country IP addresses from Web page (file name is “local.net”) and add them to list.
Fetching script is working fine, bet when I run the script to add IP to list I get only first number added to my address list.
Also, is there “4096 byte read/write limit” workaround?
Maybe there is some scripting guru that can help me?
Try these 5 lines.
just add your own TLD’s, it will add an address list for each TLD. Run it again to update them. You will need to create a firewall rule to drop the TLD lists. Beware the lists are huge (who woulda guessed) and if you load them into memory they will take it all, so select your TLD’s well and don’t go for too many, or at least monitor your memory level.
I am on the moment busy to create backup/restore for adresslists present in the router and it will export a .RSC file that smaller than the normal export.
True, one should always be very vigilant when obtaining some stuff from a 3e party.
A blunt “import” and just executing some imported stuff are not best practices for sure.
As these are block-lists, you could argue that IF the list was maliciously modified and for example legit systems added you could also potentially “disrupt” legit traffic.
I have to say I also do this for this specific list with TLD’s.
So in fact the best thing is to sort of combine existing scripts.
and then below a typical script used for importing others (eg. here a TOR-exit nodes list, similar concept)
The difference is that the IP’s are without any CIDR notation at the end (eg /24 /22 or something) and offcourse do not contain all other routerOS commands, but the regex should extraxt only IP info anyway.
So perhaps the logic of the script below needs to be slightly adapted to form a combined one. This method is safe as IP’s are placed and handled/parsed). Any fake command in the list would cause an error anyway.
→ No need to fetch them, as the above script has the iteration and downloads separate files for the different TLD’s (CN / RU / UA in this example) so $i could be used as the source.
Anybody wants to give a go at this below ?
So basically these TLD’s come from the onsite website in this form for IPv4 and IPv6
Let’s start simple and only look at IPv4
I don’t have the impression that the logic below handles it well. It should throw away/ignore anything besides X.X.X.X/Y and then import it into a ACL.
Regex guru’s here ?
remember than this are wroten for be put directly on script,
if tested on terminal you must add \ before the ?
if tested on regex101 must be removed \ before . and /
What you have post is my version that uses less CPU because it approximates the value, with or without zero, but it is quite reliable.
0.0.0.0/0 to 299.299.299.299/39
000.000.000.000/00 to 299.299.299.299/39
This is the most precise version I have made, but use more CPU, really not soo much:
This match exactly IP-prefix (on the link on this post are present alternative “IP only” and “IP with optional prefix”)
0.0.0.0/0 to 255.255.255.255/32
with or without the 0 like 008.000.000.000/08
But the ACL is not created on my RB3011
I’ve tried the code below, I’ve omitted the “^” because we will not see any match at the beginning of a string anyway.
Tried it on regex101.com but I had to modify it in order to capture/match an IP like 10.20.10.0/12
I add a warning for be clear, also on other post, thanks for make me notice that!!!
remember than this are wroten for be put directly on script,
if tested on terminal you must add \ before the ?
if tested on regex101 must be removed \ before . and /
On regex101 you must remove MikroTik required \ on terminal, but you must not remove the \ on . and on / because must be literal . and / , not any character…
from
((25[0-5]|(2[0-4]|[01]?[0-9]?)[0-9])</big>.){3}(25[0-5]|(2[0-4]|[01]?[0-9]?)[0-9])</big>/(3[0-2]|[0-2]?[0-9])to
((25[0-5]|(2[0-4]|[01]</big>?[0-9]</big>?)[0-9])\.){3}(25[0-5]|(2[0-4]|[01]</big>?[0-9]</big>?)[0-9])\/(3[0-2]|[0-2]</big>?[0-9])and to
((25[0-5]|(2[0-4]|[01]?[0-9]?)[0-9]).){3}(25[0-5]|(2[0-4]|[01]?[0-9]?)[0-9])/(3[0-2]|[0-2]?[0-9])
Hmm, don’t get it. It creates no ACL at all, yet it reports no issues ;-(
So the only way to troubleshoot is execute on CLI and see what comes on the screen ?
But if tested on CLI, then regex-code differs slightly.
First of all you must split on three parts because the IP is not at the start and not at the end, like other lists…
Space replaced with “·”
:do·{·add·address=|2.84.0.0/14|·list=GR·}·on-error={}left delimiter:
address=
right delimiter:
·list=
including space
Example for CLI, I use global just for the test, on script can/must be used local.
# test string
:global teststr ":do { add address=2.84.0.0/14 list=GR } on-error={}\r\n:do { add address=5.54.0.0/15 list=GR } on-error={}"
# remove head and tail, used + 8 because is the character lenght of "address="
# instead of a fixed value can be set also, for example, with [:len $rightstrdelimiter] on script
# notice the needed space before list
:global testip [:pick $teststr ([:find $teststr "address=" -1] + 8) [:find $teststr " list=" -1]]
# now I chech against regexp if is valid IP-prefix
:if ($testip~"((25[0-5]|(2[0-4]|[01]\?[0-9]\?)[0-9])\\.){3}(25[0-5]|(2[0-4]|[01]\?[0-9]\?)[0-9])\\/(3[0-2]|[0-2]\?[0-9])") do={ \
:put "$testip is a IP-prefix"
} else={:put "$testip is NOT a IP-prefix"}
Doing again adding last “right” position value (on example 29), give next IP, without split the file on lines and without modify everytime the data value.
:global testip [:pick $teststr ([:find $teststr "address=" 29] + 8) [:find $teststr " list=" 29]]
:if ($testip~"((25[0-5]|(2[0-4]|[01]\?[0-9]\?)[0-9])\\.){3}(25[0-5]|(2[0-4]|[01]\?[0-9]\?)[0-9])\\/(3[0-2]|[0-2]\?[0-9])") do={ \
:put "$testip is a IP-prefix"
} else={:put "$testip is NOT a IP-prefix"}
Thx for the feedback, I’ll play with that and try to get it working.
I was under the impression that perhaps the parsing was done a bit like “sed” (streaming editor) in Linux/Unix so it would scan the line (until the delimeter \n) and capture the regex logic in there without
the need to split it yourself etc.