One additional relevant piece of information that isn’t in the post @jimtng linked to (please read it because it is relevant overall) is that when you call executeCommandLine, the command is running as the same user that openHAB runs as, which is usually openhab. That is almost never the same user that you are logged in as so “via openhab cli I can connect to the server with” is almost irrelevant. The ssh key you’ve set up is unknown by the openhab user and you need to set up that user with the ssh keys and permissions and everything else it needs to run the command.
Ok, but for my understanding please explain.
I connect with user eg dirk to my other server via ssh from openhab console.
So why is there the user openhab involved?
The ssh key was created for user eg dirk. Why is openhab user important when the command get executed under another user on another server?
Thanks!
Because you are running the command as user openhab and the user you pass on the command line to ssh is the user on the other machine, not the local machine.
User openhab needs to have the ssh key.
Here’s an experiment you can run. On the command line:
sudo -u openhab
ssh dirk@192.168.178.180 ls
The first line gives you a command line running as the openhab user, same as what executeCommandLine gets.
You’ll get either a password prompt or a cert error depending on how the ssh server is configured when you run the second command.
Luckily to get that second line to work is probably as simple as copying the ssh public and private keys to ~openhab/.ssh which is going to resolve to /var/lib/openhab/.ssh. This directory does not already exist though, you’ll have to create it. And the cert files must be owned by user openhab and have read permissions for only the openhab user.
Once you can execute the command as user openhab on the command line, use the version of the call to executeCommandLine@jimtng posted above in post 5.
################ MISCELLANOUS ###################
# The karaf sshHost parameter configures the bind address for the ssh login to karaf.
# Default is 127.0.0.1 (localhost), so it is only possible to login from the local machine.
#
# Setting this to the address of another network interfaces will allow login from this network.
# Setting this to 0.0.0.0 will allow login from all network interfaces.
#
# !!! Security warning !!!
# Remember to change default login/password, if you allow external login.
# See https://www.openhab.org/docs/administration/console.html for details.
#
org.apache.karaf.shell:sshHost=0.0.0.0
When I open the terminal in Docker, I am able to send eg. ping commands but ssh can’t be run. It says: bash: ssh: command not found. I’ve come to the discovery that the cont-init.d folder also doesn’t mount. Set all permissions and users correct though…
It’s the latter. So from OH towards another, in this case, Raspberry Pi. I will comment this back out again. Thanks for the heads up.
In the meantime, the dir did mount and your script works like a treat! I can now go into the Docker container, open the terminal and ssh into the other Pi. For the passwordless setup, we need the certificate(s). I generated this before, or at least tried to, so I wonder where to mount this folder in the container? Do I need to touch ~/.ssh and place it there or is there some other place to stick it?
Hm, from the terminal all works fine, when I put it in a bash script, all works fine but when I put is directly in the Rules as
var test = actions.Exec.executeCommandLine(time.Duration.ofSeconds(10), "ssh", "pi@192.168.xx.xx")
console.log(test)
it comes back with the Failed to execute commandLine '[ssh pi@192.168.xx.xx]'-error. Ideally I keep the code in one place (so avoid setting up bash scripts). Are there any more hint as to why this approach doesn’t work?
I added all commands, with and without quotes to the exec.whitelist file…
Don’t know what happened but after yet another restart of the container it works!
I now just get a warning bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8) in my console.log(test) as return but at least after that also the expected output! Need a way to filter this out but that is beyond the scope of this topic.