[SOLVED] Exec script - with arguments, not working

On OH2.4 (latest as of today) and am setting up an exec rule that connects to my Fortigate firewall and disable or enables a specific wireless interface (an SSID that is reserved for my kids to use that has policy filtering on it etc)

The thing, items and rule are setup identical to the demo setup as per here https://www.openhab.org/addons/bindings/exec/

The script is quite simple, it takes an argument of either ‘up’ or ‘down’ which quite simply is used to set the wireless interface on the firewall either up or down.

echo config system interface > fgtscript
echo edit kidswifi >> fgtscript
echo set status $1 >> fgtscript
echo end >> fgtscript
echo exit >> fgtscript
sshpass -p thepassword ssh -T piadmin@ < fgtscript
echo "Wifi Interface $1"
rm fgtscript

I put the last echo command in to output the state of what the script is doing (it will say either “Wifi interface up” or “Wifi interface down”) which should is returned to the sKidsWifi_Out String item so I can confirm the script is actually running, with the rule outputting this ‘Result’ to the log file.

The items file:

Switch          swKidsWifiOnOff					"Kids Wifi OFF"                       <network>
Switch          swKidsWifi_Run                                                                                                                   { channel="exec:command:0e5ea31f:run" }
String          sKidsWifi_Args		     	                                                                                                     { channel="exec:command:0e5ea31f:input" }
String          sKidsWifi_Out                                                                                                                    { channel="exec:command:0e5ea31f:output" }

The rules file:

// Kids Wifi on or off
rule "Kids wifi up or down"
	Item swKidsWifiOnOff changed
        // Turn switch ON to disable, turn off to enable
        if (swKidsWifiOnOff.state == ON)
		logInfo("KidsWifi", "Bringing KidsWifi interface down")
		logInfo("KidsWifi", "Bringing KidsWifi interface up")

     	// Trigger execution
      	// wait for the command to complete
      	// State will be NULL if not used before or ON while command is executed
      	while (swKidsWifi_Run.state != OFF)

      	// Logging of command line result
	logInfo("KidsWifi", "Result:" + sKidsWifi_Out.state )

(The first thing to note, is I believe the exec demo rule on the Exec binding page is wrong - it shows the …_Run.sendCommand(ON) part AFTER the while / thread sleep code, this does not seem logical to me, surely enabling the ‘run’ command to kick the script off and THEN having the wait part to check that the script is finished is how it should be configured?)

The Exec ‘Thing’ calls the command:

/etc/openhab2/scripts/kidswifi.sh %2$s

(with Interval 0, REGEX((.*)) transform and autorun disabled)

The weird thing is when I click to turn the switch on or off in OH GUI, the log file shows the following:

2018-11-02 09:35:07.400 [INFO ] [ipse.smarthome.model.script.KidsWifi] - Bringing KidsWifi interface down
2018-11-02 09:35:07.423 [INFO ] [ipse.smarthome.model.script.KidsWifi] - Result: Wifi Interface up

First line shows the log message from the rules file that’s telling me that I’m turning the switch ON (to bring the wireless interface DOWN) the next line is returning the Output state that I’ve echo’d from the script file - but it is showing the interface as UP???!!! Because it simply echo’s the argument that is passed from the Exec binding to the script - this should match. If I turn the switch OFF, then the opposite is logged!!! But, it’s the same argument passed to the script file - so they both should match.

2018-11-02 09:38:24.450 [INFO ] [ipse.smarthome.model.script.KidsWifi] - Bringing KidsWifi interface up
2018-11-02 09:38:24.490 [INFO ] [ipse.smarthome.model.script.KidsWifi] - Result: Wifi Interface down

What is going on here? Surely it’s something simple I’m doing wrong?

Last issue - if I manually run the script from the cmd line as user ‘pi’ it works fine (wifi interface is brought up or down) If I run from the GUI it doesn’t work (assume it runs under the ‘openhab’ user context) I believe this is because of SSH keys and it’s prompting to accept the key. It doesn’t actually run successfully so I need to either somehow logon as openhab user and run the command once and accept the key, or get the key pair between the firewall and Rpi setup. Unsure how?!


There’ll be a finite time between telling exec to run, and the run state going to ON. i.e. if you immediately test for OFF it probably will be, and you then get the results of the previous run.

Using sleep is generally viewed as a bit of a bodge in rules, but you could add another before the test.

I’d try to break into two rules - one to kick off the exec, another listening for the output state to have changed.

So the SSH keys were easily ignored by simply adding “-o StrictHostKeyChecking=no” to the ssh command, so now looks like:

sshpass -p thepassword ssh -T -o StrictHostKeyChecking=no piadmin@ < fgtscript

A couple second ‘thread::sleep’ as suggested sorted the logging out too… thanks!


I do intermittently get the following as well, when turning on or off - but only remotely via MyOpenhab:

[WARN] [io.openhabcloud.internal.CloudClient] - Jetty request 3298329 failed: null
[WARN] [io.openhabcloud.internal.CloudClient] - Response Failure: null