Page 1 of 1

Mikrotik script how to line break

Posted: Mon Nov 18, 2013 6:02 pm
by Vyrtu
Hi, i have this script:

ros code

:if ([/ping address=8.8.8.8 count=25]=25)  do={

/file set miconexion.txt contents [/system clock get date]

} else={/file set miconexion.txt contents [/system clock get date]}
I need line break for each script run like that:
nov/18/2013
nov/18/2013
nov/18/2013
nov/18/2013

And this script only replace the first line for each run..

Re: Mikrotik script how to line break

Posted: Mon Nov 18, 2013 6:23 pm
by boen_robot
You need to explicitly add to the contents, rather than replace it. Use the "/file" menu's "get" command to get the current contents.

i.e.

ros code

/file set miconexion.txt contents ([/file get miconexion.txt contents] . "\n" . [/system clock get date])

Re: Mikrotik script how to line break

Posted: Mon Nov 18, 2013 6:56 pm
by Vyrtu
You need to explicitly add to the contents, rather than replace it. Use the "/file" menu's "get" command to get the current contents.

i.e.

ros code

/file set miconexion.txt contents ([/file get miconexion.txt contents] . "\n" . [/system clock get date])
NICE! The script works, now i need to write a string, i have this:

ros code

:if ([/ping address=8.8.8.8 count=25]=25)  do={

/file set miconexion.txt contents ([/file get miconexion.txt contents] . "\n" . [/system clock get date] . "\_" . [/system clock get time] ."\_ ". "Conexion" . )

} else={/file set miconexion.txt contents [/system clock get date]}
How i can write strings?

Re: Mikrotik script how to line break

Posted: Mon Nov 18, 2013 7:01 pm
by boen_robot
How i can write strings?

ros code

"Conexion"
is a string...

So... you know... just change it to whatever you want to write. In the case of the above, you've written the word Conexion.

Re: Mikrotik script how to line break

Posted: Tue Nov 19, 2013 10:27 am
by Vyrtu
Nice it's working now, i put the final script, if somebody needs it:

ros code

:if ([/ping address=8.8.8.8 count=25]=25)  do={

/file set miconexion.txt contents ([/file get miconexion.txt contents] . "\n" . [/system clock get date] . "\_" . [/system clock get time] ."\_ ". "Conexion")

} else={/file set miconexion.txt contents [/system clock get date]}

pd: The script didnt run before because i didnt need the last concat ".. .\_ ". "Conexion" .)"

Re: Mikrotik script how to line break

Posted: Wed Nov 27, 2013 11:25 am
by Vyrtu
Hi again guys, i have a little problem with this script, this is the final code:

ros code

:if ([/ping address=8.8.8.8 count=4]=4)  do={

/file set miconexion.rsc contents ([/file get miconexion.rsc contents] . "\n" . [/system clock get date] . "\_" . [/system clock get time] ."\_ ". "Conexion")

} else={/file set miconexion.rsc contents ("\n" . [/system clock get date] . "\_" . [/system clock get time] ."\_". "No hay conexion")}
So, i have this script in a scheduler, every 5 seconds for testing, but i dont know why, this script stop writing in the file or it rewrites the file, erasing all before lines wrote, after X seconds.. :S
Maybe the problem is on the else, but i dont find it..

UPDATE: The script usually stops when the file (miconexion.rsc) reached the +-4000B

Re: Mikrotik script how to line break

Posted: Wed Nov 27, 2013 1:20 pm
by boen_robot
Yeah, sadly it's a known issue with RouterOS... The upper limit is really 4096B (or 4KiB).

The only way to keep a larger file is to simply not use scripting to write it and/or store it. Like, you could perhaps use "/tool fetch" to send the results to an FTP or HTTP server, where the contents would be written on a file there.

"But what about when there's no connection to the server?" I hear you asking. Well, you could keep a file on the router up until the server is reachable, and as soon as the connection is back on, send the file's contents, and remove it.

Re: Mikrotik script how to line break

Posted: Fri Mar 12, 2021 2:19 pm
by vklpt
For Python: try to replace '\n' with '\r\n'