Community discussions

MikroTik App
 
nydiow
just joined
Topic Author
Posts: 8
Joined: Wed Feb 22, 2012 7:27 pm

Writing interfaces address to a file

Tue Mar 13, 2012 3:52 pm

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"
Here is what i got:

# mar/13/2012 10:47:40 by RouterOS 5.14
# software id = 4UMY-QSP2
#
# NAME TYPE SIZE CREATION-TIME
0 skins directory jan/01/1970 21:12:43
1 rb433uah.pem .pem file 887 mar/02/2012 11:07:18
2 um-before-m... .tar file 15 872 jan/01/1970 21:00:37
3 ca.crt .crt file 1 224 mar/02/2012 11:07:18
4 rb433uah.crt .crt file 3 774 mar/02/2012 11:07:18
5 autosupout.... .rif file 336 778 mar/06/2012 16:22:47
6 autosupout.rif .rif file 322 834 mar/07/2012 16:02:49
7 pub directory mar/06/2012 15:20:17
8 1wan,2lan,3... backup 17 570 mar/08/2012 16:49:39


Anyone?

Thanks,

Regards,
 
airock
newbie
Posts: 30
Joined: Fri Oct 09, 2009 11:57 pm

Re: Writing interfaces address to a file

Tue Mar 13, 2012 6:57 pm

Did you actually execute a script or just typed the commands above at the command-line prompt?

If you are testing at the command-line, try this sequence:
/file print file=test
/file set test.txt contents="$[/ip address get [find interface="ether1-wan1"] address]"
Let me know if it works as expected.

Regards,
rock
 
nydiow
just joined
Topic Author
Posts: 8
Joined: Wed Feb 22, 2012 7:27 pm

Re: Writing interfaces address to a file

Tue Mar 13, 2012 8:40 pm

Dont know why but adding /delay 10, its works properly.

Now i have a new interesting question:

Why it works(on script)?
:if ( [/interface ovpn-client get ovpn-comnect-ebt disabled]=no ) do={
:put "VPN EBT UP"
:global vpn1 [/ip address get [find interface="ovpn-comnect-ebt"] address]
:put "$vpn1"
}
And why it does not work (on script)?
:if ( [/interface ovpn-client get ovpn-comnect-ebt disabled]=no ) do={
:put "VPN EBT UP"
:global vpn1 [/ip address get [find interface="ovpn-comnect-ebt"] address]
}

:put "$vpn1"

expected end of command (line 23 column 7)
Is does not make any sense!
 
airock
newbie
Posts: 30
Joined: Fri Oct 09, 2009 11:57 pm

Re: Writing interfaces address to a file

Tue Mar 13, 2012 9:39 pm

Apparently it does not make sense, but actually does.

See RouterOS scripting manual section 1.1.6. There is a nice red warning waiting for you to read.

(...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. :D

Regards,
rock
 
changeip
Forum Guru
Forum Guru
Posts: 3830
Joined: Fri May 28, 2004 5:22 pm

Re: Writing interfaces address to a file

Wed Mar 14, 2012 1:38 am

/file print file=test

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)

Sam
 
airock
newbie
Posts: 30
Joined: Fri Oct 09, 2009 11:57 pm

Re: Writing interfaces address to a file

Fri Mar 16, 2012 9:16 pm

/file print file=test

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)

Sam
Play it again, Sam.

...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:

1) 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.

2) 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.

:-)

Regards,
rock
 
changeip
Forum Guru
Forum Guru
Posts: 3830
Joined: Fri May 28, 2004 5:22 pm

Re: Writing interfaces address to a file

Fri Mar 16, 2012 11:30 pm

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?

Who is online

Users browsing this forum: No registered users and 17 guests