aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Christian Hesse <mail@eworm.de>2021-09-20 13:19:34 +0200
committerGravatar Christian Hesse <mail@eworm.de>2021-09-21 21:26:13 +0200
commit9295d06fe2fce058b6b21d69ea6eac8282d23337 (patch)
tree91d002607eb86341a7da6f8cd8021fd099c976fc
parentce78d7d9e9ad1c2ab0e5ffab716b40975c6bd0eb (diff)
global-functions.d/inspectvar: add $InspectVar...change-64
to inspect variables. This is useful for variables with (nested) arrays, for example inspecting $NetwatchNotify (from netwatch-notify): [admin@Mikrotik] > $InspectVar $NetwatchNotify -type-> array -key-> quad-one -type-> array -key-> count -type-> num -value-> 0 -key-> notified -type-> bool -value-> false -key-> parent -type-> nothing -key-> resolve-failed -type-> nothing -key-> since -type-> nothing -key-> example.com -type-> array -key-> count -type-> num -value-> 0 -key-> notified -type-> bool -value-> false -key-> parent -type-> str -value-> quad-one -key-> resolve-failed -type-> nothing -key-> since -type-> nothing
-rw-r--r--global-config2
-rw-r--r--global-config-overlay2
-rw-r--r--global-config.changes1
-rw-r--r--global-functions2
-rw-r--r--global-functions.d/inspectvar40
5 files changed, 44 insertions, 3 deletions
diff --git a/global-config b/global-config
index cc0e389..bf3b0e4 100644
--- a/global-config
+++ b/global-config
@@ -8,7 +8,7 @@
# Make sure all configuration properties are up to date and this
# value is in sync with value in script 'global-functions'!
-:global GlobalConfigVersion 63;
+:global GlobalConfigVersion 64;
# This is used for DNS and backup file.
:global Domain "example.com";
diff --git a/global-config-overlay b/global-config-overlay
index 854d4f8..78d34b0 100644
--- a/global-config-overlay
+++ b/global-config-overlay
@@ -8,7 +8,7 @@
# Make sure all configuration properties are up to date and this
# value is in sync with value in script 'global-functions'!
# Comment or remove to disable news and change notifications.
-:global GlobalConfigVersion 63;
+:global GlobalConfigVersion 64;
# Copy configuration from global-config here and modify it.
diff --git a/global-config.changes b/global-config.changes
index 7504693..1ef2792 100644
--- a/global-config.changes
+++ b/global-config.changes
@@ -67,6 +67,7 @@
61="Finally removed old scripts.";
62="Added '\$ScriptRunOnce' to run a script from URL once without installation, intended to aid configuration management and the like.";
63="Moved optional functions '\$IPCalc' and '\$ScriptRunOnce' to modules.";
+ 64="Implemented '\$InspectVar' in module to inspect variables.";
};
# Migration steps to be applied on script updates
diff --git a/global-functions b/global-functions
index 34be4fd..4443431 100644
--- a/global-functions
+++ b/global-functions
@@ -8,7 +8,7 @@
# https://git.eworm.de/cgit/routeros-scripts/about/
# expected configuration version
-:global ExpectedConfigVersion 63;
+:global ExpectedConfigVersion 64;
# global variables not to be changed by user
:global GlobalFunctionsReady false;
diff --git a/global-functions.d/inspectvar b/global-functions.d/inspectvar
new file mode 100644
index 0000000..15da04a
--- /dev/null
+++ b/global-functions.d/inspectvar
@@ -0,0 +1,40 @@
+#!rsc by RouterOS
+# RouterOS script: global-functions.d/inspectvar
+# Copyright (c) 2020-2021 Christian Hesse <mail@eworm.de>
+# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
+
+:global InspectVar;
+
+# inspect variable
+:set InspectVar do={
+ :local Input $1;
+ :local Level (0 + [ :tonum $2 ]);
+
+ :global InspectVar;
+
+ :local PutIndent 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 . " ");
+ }
+ :put ($Indent . "-" . $Prefix . "-> " . $Value);
+ }
+
+ :local TypeOf [ :typeof $Input ];
+ $PutIndent "type" $TypeOf $Level;
+
+ :if ($TypeOf = "array") do={
+ :foreach Key,Value in=$Input do={
+ $PutIndent "key" $Key ($Level + 1);
+ $InspectVar $Value ($Level + 2);
+ }
+ } else={
+ :if ($TypeOf != "nothing") do={
+ $PutIndent "value" $Input $Level;
+ }
+ }
+}