2023-03-07 10:57:42 +01:00
|
|
|
#!rsc by RouterOS
|
|
|
|
# RouterOS script: mod/inspectvar
|
2025-01-02 00:04:06 +01:00
|
|
|
# Copyright (c) 2020-2025 Christian Hesse <mail@eworm.de>
|
2025-01-24 20:46:12 +01:00
|
|
|
# https://rsc.eworm.de/COPYING.md
|
2023-04-04 19:22:13 +02:00
|
|
|
#
|
2025-02-07 17:39:48 +01:00
|
|
|
# requires RouterOS, version=7.15
|
2024-04-04 21:45:02 +02:00
|
|
|
#
|
2023-04-04 19:22:13 +02:00
|
|
|
# inspect variables
|
2025-01-24 20:46:12 +01:00
|
|
|
# https://rsc.eworm.de/doc/mod/inspectvar.md
|
2023-03-07 10:57:42 +01:00
|
|
|
|
|
|
|
:global InspectVar;
|
|
|
|
:global InspectVarReturn;
|
|
|
|
|
|
|
|
# inspect variable and print on terminal
|
2024-12-09 09:10:08 +01:00
|
|
|
:set InspectVar do={ :do {
|
2023-03-07 10:57:42 +01:00
|
|
|
:global InspectVarReturn;
|
|
|
|
|
2024-07-11 23:12:50 +02:00
|
|
|
:put [ :tocrlf [ $InspectVarReturn $1 ] ];
|
2024-12-09 09:10:08 +01:00
|
|
|
} on-error={
|
|
|
|
:global ExitError; $ExitError false $0;
|
|
|
|
} }
|
2023-03-07 10:57:42 +01:00
|
|
|
|
|
|
|
# inspect variable and return formatted string
|
|
|
|
:set InspectVarReturn do={
|
|
|
|
:local Input $1;
|
|
|
|
:local Level (0 + [ :tonum $2 ]);
|
|
|
|
|
|
|
|
:global IfThenElse;
|
|
|
|
:global InspectVarReturn;
|
|
|
|
|
|
|
|
:local IndentReturn do={
|
|
|
|
:local Prefix [ :tostr $1 ];
|
|
|
|
:local Value [ :tostr $2 ];
|
|
|
|
:local Level [ :tonum $3 ];
|
|
|
|
|
|
|
|
:local Indent "";
|
|
|
|
:for I from=1 to=$Level step=1 do={
|
|
|
|
:set Indent ($Indent . " ");
|
|
|
|
}
|
|
|
|
:return ($Indent . "-" . $Prefix . "-> " . $Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
:local TypeOf [ :typeof $Input ];
|
|
|
|
:local Return [ $IndentReturn "type" $TypeOf $Level ];
|
2024-03-04 20:40:35 +01:00
|
|
|
|
2023-03-07 10:57:42 +01:00
|
|
|
:if ($TypeOf = "array") do={
|
|
|
|
:foreach Key,Value in=$Input do={
|
|
|
|
:set $Return ($Return . "\n" . \
|
|
|
|
[ $IndentReturn "key" $Key ($Level + 1) ] . "\n" . \
|
|
|
|
[ $InspectVarReturn $Value ($Level + 2) ]);
|
|
|
|
}
|
|
|
|
} else={
|
|
|
|
:if ($TypeOf != "nothing") do={
|
|
|
|
:set $Return ($Return . "\n" . \
|
|
|
|
[ $IndentReturn "value" [ $IfThenElse ([ :len $Input ] > 80) \
|
|
|
|
([ :pick $Input 0 77 ] . "...") $Input ] $Level ]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
:return $Return;
|
|
|
|
}
|