Community discussions

MikroTik App
 
kispalsz
just joined
Topic Author
Posts: 5
Joined: Sun Jul 30, 2023 9:41 pm

Simple querys output to variable

Sun Aug 06, 2023 10:14 pm

Hello!

I want to store the output of 2 simple queries in a variable.
I will concatenate these variables later and send them to myself by email.

One is the serial port detailed information.
/port print detail
Flags: I - inactive
0 name="serial0" used-by="Serial Console" device="" channels=1 baud-rate=auto
data-bits=8 parity=none stop-bits=1 flow-control=none
The other is the users in the system.
/user print detail
Flags: E - expired, X - disabled
0 ;;; system default user
name="admin" group=full address="" last-logged-in=aug/06/2023 18:44:19

1 X name="teszt" group=read address=""
How can I request these from the system in an rsc script and store them in 1-1 variables. If possible, so that you don't have to write anything to a file.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 4505
Joined: Sun May 01, 2016 7:12 pm
Location: California
Contact:

Re: Simple querys output to variable

Mon Aug 07, 2023 1:15 am

The "as-value" is offered on the print command, what this does is "returns" the resulting print as an array (vs. outputting to screen/"stdout" as what happens without the "as-value").

So at a basic level, it's just:
:global portprint [/port print detail as-value]
:global userprint [/user print detail as-value]
:put " Ports:\n $portprint \n\n Users:\n $userprint "

However, this will not have the same formatting as the screen since the "as-value" convert it to an array type & if used in email directly might be ugly since the array is converted to a string. e.g.
.id=*1;baud-rate=115200;channels=1;data-bits=8;device=;flow-control=none;name=serial0;parity=none;stop-bits=1;used-by=remote-access 

So if your going to concat things into an /tool email send body=... — you can directly use a :foreach to build your own output text using "find" to get all ports, net "get" on any variables you want to use in the email body:
{
   :local porttext; 
   /port
   :foreach p in=[find] do={ 
            :set porttext "$porttext $[get $p name] $[get $p data-bits]-$[:pick [get $p parity] 0 1]-$[get $p stop-bits] $[get $p baud-rate] (use: $[get $p used-by])\r\n" 
   }
   :put "$porttext"
}
serial0 8-n-1 115200 (use: remote-access)
serial1 8-n-1 115200 (use: )