Newbie needs help with the Exec binding

Hi there,

I searched a lot regarding the Exec binding, but it looks like I’m not really understanding how it works.

My prerequisites: OH running on Raspberry no.1 (IP adress *.48). Another Raspberry no. 2 (IP *.45) is used to switch some LED stripes using a python script.

I setup RSA keys to ssh from RPI no.1 to RPI no.2 and checked that my command(s) “ssh pi@192.168.1.45 ‘cd rpi_ws281x/python/ ; sudo python examples/Hoch.py’” are working properly.

Now, I want just to switch this using a button in my sitemap.

Here’s my code:

.things:

  Thing exec:command:GarageDoorUPscript [command="ssh pi@192.168.1.45 'cd rpi_ws281x/python/ ; sudo python examples/Hoch.py'"]

*.items:

   Switch GarageDoorUp {channel="exec:command:GarageDoorUPscript:run"}

*.sitemap:

  Switch item= GarageDoorUp

I expected that this would run now, form the log I cannot identfy why it is not working:

2019-02-07 21:43:53.037 [hingStatusInfoChangedEvent] - ‘exec:command:GarageDoorUPscript’ changed from UNINITIALIZED to INITIALIZING
2019-02-07 21:43:53.229 [hingStatusInfoChangedEvent] - ‘exec:command:GarageDoorUPscript’ changed from INITIALIZING to ONLINE
2019-02-07 21:45:37.256 [ome.event.ItemCommandEvent] - Item ‘GarageDoorUp’ received command ON
2019-02-07 21:45:37.347 [nt.ItemStatePredictedEvent] - GarageDoorUp predicted to become ON
2019-02-07 21:45:37.734 [vent.ItemStateChangedEvent] - GarageDoorUp changed from OFF to ON
2019-02-07 21:45:38.184 [vent.ItemStateChangedEvent] - GarageDoorUp changed from ON to OFF

Does anybody has an idea why the commands are not processed?

Thanks in advance,

Check out How to solve Exec binding problems

In all likelihood you do not realize or forgot that openHAB runs as the openhab user, not pi, root, openhabian, nor any other account you need to log in as. Any permissions (e.g. sudo), ssh certs, group memberships, or any other configuration on the account needs to be done to the openhab account.

1 Like

Ok, I really was not aware about this.
I will read your article and try to make it work on my system.

Thanks,

1 Like

I added the logging rule and it seems that I don’t have the right permissions (which makes sence when the openhab user is different from the “normal” openhabian user). So I need to adopt / re-do the exchange of the RSA keys once again, but this time with the user “openhabianpi” on my RPI no.1.

Unfortunately, I am also a beginner in RPI / Linux. How can I log in as openhab user? I usually do login via putty / WinSCP using the openhabianpi user and the respective password.
If I try with user “openhab” or “openhab2” it does not work…

Any idea how I can go forward?

You cannot. It is a service user account. All services run as different users with limited capabilities as a security feature.

To execute a command as the openhab user you would use sudo -u openhab <cmd> where <cmd> is the command to execute. To get a shell running as the openhab user you would execute the command sudo -u openhab /bin/bash.

Unlike most service user accounts, the openhab user does have a home directory. It is /var/lib/openhab2. That is the folder where your .ssh folder will need to be configured.

Finally, it is worth mentioning that this is really not the best approach for integrating devices connected to a remote RPi. It would be better to run some script or service on the remote RPi and enable a REST API connection or MQTT to send and receive information. I wrote something for this purpose here.

OK, I catched your point regarding the API and it definitely makes sence. But for the moment, I just want to understand how this SSH command can work properly (I really want to understand how this thing is working and improve my knowledge about RPI/Linux/etc.).

So, I logged in as Service User openhab, again made the RSA key pairing ancd checked as openhab user, that the ssh command is working properly:

[17:44:59] openhab@openHABianPi:/home/openhabian$ ssh pi@192.168.1.45 'sudo python rpi_ws281x/python/examples/Hoch.py'

If I add this command to my rule

rule "ExceDebug"
when
 Item GarageDoorUp received update ON
then
 val results = executeCommandLine("ssh pi@192.168.1.45 'sudo python rpi_ws281x/python/examples/Hoch.py'", 5000)
  logInfo("execTest", results)
end

I still get an error that the folder was not found:

2019-02-11 17:48:45.678 [INFO ] [ipse.smarthome.model.script.execTest] - bash: sudo python rpi_ws281x/python/examples/Hoch.py: Datei oder Verzeichnis nicht gefunden

I tried to figure out, why this happens, so I exchanged the command by just “ls” and in the log, I could see the respective folder.

So I am still doing something wrong, but I don’t know what…

If google translate is correct, it can’t find the file using that relative path. Try using the full path to that file.

I already tried this before. It is not working. Neither as openhab user in putty, nor in openhab (the "ConnectionTest.py only prints “The connection is working”):

putty:

[20:08:50] openhab@openHABianPi:/home/openhabian$ ssh pi@192.168.1.45 'sudo python rpi_ws281x/python/examp                                     s/ConnectionTest.py'
The connection is working

[20:10:04] openhab@openHABianPi:/home/openhabian$ ssh pi@192.168.1.45 'sudo python home/pi/rpi_ws281x/python                                     /examples/ConnectionTest.py'
python: can't open file 'home/pi/rpi_ws281x/python/examples/ConnectionTest.py': [Errno 2] No such file or directory

Openhab log:

2019-02-11 20:13:29.939 [INFO ] [ipse.smarthome.model.script.execTest] - bash: sudo python home/pi/rpi_ws281x/python/examples/ConnectionTest.py: Datei oder Verzeichnis nicht gefunden

2019-02-11 20:13:58.217 [INFO ] [ipse.smarthome.model.script.execTest] - bash: sudo python rpi_ws281x/python/examples/ConnectionTest.py: Datei oder Verzeichnis nicht gefunden

And even not, if I direct it to the root (cd //) and then to the path:

2019-02-11 20:18:07.493 [INFO ] [ipse.smarthome.model.script.execTest] - bash: cd // ; sudo python home/pi/rpi_ws281x/python/examples/ConnectionTest.py: Datei oder Verzeichnis nicht gefunden

But, I see that the file and the path is there:

pi@magicmirrorpi://home/pi/rpi_ws281x/python $

Sorry for being soo dumb…I have no clue what I’m doing wrong.

You are missing the leading /

/home/pi/rpi_ws281x/python/examples/ConnectionTest.py

Use the full path to python as well. If you don’t know it, log into the machine and run

which python

Thanks for your fast replies and for your help…

I tried it with an without the leading /. Still failing.

I also ran which python (results in /usr/bin/python). I checked that python works on the remote RPI:

pi@magicmirrorpi:// $ usr/bin/python
Python 2.7.9 (default, Sep 17 2016, 20:26:04)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "Test"
Test

and changed the command to

"ssh pi@192.168.1.45 'cd // ; sudo usr/bin/python /home/pi/rpi_ws281x/python/examples/ConnectionTest.py'"

but it still fails, as seen in the log:

2019-02-11 21:42:57.755 [INFO ] [ipse.smarthome.model.script.execTest] - bash: cd // ; sudo usr/bin/python /home/pi/rpi_ws281x/python/examples/ConnectionTest.py: Datei oder Verzeichnis nicht gefunden

Furthermore, I have set the PYTHONPATH variable, so calling the full path of python shouldn’t be necessary…

BTW: logged in as openhab user on the primary RPI SSHing the slave RPI, the command works:

[21:46:32] openhab@openHABianPi://$ ssh pi@192.168.1.45 'cd // ; sudo usr/bin/python /home/pi/rpi_ws281x/python/examples/ConnectionTest.py'
The connection is working

The standard and proper way to handle full paths is not to cd to / and then use a releative path but to just provide the full path. /usr/bin/python /home/pi/…

Are you sure you are sshing to the right machine?

You can see clearly in the log the exact command that OH issued and you can see clearly the error message that was the result. What if you just issue:

ls /home/pi/rpi_ws281x/python/examples/ConnectionTest.py

Does it show the file? If so I’ve no further ideas. If not then there is something wrong with the ssh. Either it’s not actually running the command on the remote machine or there is a typo in the path or something.

It does not show the file when running via OpenHAB.

Running from putty, it works with openhabian user as well as with openhab user:

[06:33:28] openhabian@openHABianPi:~$ ssh pi@192.168.1.45 'ls /home/pi/rpi_ws281x/python/examples/ConnectionTest.py'
/home/pi/rpi_ws281x/python/examples/ConnectionTest.py
[06:34:53] openhab@openHABianPi:/home/openhabian$ ssh pi@192.168.1.45 'ls /home/pi/rpi_ws281x/python/examples/ConnectionTest.py'
/home/pi/rpi_ws281x/python/examples/ConnectionTest.py

The same command copied (=no typo) to the rule doesn’t work:

rule "ExceDebug"
when
Item GarageDoorUp received update ON
then
val results = executeCommandLine("ssh pi@192.168.1.45 'ls /home/pi/rpi_ws281x/python/examples/ConnectionTest.py'", 5000)
logInfo("execTest", results)
end
2019-02-12 06:38:18.607 [INFO ] [ipse.smarthome.model.script.execTest] - bash: ls /home/pi/rpi_ws281x/python/examples/ConnectionTest.py: Datei oder Verzeichnis nicht gefunden

Another remark:

[06:50:09] openhab@openHABianPi:/home/openhabian$ ssh pi@192.168.1.45 'which python'
/usr/bin/python

With putty, I am able to run the which python command via ssh.
With OpenHAB, it tells me “command not found”:

2019-02-12 06:56:12.976 [INFO ] [ipse.smarthome.model.script.execTest] - bash: which python: Kommando nicht gefunden.

All I can suggest is either the openHAB command is not sshing to the right machine, or it is treating the stuff that happens after the ssh as a separate command.

All I know is the command is not taking place on the machine you want it to.

Honestly, this is why I recommended against this sort of approach in the first place. It almost never works the way you expect it to.

1 Like

Thanks a lot for your help. Even I did not achive what I wanted to, I learned a lot. I’ll keep playing around with this stuff if there is some time(I can’t beleive that this is not working - there needs to be an answer…). But befor I spend too many hours, I’ll check your python script and implement this.

Another chance to learn something new.

Again, thanks and keep going!!!