Error occurred when executing commandLine

I try to on/off NAT rules in Mikrotik router via ssh:

executeCommandLine('ssh xx@ -p 22 ip fi nat enable numbers='  + ruleN )


2021-05-03 22:16:29.720 [WARN ] [] - Error occurred when executing commandLine '[ssh xx@ -p 22 ip fi nat disable numbers=1]' Cannot run program "ssh ag@ -p 22 ip fi nat disable numbers=1": error=2, There is no such file or directory
	at java.lang.ProcessBuilder.start( ~[?:?]
	at java.lang.ProcessBuilder.start( ~[?:?]

What wrong?

OH 3.1.0M4

Syntax. In OH3, spaces have to be represented with commas.


No one word about commas there…

Quote from the docs:

Scripts with parameters

Let’s assume that your path/to/my/ needs two item states to process them with some calculation. In console you would call it like

path/to/my/ itemState1 itemState2

To solve this constellation within a rule you have to add one argument per script parameter to the function. The script above would be configured like shown below.

// When you are not interested in the script output
executeCommandLine("path/to/my/", itemState1, itemState2);

// When you need the output in your further rule processing
var ScriptResponse = executeCommandLine(Duration.ofSeconds(60), "path/to/my/", itemState1, itemState2);

// Space separated constants must be given as separate parameters as well
// e.g. path/to/your/ xyz fred.file
var ScriptResponse = executeCommandLine(Duration.ofSeconds(60), "path/to/your/", "xyz", "fred.file");

If you have any ideas to make that more clear we would love to see a PR. I’d fix it but it seems very clear to me. If the script I’m calling has arguments, I need to pass each argument separately in the call to executeCommadnLine.

Ohh, i see :grinning:, please add this for stupid users like me :joy:

Instead of

do executeCommandLine(('ssh xx@ -p 22 ip fi nat enable numbers=' + ruleN).split(" "))

This achieves the same thing without making the command harder to read and edit.

Thank you, this is much clearer :+1:

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.