Community discussions

MikroTik App
 
millenium7
Long time Member
Long time Member
Topic Author
Posts: 616
Joined: Wed Mar 16, 2016 6:12 am

:execute output to console? Or any other method?

Tue Jan 07, 2025 5:10 am

I'm trying to work around a problem and haven't yet been able to find a solution
The problem: running either '/export terse' on RouterOSv6 or '/export terse show-sensitive' on RouterOSv7 in order to output the configuration for backup purposes

That's it, simple right? Maybe, but I can't figure it out

Detecting which command based on version is doable. I've already posted on these forums on how to detect and run version specific commands without failure, i.e. being able to paste in a code snippet that would update wifi details regardless of whether its v6, v7, legacy or wave2 drivers.
The following code snippet will do it via the execute command. Unfortunately :execute does not provide any output to the console
# Get RouterOS Version and extract major and minor versions
:global RouterOS [:pick [/system resource get version] 0 [:find [/system resource get version] " "]]
:global RouterOSMajor [:pick $RouterOS 0]
{
:local o
:local a ([:find $RouterOS "."]+1)
:local b ([:find $RouterOS "." $a])
:if ($b > 0) do={:set $o [:pick $RouterOS 0 $b]} else={:set $o $RouterOS}
:global RouterOSMinor [:pick $o 2 [:len $o]]
}

### Version Specific Commands
:if ($RouterOSMajor > 6) do={
:global C "/export terse show-sensitive ; /log warning \"Running RouterOSv7 or above\""
} else={
:global C "/export terse ; /log warning \"Running RouterOSv6 or below\""
}
execute $C ; /system script environment remove [find where name=C]
i've tried outputting the execute results to a file which does work, i.e.
:execute $C file=ConfigOutput.txt
And then with v7 I can use the following to read it to the console
:put [/file get ConfigOutput.txt contents] ; /file remove ConfigOutput.txt
Great!..... but it doesn't work on V6 it just returns an empty string...........??????

So i'm stumped, I can't think of any other way to handle this without errors. At best this could be used to FTP a file to an external location but that's not what I want. I just need the config spat out in the terminal to be seamlessly integrated with our config change software
 
optio
Forum Guru
Forum Guru
Posts: 1078
Joined: Mon Dec 26, 2022 2:57 pm

Re: :execute output to console? Or any other method?

Wed Jan 08, 2025 3:46 pm

Put file=ConfigOutput.txt argument for export command in C variable instead for :execute.

Idk why output to file for :execute on v6 is not working (not having v6 for checking it), but since you need only config export then it is more appropriate to use file argument for export command than for :execute.
Did you check if output to file on v6 is working with something simple like
:execute ":put test1; :put test2; :error err" file=output.txt
Last edited by optio on Wed Jan 08, 2025 4:03 pm, edited 1 time in total.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4697
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: :execute output to console? Or any other method?

Wed Jan 08, 2025 3:59 pm

I just need the config spat out in the terminal to be seamlessly integrated with our config change software
It's this I'm confused by: sending to terminal is different from automation. If your config software can use SSH to issue the "export" command, that will spit out to "terminal", which presumable be captured by config change software.

Since I do not know what your trying to accomplish.., but a alternative to :execute is using :parse and another [] around it to execute it:
[[:parse ":export"]]
 
millenium7
Long time Member
Long time Member
Topic Author
Posts: 616
Joined: Wed Mar 16, 2016 6:12 am

Re: :execute output to console? Or any other method?

Wed Jan 08, 2025 4:03 pm

I just need the config spat out in the terminal to be seamlessly integrated with our config change software
It's this I'm confused by: sending to terminal is different from automation. If your config software can use SSH to issue the "export" command, that will spit out to "terminal", which presumable be captured by config change software.
The software does not know which version of ROS the device is. It knows its a MikroTik but thats it, so it doesn't know if it should run 'export terse' or 'export terse show-sensitive', the former for v6 and the latter for v7
I can specify which command to run manually, which becomes a massive administrative headache. Need something automated to run the appropriate command
Idk why output to file for :execute on v6 is not working (not having v6 for checking it), but since you need only config export then it is more appropriate to use file argument for export command than for :execute.
I figured out why, ROS v6 will only read files up to 4kb in size, any larger than that they just return blank

I'll try both suggestions tomorrow to see if i can make them work
 
optio
Forum Guru
Forum Guru
Posts: 1078
Joined: Mon Dec 26, 2022 2:57 pm

Re: :execute output to console? Or any other method?

Wed Jan 08, 2025 4:13 pm

I figured out why, ROS v6 will only read files up to 4kb in size, any larger than that they just return blank
If such is the case you will still have issue printing it after is generated from export command. Use :parse command as @Amm0 suggested.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12980
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: :execute output to console? Or any other method?

Wed Jan 08, 2025 5:23 pm

Detecting which command based on version is doable. I've already posted on these forums on how to detect and run version specific commands without failure,
Already done that before, but I can also make this for check what commands are supported and what parameters
viewtopic.php?p=1047407#p1047229

example code

[rex@MATRIX] > :put [$cmdtreeVar export]   
export
(a) compact
(a) file
(a) hide-sensitive
(a) show-sensitive
(a) terse
(a) verbose
(a) where
So, with /console/inspect on v7 you can check supported commands, put on one string the correct parameters and run it with :parse for still on same session.

On latest version of v7 both show-sensitive and hide-sensitive are supported, so if is run v6-like command, is accepted.
 
millenium7
Long time Member
Long time Member
Topic Author
Posts: 616
Joined: Wed Mar 16, 2016 6:12 am

Re: :execute output to console? Or any other method?

Thu Jan 09, 2025 2:23 am


Since I do not know what your trying to accomplish.., but a alternative to :execute is using :parse and another [] around it to execute it:
[[:parse ":export"]]
Perfect, didn't know you could double wrap it. This is the answer and works perfectly

So here is the version agnostic command for anyone else who is interested
:if ([:tonum [:pick [/system package get 0 version] 0]] >= 7 ) do={[[:parse "/export terse show-sensitive"]]} else={[[:parse "/export terse"]]}

On latest version of v7 both show-sensitive and hide-sensitive are supported, so if is run v6-like command, is accepted.
The problem is I need to be able to run just a single command and have it spit out output, since the software will read what is immediately after the command entered and store that as the config, can't run 2 commands or detect errors. And ROS6 is 'not' compatible with the show-sensitive suffix it couldn't be used
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12980
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: :execute output to console? Or any other method?

Thu Jan 09, 2025 10:29 am

So here is the version agnostic command for anyone else who is interested

wrong code

[...] /system package get 0 version [...]
No this is not agnostic, this is wrong.


2022-02-26 viewtopic.php?p=915725#p915725

adapted extract of code

/system resource
:if ([get version]~"^7") do={
	[:parse "/export terse show-sensitive"]
} else={
	/export terse
}

one line code

:local c "/export terse"; /sys resource;:if ([get version]~"^7") do={:set c "$c show-sensitive"};[:parse $c]

When something needs to be run on both versions, don't use syntax compatible only with v7 ;)
:local Version [/system/resource get version]
->
:local Version [/system resource get version]
(fixed missing /system on 2nd time on original post)
 
millenium7
Long time Member
Long time Member
Topic Author
Posts: 616
Joined: Wed Mar 16, 2016 6:12 am

Re: :execute output to console? Or any other method?

Thu Jan 09, 2025 11:15 am

We've been over this before, no its not wrong. It's valid syntax for both v6 and v7

Getting the system package at position 0 is a completely legitimate way, feel free to explain to me why it won't work and if you can come up with a valid real reason i'm happy to change it. The reality is there will 'always' be a package installed (otherwise ROS itself isn't installed) and the installed packages are always the same version

Your method is more elegant but mine is not 'wrong'
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12980
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: :execute output to console? Or any other method?

Thu Jan 09, 2025 11:21 am

This is not the specific case, which may or may not work,
and it's not even a question of code beauty or not (but why did you change it from the first post?)
but you absolutely must not use hardcoded numbers or .id to obtain values.
There are dozens and dozens of posts where users, copyng these errors,
make errors in the script and does not work...
 
millenium7
Long time Member
Long time Member
Topic Author
Posts: 616
Joined: Wed Mar 16, 2016 6:12 am

Re: :execute output to console? Or any other method?

Thu Jan 09, 2025 11:25 am

Feel free to prove me wrong (and i'm more than happy to accept it) but there is absolutely no situation where [/system resource get 0 version] will ever fail or not return the expected result

If this were a different case where I was i.e. trying to get position 2 or return some arbitrary value, then sure I agree with you it's a bad coding practice and it is not something I do. But this specific example it's just being pedantic, it will always succeed and always return exactly the desired result on any ROS version. If you can prove to me otherwise i'm happy to eat my own words and give you props for it
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12980
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: :execute output to console? Or any other method?

Thu Jan 09, 2025 11:31 am

With you what I write are empty words.
I have already made it clear that it is not a competition of style, or of right and wrong,
but of not leaving wrong examples on the forum.
Of course it works if a package is installed,
it is also easy to generate a problem, mixing packages of different versions,
but given the uselessness of those who do not want to understand, I do not waste time.
 
millenium7
Long time Member
Long time Member
Topic Author
Posts: 616
Joined: Wed Mar 16, 2016 6:12 am

Re: :execute output to console? Or any other method?

Thu Jan 09, 2025 11:38 am

but given the uselessness of those who do not want to understand, I do not waste time.
This would be you buddy
I literally asked you to prove me wrong and i'll happily listen and accept it. You on the other hand aren't listening at all, just preaching pedantic coding
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12980
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: :execute output to console? Or any other method?

Thu Jan 09, 2025 11:41 am

I literally asked you to prove me wrong and i'll happily listen and accept it.
I've already written the reason twice,
it's you who comes out with this defiant air.
What do you expect me to write same thing "prove me wrong" and start a game of morons?

The other people on the forum will judge the situation,
I can't help it if you don't understand the things already written twice (and on other topics).

Whatever happens, this doesn't mean that if you need help, I won't give it to you.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12980
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: :execute output to console? Or any other method?

Tue Jan 21, 2025 8:15 pm

 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12980
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: :execute output to console? Or any other method?

Wed Jan 22, 2025 1:07 pm

I literally asked you to prove me wrong and i'll happily listen and accept it. You on the other hand aren't listening at all, just preaching pedantic coding
Just wait new 7.18...
On different devices and different way of update I obtain different results.
This is one hAPax² from (??? not remember)->7.16.1 -> 7.17 -> 7.18beta2

test code


Wrong way to get running RouterOS version:

[admin@test] /system/package> pri
Flags: X - DISABLED; A - AVAILABLE
Columns: NAME, VERSION, BUILD-TIME, SIZE
 #    NAME          VERSION    BUILD-TIME           SIZE     
 0 XA calea                                         20.1KiB  
 1 XA container                                     108.1KiB 
 2 XA dude                                          1224.1KiB
 3 XA extra-nic                                     2220.1KiB
 4 XA gps                                           24.1KiB  
 5 XA iot                                           548.1KiB 
 6 XA iot-bt-extra                                  148.1KiB 
 7 XA lora                                          8.1KiB   
 8 XA rose-storage                                  3136.1KiB
 9    routeros      7.18beta2  2025-01-21 09:27:58  12.2MiB  
10 XA tr069-client                                  132.1KiB 
11 XA ups                                           32.1KiB  
12 XA user-manager                                  332.1KiB 
13    wifi-qcom     7.18beta2  2025-01-21 09:27:58  10.2MiB  
14 XA wireless                                      856.1KiB 
15 XA zerotier                                      832.1KiB 
[admin@test] /system/package> :put [get 0]
.id=*3;available=true;disabled=true;name=calea;scheduled=;size=20625;version=
[admin@test] /system/package> :put [/system package get 0 version] 

[admin@test] /system/package> :put [/system package get 9 version] 
7.18beta2
[admin@test] /system/package> :put [/system package get [find where name=routeros] version] 
7.18beta2

Correct way to get running RouterOS version:

[admin@test] /system/package> :put [/system resource get version]
7.18beta2 (testing)



If I netinstall directly 7.18beta2 (without check for updates) I obtain only
Flags: X - DISABLED; A - AVAILABLE
Columns: NAME, VERSION, BUILD-TIME, SIZE
 #    NAME          VERSION    BUILD-TIME           SIZE     
 0    routeros      7.18beta2  2025-01-21 09:27:58  12.2MiB  

If I install later the wifi-com (without check for updates):
Flags: X - DISABLED; A - AVAILABLE
Columns: NAME, VERSION, BUILD-TIME, SIZE
 #    NAME          VERSION    BUILD-TIME           SIZE     
 0    routeros      7.18beta2  2025-01-21 09:27:58  12.2MiB  
 1    wifi-qcom     7.18beta2  2025-01-21 09:27:58  10.2MiB  

but if I upgrade from 7.17 to 7.18beta2:
Flags: X - DISABLED; A - AVAILABLE
Columns: NAME, VERSION, BUILD-TIME, SIZE
 #    NAME          VERSION    BUILD-TIME           SIZE     
 0    wifi-qcom     7.18beta2  2025-01-21 09:27:58  10.2MiB  
 1    routeros      7.18beta2  2025-01-21 09:27:58  12.2MiB  

after the check for updates:
Flags: X - DISABLED; A - AVAILABLE
Columns: NAME, VERSION, BUILD-TIME, SIZE
 #    NAME          VERSION    BUILD-TIME           SIZE     
 0    wifi-qcom     7.18beta2  2025-01-21 09:27:58  10.2MiB  
 1    routeros      7.18beta2  2025-01-21 09:27:58  12.2MiB  
 2 XA calea                                         20.1KiB  
 3 XA container                                     108.1KiB 
 4 XA dude                                          1224.1KiB
 5 XA extra-nic                                     2220.1KiB
 6 XA gps                                           24.1KiB  
 7 XA iot                                           548.1KiB 
 8 XA iot-bt-extra                                  148.1KiB 
 9 XA lora                                          8.1KiB   
10 XA rose-storage                                  3136.1KiB
11 XA tr069-client                                  132.1KiB 
12 XA ups                                           32.1KiB  
13 XA user-manager                                  332.1KiB 
14 XA wireless                                      856.1KiB 
15 XA zerotier                                      832.1KiB 
 
millenium7
Long time Member
Long time Member
Topic Author
Posts: 616
Joined: Wed Mar 16, 2016 6:12 am

Re: :execute output to console? Or any other method?

Wed Jan 22, 2025 1:24 pm

I almost feel like MikroTik has gone out of their way to screw it up in 7.18 just because of this thread :lol:

I bow to you master, thank you for proving me wrong. I shall update the code to use the 'correct' syntax and not reference ID numbers from now on
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12980
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: :execute output to console? Or any other method?

Wed Jan 22, 2025 1:36 pm

Whatever happens, this doesn't mean that if you need help, I won't give it to you.

Remember that no one is infallible. I often make mistakes too, especially with RouterOS that changes things with each version.....

For example... this do NOT work on 7.17+ but work on 7.16.2 and less (also on v6)... (first log is the directory, the second log is the prefix for create the file log.0.txt)
/sys log action set [find] disk-file-name="/log/log"