openHAB version:4.3.0
I know that failures to run command line from within Openhab have been discussed many times, I tried to follow all the recommendations but still cannot get it to work
I want to run a bash script, startmm, from within Openhab
var result = actions.Exec.executeCommandLine(time.Duration.ofSeconds(3), "/etc/openhab/scripts/startmm");
console.log(result);
When I run the rule the logged result is blank and the command does not get executed on the remote server.
On the remote server I see closing of the SSH connection but not opening
Feb 15 20:20:05 pi-mirror sshd[9080]: Connection closed by 192.168.0.98 port 42692 [preauth]
Feb 15 20:21:10 pi-mirror sshd[9135]: Connection closed by 192.168.0.98 port 40054 [preauth]
Feb 15 20:25:45 pi-mirror sshd[9325]: Connection closed by 192.168.0.98 port 39794 [preauth]
First, please do not use /etc/openhab/scripts/ as a directory for bash scripts, as it’s meant only for *.script files (DSL scripts, that can be called via callScript("name") from DSL rules).
Second, you’ll have to use the exact name. If the script is named startmm.sh, you’ll have to use startmm.sh, not startmm.
Would be better to use public/private key to control the remote machine.
Apart from not being best practice, does it create a problem to place the files in /scripts?
There are two different scripts, one is startmm.sh which is the script that gets passed on to to SSH and there is an executable script startmm which is what Openhab should execute.
I tried using public/private keys and got errors, using the passssh might not be optimal but I know it works. Once I can get Openhab to succeed in running the script I can work on making the connection more secure.
you might try updating the sudoers file to allow openhab user to run with root for that script perhaps.
some thing like this in your sudoers file openhab ALL=NOPASSWD: /etc/openhab/scripts/startmm
might work and update your rule
var result = actions.Exec.executeCommandLine(time.Duration.ofSeconds(3), "sudo","/etc/openhab/scripts/startmm");
console.log(result);
I will try that but I think the problem lies somewhere else.
When I say that running the executable script as user openhab fail, what is really happening is that I get no response and the command does not get executed on the server side, but there is no error.
If I run the script as a regular user, on the server side I get this
Feb 15 17:27:09 pi-mirror sshd[1648]: Accepted password for pi-mirror from 192.168.0.98 port 59584 ssh2
Feb 15 17:27:09 pi-mirror sshd[1648]: pam_unix(sshd:session): session opened for user pi-mirror(uid=1000) by (uid=0)
Feb 15 17:27:09 pi-mirror systemd-logind[422]: New session 5 of user pi-mirror.
Feb 15 17:27:14 pi-mirror sshd[1654]: Received disconnect from 192.168.0.98 port 59584:11: disconnected by user
Feb 15 17:27:14 pi-mirror sshd[1654]: Disconnected from user pi-mirror 192.168.0.98 port 59584
Feb 15 17:27:14 pi-mirror sshd[1648]: pam_unix(sshd:session): session closed for user pi-mirror
Feb 15 17:27:14 pi-mirror systemd-logind[422]: Session 5 logged out. Waiting for processes to exit.
Feb 15 17:27:14 pi-mirror systemd-logind[422]: Removed session 5.
When I run the same script as user openhab I get this on the server
Feb 15 17:30:35 pi-mirror sshd[1698]: Connection closed by 192.168.0.98 port 46540 [preauth]
I don’t know enough about Linux to be able to figure out what this is telling.
var result = actions.Exec.executeCommandLine(time.Duration.ofSeconds(3),"sudo", "/etc/openhab/scripts/startmm");
console.log(result);
No difference in the results. I get no errors but on the server side I only see the connection closing and the script I send to the server to be executed is not executed.
I think openhab is executing the script but for some reason it is seen differently on the server than if a regular user executes it.
I found the problem.
The issue was that when openhab initiated the connection the server asked to approve (not sure if this is the correct term) the fingerprint. Adding -o “strictHostKeyChecking=no” to the ssh command solved the issue
ssh-copy-id will ask whether the connection is safe (authenticity can’t be established…) and for the password of the remote host.
check whether connection can be establishhed
ssh user@remote-host -i .ssh/openhab_ed25519
If the connection fails, please check whether the home directory is writable for other users
ls -l .. | grep openhab
The output should be like
drwxr-xr-x 20 openhab openhab 26 23. Jan 01:41 openhab
and if the bits are wrong, you should change them
chmod 755 ../openhab
If you still get errors, maybe the remote host is not able to use ed25519 (very unlikely, but…)
You can create as many public/private keys as you want, as long as the names are different (obviously). There is no need for a remote user openhab, although the public/private key belongs to openhab. But it’s crucial that openHAB will use the username from step 4 to log in remotely.
That should work as well, but openhab (the user) has to be the owner of the keys. The easiest way to achieve this, is to create them with the user openhab, then it’s straight forward…