From mikrotik newsletter 8 Fetch Tool
For some time now, RouterOS has a neat little feature - it’s called Fetch. It is a HTTP and FTP client program that allows you to download files to your router.
For example fetch in action:
[admin@I] /tool> fetch address=www.mikrotik.com path=index.php host=forum.mikrotik.com
status: connecting
status: downloading
downloaded: 7KiB
duration: 1s
You can use Fetch in scripting - for example to download auto-config files and new RouterOS versions.
If I retrieve a plaintext list of IP’s from a website, is there any way using the MT scripting that I could add these to an address list?
Can’t seem to find any commands to actually parse a file or break it into strings…
also, nothing in the 3.0 documentation about scripting at all.. other than the mikrotik newsletter I would never have even known that a fetch command existed :-/
String parsing like that would be best done elsewhere. I’d have a php or perl process on a linux box elsewhere do the work and return a prepared list to the mikrotik. The fetch cmd can, effectively, remotely execute the linux process.
Hmm, the list I’m referring to would already be preformatted, just looking for the commands I’d use to actually read the file using the mikrotik? or should I get the script to prepare it in the form
can’t you do it the other way - instead of making the router connect somewhere, make some nix box connect to the routers and execute some commands on the ROS?
I could but I figured there may be something i can use to do it on the router(s)
that way I could have multiple routers grabbing different IP lists on a regular basis.
Either way I might as well make it a request… the ability to read a file (or the ability to read address lists from one ROS box on another)
I know that other people do similar things on Linux boxes as normis mentioned (normally for iptables on their actuall box thou) so I figure it would be a good feature to have on the MT’s either for system-wide black lists, adding unmetered IP ranges etc etc.
Okay … here is an example of something I am working on. This works under 3.10, I havent tested earlier versions. I also cannot figure out how to use ‘/tool fetch’ in a script and have it do anything afterwards, so I believe you have to use 2 scripts, one to fetch, and another to parse. If someone wants to explain that let me know.
Hi to all.
Can somebody help me with this script?
I’m trying to modify it to process file from http://www.colocall.net/uaix/prefixes.txt
For fetch script I use “/tool fetch address=www.colocall.net src-path=/uaix/prefixes.txt host=www.colocall.net mode=http” and it works and creates file prefixes.txt
But I can’t manage to add info from this file to address list
It seems that content from file can’t be retrieved.
This part of code doesn’t work and return in log $content=nothing and $contentLen=0 even so I opened file and it is full of lines
:global content [/file get [/file find name=prefixes.txt] contents] ;
:log info $content;
:global contentLen [ :len $content ] ;
:log info $contentLen;
I am not certain you will consider this help, but take a look at
/file print detail
If you do not see a ‘contents’ listing associated with the filename, it is probably because the file is larger than 4095 bytes.
By experience, if there is no ‘contents’ listing, it cannot be loaded/accessed/edited by a script. Maybe someone else can be more help.
Thank you SurferTim for your help.
It was the size of file, when I edited it to 2049 bytes the script correctly processed it and added to address list. But how this limit can be overcomed? I need this big list of networks.
Others have the same challenge. I was hoping there was a “readline” command for text files, where you could read a line a a time from large files (like C and other languages), but have not found anything close. Maybe the MikroTik Team will read this and include something in a future release.
It would read the number of lines in test.txt into $linecount and the 20th (ok, maybe 21st) line from test.txt into $thisline, and subsequent calls without a ‘line number’ parameter retrieves the next line. So the last line is the same as
:set nextline [:readline test.txt 21]