Hi, i have web auth system and one of the part is temporary enabling user IP adress in address list. My web auth system save whitelisted IP in DB and Mikrotik can download it by fetching URL.
IP is returned like that:
10.0.0.1
10.0.0.2
10.0.0.3
10.0.0.4
in UTF-8/LF, new line separator is \n
My script (parts is stolen from internet):
:global replaceChar do={
:for i from=0 to=([:len $1] - 1) do={
:local char [:pick $1 $i]
:if ($char = $2) do={
:set $char $3
}
:set $output ($output . $char)
}
:return $output
}
:global contentA [/tool fetch url="http://192.168.1.4/security/whitelist_ip.txt" as-value output=user];
:global content [ $replaceChar $contentA "finished" "" ];
:global contentLen [ :len $content ];
:global lineEnd 0;
:global line "";
:global lastEnd 0;
:global IPWhitelistname "web_whitelisted";
/ip firewall address-list remove [/ip firewall address-list find list=$IPWhitelistname]
:do {
:set lineEnd [:find $content "\n" $lastEnd ] ;
:set line [:pick $content $lastEnd $lineEnd] ;
:set lastEnd ( $lineEnd + 1 ) ;
#If the line doesn't start with a hash then process and add to the list
:if ( [:pick $line 0 1] != "#" ) do={
:global entry [:pick $line 0 $lineEnd ]
:if ( [:len $entry ] > 6) do={
/ip firewall address-list add list=$IPWhitelistname address=$entry timeout="1d 00:00:00";
:set entry "";
}
}
} while ($lineEnd < $contentLen)
}
Script si working, i will get IP adress in address list but script generate one record with first ip address in file whitout first character. So example of whitelisted IP generate this records:
61 D web_whitelisted 10.0.0.1 mar/27/2020 09:11:08 23h56m50s
62 D web_whitelisted 10.0.0.2 mar/27/2020 09:11:08 23h56m50s
63 D web_whitelisted 10.0.0.3 mar/27/2020 09:11:08 23h56m50s
64 D web_whitelisted 10.0.0.4 mar/27/2020 09:11:08 23h56m50s
65 D web_whitelisted 0.0.0.1 mar/27/2020 09:11:08 23h56m50s
Second problem is, if i dont put empty line on end of Address file, last IP is ignored and record with missing first character is generated (but for first IP in list).
Note: Variables id GLOBAL for debugging, in production will be local, exept the function at start.
Thank you for any help.