Hello guys, im quite new here but i always get some help here, and now i found out a new problem.
Im trying to write into a file my ether1-wan1 address, but i always get a list of my /files directory written on it instead the address, my research ive seen problems with files bigger than 4096, and this same problem that i found out about writing the directory instead the proper information. Can someone help me out?
Here is my script:
:local newIP [/ip address get [find interface="ether1-wan1"] address];
/file print file=test
/file set test.txt contents="$newIP"
(…and, yes, RouterOS scripting is HORRIBLE, but that’s all we have right now; there was an attempt to support LUA scripting in the past but it was discontinued. By the way I would feel perfectly at home with a Tcl/tclsh implementation, as in Cisco IOS).
Anyway, the more a language is hugly, the more you will benefit from… reading the manual.
the above prints the directory listing to file=test. You dont want that, you want to print the contents of the file, not write to it. Try something like:
/file get “test.txt” contents (not exact syntax, but you get the idea)
…sorry for the joke, but it was too beautiful to go without it! I’m referring, of course, to the famous line from the movie Casablanca (actually a misquotation, but this is another story). Back on the matter.
You missed the point (…well, more than one).
What nydiow wanted was exactly to write into a file: re-read his original message. And, apparently, he was doing it in a correct way, so the problem was elsewhere.
At the best of my knowledge, in RouterOS there are no provisions to write some specific content directly into a file (may be that latest versions changed this: if so I would be glad to be corrected and to learn something new); so we have to use a trick. The usual workaround is as follows:
first, create the file with the command:
/file print file=test
This, as you correctly said, creates a file named “test.txt” containing the listing of the top directory.
second, change the contents of the file to what you want:
/file set test.txt contents="Hello world!"
I don’t know why it didn’t originally work for nydiow: my first thought was that he tried his sequence of commands at the console prompt, missing the fact that local variables are not retained between different command-lines (when typing commands at the console the scope and lifetime for local variables is limited to each single command-line). And that’s the reason for my hint. And, to be true, I have more than one doubt about the “delay” stuff: the second question confirmed some nydiow’s misunderstanding on the “peculiarity” of RouterOS scripting about variables and their scope.
Hope that my hints and comments will push both of you in carefully reading the manual: definitely NOT the best manual I’ve ever read but, again, that’s what we have. And it sure has the answers to most of the basic questions.
ah, I see … working around a limitation that you can’t create a file by writing directly to it first. I see. Maybe the delay just gives the script enough time to close the file properly. . . can you live with a :delay 1 in the script?