Page 1 of 1
send script output to a file
Posted: Mon Feb 05, 2018 10:55 am
by dana1975
i want to send the out script to a file, i set the variable $contents and i need to send this variable to a text file
:local updown
:local up
:local down
:local totaldown
:local name
:local contents
:foreach counter in=[/queue simple find ] do={
:set $updown [/queue simple get $counter bytes]
:set $up [:pick $updown 0 ([:find $updown "/"])]
:set $down [:pick $updown ([:find $updown "/"]+1) 255]
:set $totaldown ($up+$down)
:set $name [/queue simple get $counter name]
#:put ("Name", $name, "Upload/Downoad" , $updown , " Upload:" , $up " Download:" , $down" Total Download:" , $totaldown)
:set $contents ("Name", $name, "Upload/Downoad" , $updown , " Upload:" , $up " Download:" , $down" Total Download:" , $totaldown)
}}
Re: send script output to a file
Posted: Thu Mar 01, 2018 8:37 am
by dana1975
any answer !!
Re: send script output to a file
Posted: Thu Mar 01, 2018 10:08 pm
by sebastia
Hey
(that's for whole script :execute script="..." file=<file>)
print ... file=<file>
https://wiki.mikrotik.com/wiki/Manual:S ... parameters
Re: send script output to a file
Posted: Fri Mar 02, 2018 8:51 am
by dana1975
i used this command
/file set filename contents=$contents
but it was not work. please correct my script if you can
Re: send script output to a file
Posted: Fri Mar 02, 2018 11:23 pm
by sebastia
but it was not work. please correct my script if you can
Is that a challenge?
BTW, have you tried the suggestion?
Re: send script output to a file
Posted: Sat Mar 03, 2018 6:30 am
by dana1975
the print file=filename dosnt work for me
Re: send script output to a file
Posted: Sun Mar 04, 2018 9:28 am
by dana1975
print file=filename -> export the script content only, i need print the script result
/system script run scriptname -> it can not export the result to a file
has been solved with this command
{:local a [/system script get script1 source]; execute script=$a file=$fname1}
Re: send script output to a file
Posted: Sun Apr 12, 2020 10:51 am
by bluecrow76
has been solved with this command
{:local a [/system script get script1 source]; execute script=$a file=$fname1}
Fantastic solution!!!
This came to my rescue this evening. I wrote a script to print out the netwatch statuses in CSV format, but I wanted to save the output it to a file for easy download or email. To do it takes two scripts, as shown below. The first is the script that does the work. The second runs the first script and saves the output. Tested working on v6.46.4.
Script: netwatches-to-csv
# MSHARP 20200412
# Print netwatch statuses in CSV format. Allows quick import into Excel
{
# print header
:local message "\"host\",\"status\",\"since\",\"disabled\",\"comment\""
:put $message
# print each netwatch
:foreach n in=[/tool netwatch find] do={
:local comment [/tool netwatch get $n comment]
:local host [/tool netwatch get $n host]
:local status [/tool netwatch get $n status]
:local since [/tool netwatch get $n since]
:local disabled [/tool netwatch get $n disabled]
:local message "\"$host\",\"$status\",\"$since\",\"$disabled\",\"$comment\""
:put $message
}
}
Script: netwatches-to-csv_save-output-to-txt-file
# MSHARP 20200412
# Save the output of the netwatches-to-csv script to a txt file
# Solution found in the link below:
# https://forum.mikrotik.com/viewtopic.php?t=130448#p645867
{
:local a [/system script get netwatches-to-csv source]
:local outFile "netwatches-to-csv.csv"
execute script=$a file=$outFile
}
Re: send script output to a file
Posted: Wed Sep 23, 2020 2:19 am
by brg3466
@bluecrow76 May I know what is your RouterOS version, It seems " execute script=$a file=$filename" only works in early version such as 6.45.9, it doesn't work on 6.47.3.
Re: send script output to a file
Posted: Thu Sep 24, 2020 4:13 am
by bluecrow76
@bluecrow76 May I know what is your RouterOS version, It seems " execute script=$a file=$filename" only works in early version such as 6.45.9, it doesn't work on 6.47.3.
I don't recall the exact version I was running when I posted this back in April, but I would have been running whatever was stable at the time.
To test, I just ran the code exactly as listed in my previous post on a 6.47.4 CHR and it worked as expected. My only advise would be to make sure that you're filenames don't have any spaces in them if you're not explicitly putting the variables inside of quotes. Below is a "/system scripts export" of the script as I ran it on the CHR.
# sep/24/2020 01:07:15 by RouterOS 6.47.4
# software id =
#
#
#
/system script
add dont-require-permissions=no name=netwatches-to-csv policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source="#\
\_MSHARP 20200412\r\
\n# Print netwatch statuses in CSV format. Allows quick import into Excel\
\r\
\n{\r\
\n# print header\r\
\n:local message \"\\\"host\\\",\\\"status\\\",\\\"since\\\",\\\"disabled\
\\\",\\\"comment\\\"\"\r\
\n:put \$message\r\
\n# print each netwatch\r\
\n:foreach n in=[/tool netwatch find] do={\r\
\n\t:local comment [/tool netwatch get \$n comment]\r\
\n\t:local host [/tool netwatch get \$n host]\r\
\n\t:local status [/tool netwatch get \$n status]\r\
\n\t:local since [/tool netwatch get \$n since]\r\
\n\t:local disabled [/tool netwatch get \$n disabled]\r\
\n\t:local message \"\\\"\$host\\\",\\\"\$status\\\",\\\"\$since\\\",\\\"\
\$disabled\\\",\\\"\$comment\\\"\"\r\
\n\t:put \$message\r\
\n}\r\
\n}"
add dont-require-permissions=no name=\
netwatches-to-csv_save-output-to-txt-file policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source="#\
\_MSHARP 20200412\r\
\n# Save the output of the netwatches-to-csv script to a txt file\r\
\n# Solution found in the link below:\r\
\n# https://forum.mikrotik.com/viewtopic.php\?t=130448#p645867\r\
\n{\r\
\n:local a [/system script get netwatches-to-csv source]\r\
\n:local outFile \"netwatches-to-csv.csv\"\r\
\nexecute script=\$a file=\$outFile\r\
\n}"
Re: send script output to a file
Posted: Thu Sep 24, 2020 9:27 pm
by SiB
RouterOS 7.1beta2
execute "/interface lte cell-monitor lte1 max-age=1m duration=1m" file="stdout.txt"
Re: send script output to a file
Posted: Fri Sep 25, 2020 7:14 pm
by brg3466
@bluecrow76 @sib, thank you for your reply ! Finally figured out why it failed before. :-)
In "execute script=$a .... " the $a must be an expression instead of a script name.
Re: send script output to a file
Posted: Fri Sep 25, 2020 7:42 pm
by SiB
I do testing at global to see results in WinBox, to confirm what exactly will be done with $a
global a [/interface lte cell-monitor lte1 max-age=1m duration=10s] <- run command now in CLI, $a saved as empty
global a {/interface lte cell-monitor lte1 max-age=1m duration=10s} <- this create $a = false
global a [ execute {/interface lte cell-monitor lte1 max-age=1m duration=10s } ] <- this create $a widh hex ID in value
global a do={/interface lte cell-monitor lte1 max-age=1m duration=10s } <- this create Function with bad syntax msg in final.txt, works only in CLI as: $a
global a "/interface lte cell-monitor lte1 max-age=1m duration=10s" <- this save raw version in variable who works in background of executive
# final test by
execute $a file="stdout.txt"
Verification was in System > Script > Environment
Re: send script output to a file
Posted: Sat Sep 26, 2020 2:55 am
by msatter
Many many many thanks! I was looking for a way to write LARGE files for a long long time. This also works in the 6.4X version of ROS.
You can test your code easier in the Terminal and here I save a very lean import file for an address list:
Script - function - comment is in the top of the file and I think even define a specific bottom line and I will make a complete one later. $addAddress is the function that writes the address in the address-list on import.
Update:
Got it working and it was bit think how to put code as just text and not be syntax checked. Very nice and I am pleased that finally I can create back-up/source files also to be used on other routers without having to go through the work in a export file. Now I have to think how to rename the created file from .rsc.txt to .rsc on the disk. I will create a new topic in scripting soon to have the beginning of a ecosystem to back-up/distribute address-list in a lean and efficient way.