Hi
I like to read and write a file ??.txt
I like to find text in this file and erase or modify it.
It’s possible in RouterOS?
Thanks
Step 1: turn off caps lock. All capital letters equals shouting. Shouting is rude.
Step 2: read http://wiki.mikrotik.com/wiki/Manual:Scripting. There is rudimentary support for text parsing. You cannot read or write files larger than 4096 bytes.
ok, I’ve spent an age reading that page and trying different things but for the life of me I can’t work out how to read a file.
Closest I can find is
/file print detail where name="file.ext"
from http://wiki.mikrotik.com/wiki/Manual:System/File as it at least echos amongst other detail “contents=”
i was hoping to do something like
/tool e-mail send to="email@address" subject="test" body="$[/file print detail where name="file.ext"]"
because I don’t want the file as an attachment, except it doesn’t work. nor does
:set myVar [/file print detail where name=file.ext]];
- it prints to the console but not to the variable.
what’s the answer as per the OP, or what am I doing wrong?
I use this. This example sets global variable “test” to the contents of the file named “ip.txt”. The output from the :put below is correct.
[admin@test] /file> :global test [/file get ip.txt contents];
[admin@test] /file> :put [$test];
*2,*3,*6
:put is to verify the contents of the variable “test”.
:put does not work in a scheduled script, only at the command prompt.
^ thanks for that
/tool fetch url="http://automation.whatismyip.com/######.asp" mode=http;
global getIP [/file get ######.asp contents];
global currentIP
if ($getIP != $currentIP) do={
/tool e-mail send to="thenexus@ihug.co.nz" subject="address" body=[$getIP];
:set currentIP $getIP;
}
seems to work ok. needs more things like a timestamp in the email and error trapping for say if the file can’t be retrieved. find ###### by reading instructions on http://www.whatismyip.com
feedback appreciated
edit 20110704: fixed a booboo with getip/newip
OK. But just so you know, I am using a script function that is not documented. (:execute).
This is a two script deal. One script does the fetch command, and the other reports on the success. If the fetch command fails, the test script halts at that command. The test2 script, started before the fetch call, does not. It will wait 10 seconds for the test script to finish before exiting the while loop if the fetch fails in the test script.
This example attempts to download a file named “test.txt”. You will need to insert your server ip in the fetch command in the test script. The test script has put statements in it, so it will run as a script only from the command line. The test2 script puts the result in the router log. So after you run the test script, check the log.
This is script #1. I named it “test”
:global done false;
:local date [/system clock get date];
:local time [/system clock get time];
:local filename "test.txt";
:local ebody "Report on $date at $time";
:put $ebody;
if ( [:len [/file find name=$filename]] > 0 ) do={
:put "Found";
/file remove $filename;
} else={
:put "Not found";
}
:execute test2;
/tool fetch url="http://xx.xx.xx.xx/$filename" mode=http;
:set done true;
:if ( [:len [/file find name=$filename]] > 0 ) do={
:put "Download ok";
} else={
:put "Download failed";
}
This is script #2. I named it “test2”. If you change this script name, insure to change the “:execute test2” command in the test script.
:global done;
:local count 0;
:local maxcount 10;
:while ($done = false) do={
:set count ($count + 1);
:if ($count = $maxcount) do={
:set done true;
}
:delay 1;
};
:if ($count < $maxcount) do={
:log info "fetch success in $count seconds";
} else={
:log info "fetch failed";
}
This is like a software “watchdog timer”.