Do want, also I’ve asked for a feature to write to files also.
Would be very useful for exporting address lists to a csv file or similar.
See: http://forum.mikrotik.com/t/feature-request-write-to-file/22860/1
Do want, also I’ve asked for a feature to write to files also.
Would be very useful for exporting address lists to a csv file or similar.
See: http://forum.mikrotik.com/t/feature-request-write-to-file/22860/1
Hi omega-00
How about writeline? But to simplify matters, it would be easier to use a sequential write rather than random, like “_open +a” in C. Just appends lines to the file, but in the correct format. Editing large text files, especially adding longer lines near the beginning of the file can be memory intensive. Usually the process requires rebuilding the file completely. That ok with you? Like:
:if ([:writeline test.txt $newline] = [:len $newline]) do={:set writeok true} else={:set writeok false}
Creates /file/test.txt if it doesn’t exist. Returns the length of the write in bytes. Zero or -1 on fail.
You could use this in conjunction with readline to rebuild large text files on your own. Just watch your memory.
STILL WISHING!! ![]()
There is a way to write a file:
#create file
/file print file=test
#write to file
/file set test.txt contents="lalala";
#add another line
/file set test.txt contents=( get test.txt contents ] . "\nlalalala" );
I know it is not a good way to write to file, but still it is something to work with.
Now the bad news. 4095 bytes max. ![]()
Yes, forgot to mention that.
Hi mrz
The file we are discussing has about that many lines. It is a list of IP addresses that are to be entered into a list. I presume this is not a list you want to get on. Most of the list is remarks, but there are several hundred IP addresses in the list, and the file size is at about a meg.
It would be unreasonable to expect a single variable ‘contents’ to hold the entire file, but a line at a time would be manageable, don’t you think?
I think it’s clear that we need to be able process big files and Mikrotik please add this as future request. Until that working with file is imposible, what if today file small and processed but tommorow it will be a little bigger and everything will be broken?
I agree completely.
Hi to all, as you are able to read form a file, and making with that a command.
Example, in a file " users-list" read a mac form a user and for example deny the connection.
My problem is that i have to read from a file 16Mb of porn urls and put them on the proxy to deny.
1- I don´t know if the web proxy could handled so much information and also if the time of reading this dictionary it´s possible
2- cuold you give me some lines of the script??
Thx
Its not going to work, as noted above the string will only hold a maximum of 4095 bytes.
If I were you I’d instead try using a service like OpenDNS (http://www.opendns.com) to block these for you.
mrz, is there any way to use the file-writing you’ve mentioned to include a variable? I can’t seem to get it to accept anything other than direct text in the contents field :-/
:global ipAddr 1.1.1.1
/file print file=test
/file set test.txt contents="lalala $ipAddr "
/file :put [ get test.txt contents ]
Output:
lalala 1.1.1.1
So works for me.
Was my fault, is working fine now. Thanks!
I tried to use the script for the spamhaus drop list but it does not work. I am running 3.19 on my router. I even tried manually downloading the lasso file and running it and it never populated the firewall address lists. There was no errors in the logs either. Is this script somewhere that is known to work with 3.19 or better?
Thanks
Try running the script directly from terminal, that’ll show you if it stops at some error and if so will give the error line and the reason for stopping.
[admin@Krusher] > system script run not-working-script
syntax error (line 1 column 9)
[admin@Krusher] >
The dshield part works but no rules are ever create for the spamhaus… I am running 3.23 and here is what I am running:
/tool fetch address=feeds.dshield.org host=feeds.dshield.org mode=http src-path=block.txt
/tool fetch address www.spamhaus.org host www.spamhaus.org mode http src-path drop/drop.lasso
:if ( [/file get [/file find name=block.txt] size] > 0 ) do={
/ip firewall address-list remove [/ip firewall address-list find list=dshield]
:global content [/file get [/file find name=block.txt] contents] ;
:global contentLen [ :len $content ] ;
:global lineEnd 0;
:global line “”;
:global lastEnd 0;
:do {
:set lineEnd [:find $content “\n” $lastEnd ] ;
:set line [:pick $content $lastEnd $lineEnd] ;
:set lastEnd ( $lineEnd + 1 ) ;
:if ( [:pick $line 0 1] != “#” ) do={
:if ([:typeof [:toip [:pick $line 0 [:find $line “\t”] ] ] ] != “nil”) do={
:local pos1 [:find $line “\t” 0]
:local pos2 [:find $line “\t” $pos1]
:local pos3 [:find $line “\t” $pos2]
:log info ( "DShield Entry: " . [:pick $line 0 $pos1 ] . “/” . [:pick $line ($pos2+1) $pos3 ] )
/ip firewall address-list add list=dshield address=( [:pick $line 0 $pos1 ] . “/” . [:pick $line ($pos2+1) $pos3 ] )
}
}
} while ($lineEnd < $contentLen)
}
:if ( [/file get [/file find name=drop.lasso] size] > 0 ) do={
/ip firewall address-list remove [/ip firewall address-list find list=spamhaus.lasso]
:global content [/file get [/file find name=drop.lasso] contents] ;
:global contentLen [ :len $content ] ;
:global lineEnd 0;
:global line “”;
:global lastEnd 0;
:do {
:set lineEnd [:find $content “\n” $lastEnd ] ;
:set line [:pick $content $lastEnd $lineEnd] ;
:set lastEnd ( $lineEnd + 1 ) ;
:if ( [:pick $line 0 1] != “;” ) do={
:if ([:len [:pick $line 0 [:find $line “;”] ] ] > 0 ) do={
:local pos1 [:find $line “;” 0]
:local entry [:pick $line 0 ($pos1-1) ]
:if ( [:len $entry ] > 0 ) do={
:log info “Lasso Entry: $entry”
/ip firewall address-list add list=spamhaus.lasso address=$entry
}
}
}
} while ($lineEnd < $contentLen)
}
If I run the command via command console I get this:
[admin@GATEWAY] /system script> run droplist
status: finished
status: finished
[admin@GATEWAY] /system script>
But no spamhaus rules…
If I run the command in winbox by clicking run it download and creates the drop files but neither dshield nor spamhause firewall rules and remove or created?
this is another one of those ‘RouterOS upgrades breaks my scripts’. I guess we need to start coding for particular versions now… and put that in the script comments. I will try to troubleshoot this sometime next week, no time at the moment. I spend way to much time in this place I am realizing : )
Is the script being “broken” in that the second fetch will not execute? The script engine seems to take the next like as input to the status display of the first fetch, so the second fetch doesn’t get executed.
In other words, have you figured out a way to get the script to “pause” until the fetch completes?
your second /tool fetch is missing the = signs.
RouterOS4 will support LUA, which is kind of standard.