Simple querys output to variable

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.

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