I am trying to run remote commands on a server using ssh.
I have tried directly by calling a one-liner ssh command (I have setup keys between OH2 ad the remote server.
I have tried using a local local bash script that remotely ssh’s to the remote server and runs commands or scripts on the remote server.
I have also tried an expect script once again the script ssh’s to remote server and runs commands/script as required.
All of these methods work using the openhabian user on the command line, but do NOT work when using executeCommandLine.
Using executeCommandLine I can run simply local commands such as “logger Test”
The basin command in my rules looks like this
executeCommandLine ("logger Test",5000)
As per my explanation above, I have tried the following and all have failed.
// bash script to login and run a remote bash script.
executeCommandLine ("update_me update_venus")
// expect script that logs in remotely and runs a command
executeCommandLine ("rmt_cmd 'logger Test'",5000)
I do not get an error in the openhabian.log
It is as if executeCommandLine cannot spawn a new process.
I would appreciate any tips in troubleshooting this as I have been going around in circles.
I believe (that in openHABian) the OH2 process runs as user openhab.
Try to troubleshoot this user with regards to shell commands.
Change its login shell (currently disabled in /etc/passwd), login as openhab and try to execute the script.
As opus pointed out, the problem is almost certainly caused by the fact that OH is running as the openhab user, not the openhabian user. It is standard practice on Linux to give services their own limited access user to run under.
Consequently, you will need to set up your ssh keys in ~openhab/.ssh and make sure they are owned by the user openhab and have only user read permissions.
To see the result from running executeCommandLine, you need to use the call that has the number as the second argument. But what you are missing is that the result is returned by executeCommandLine. So what you want to see the error is:
val results = executeCommandLine("rmt_cmd 'logger Test'", 5000)
That will likely show you the error you are getting from ssh.
I would like to thank you all for the replies as between you all I was able to piece it all together and get it working.
I had indeed fall into the apparent common trap of setting keys for openhabian rather than openhab, and even as a reasonably experienced linux user I had never set it up for a system account before so pointers in that direction were great.
As it seems like a common issue for many users I am wondering if the settings could be scripted tto include key creation and distribution to a remote server with a set of tools (scripts)?