wrong ssh-exec output

Hello.

my ssh-exec command returns as string 1 but witch length of 3, why?
i cant easily convert to num und all if conditions fail.

anyone an idea, howto fix my issue or is this mayby a bug?

my goal is compare in the if condition or in some loops for future.

only the 2nd if is working probably, but thats not ideal soluten, just a workaround.

here my little diagnostic:

:set $out [:put ([/system/ssh-exec address=$remote user=$user command=("/ip/dhcp-server/lease/print count-only where mac-address=CE:D7:2C:8A:02:DB") as-value]->"output")]

:if ($out=1) do={:put "yes" } else={:put "no"}

:if ((:pick $out 0 1)=1) do={:put "num yes" } else={:put "num no"}
:if ((:pick $out 0 1)="1") do={:put "str yes" } else={:put "str no"}
:if ((:pick $out 1 2)=" ") do={:put "str space yes" } else={:put "str space no"}
:if ((:pick $out 1 2)="") do={:put "str yes" } else={:put "str no"}

:put "type: $[:typeof $out]"
:put "len: $[:len $out]"

:put "len: $[:len $out]"
:local var [:tonum $out]
:put "type: $[:typeof $var]"
:put "len: $[:len $var]"

:put "pick1: $[:pick $out 0 1]"
:put "pick2: $[:pick $out 0 2]"
:put [:typeof [:pick $out 1 2]]
:put "pick3: $[:pick $out 2 3]"
:put "pick4: $[:pick $out 3 4]"
:put "pick5: $[:pick $out 3 5]"

It’s likely caused by newline or whitespace characters in the output returned by ssh-exec. Even if the visible result is ‘1’, the actual string might include hidden characters like ‘\r’ or ‘\n’, which would explain the length of 3. The built-in :tonum doesn’t handle end-of-line characters well.

You can check what’s really in the string using:

# check what the string contains
put [:convert to=url $myStr ]

To clean the output, try:

:local returnstr ([/system/ssh-exec address=$remote user=$user command="..."]->"output")
:local returnstr [:pick $returnstr 0 [:len $returnstr] - 1]  # removes trailing newline or whatever
:set $out [:tonum $returnstr]

Or this, if there’s a carriage return:

# Remove trailing carriage return
:set $out [:tonum [:pick $returnstr 0 [:find $returnstr "\r" ]]]

Or more robustly:

# Remove both trailing newline and carriage return characters
:while ([:pick $returnstr ([:len $returnstr] - 1)] = "\r" or [:pick $returnstr ([:len $returnstr] - 1)] = "\n") do={
    :set returnstr [:pick $returnstr 0 ([:len $returnstr] - 1)]
}
:set $out [:tonum $returnstr]

Alternatively, you could probably use a regex to extract the expected format directly from the output. In general, these quirks are pretty common with ssh-exec output and stripping invisible characters like ‘\r’ and ‘\n’ usually fixes it.

@larsa → thx, code below was the solution

# Remove trailing carriage return
:set $out [:tonum [:pick $out 0 [:find $out"\r" ]]]