How to restart bundles automatically

Yes, it is in passwd. I have tried rolling it back and redoing your instructions with out root. I have complete reinstalled the OS and using a nightly build and the regular build and it all results in the same. Now I am the stable release of openHABian and it still is not working. Keeps asking for a password and the log keeps looking in the my home directory. I am at a lose. May I ask what you have yours running on? Maybe it’s an issue with Ubuntu security.

I’m running Debian GNU/Linux 8.
Well, at the moment I’ve no idea as well, sorry.

OK so here is where I am at. I was able to get openhab added to the trusted hosts by running the following command under root.

sudo -u openhab ssh -i /var/lib/openhab2/.ssh/openhab.id_dsa openhab@127.0.0.1 -p 8101 -v

I can see in the log

debug1: Found key in /var/lib/openhab2/.ssh/known_hosts:1

I think this is the issue, but no idea what it means

debug1: Skipping ssh-dss key /var/lib/openhab2/.ssh/openhab.id_dsa - not in PubkeyAcceptedKeyTypes

When I run the above script from the rule, before I was getting

but now I am getting

2018-01-28 10:41:24.268 [INFO ] [e.smarthome.model.script.RestartBind] - RestartBind rule triggered
2018-01-28 10:41:24.321 [WARN ] [lipse.smarthome.io.net.exec.ExecUtil] - Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program “/etc/openhab2/scripts/Restart.sh” (in directory “.”): error=13, Permission denied)
2018-01-28 10:41:24.323 [INFO ] [e.smarthome.model.script.RestartBind] - Results from binding restart:

So I am going from exceptionCaught to Execution failed
So I think the issue now is with it skipping the ssh key.

Try first to get it running without sudo. Give openhab a login shell e.g. /bin/bash and try to get it running on the console

ssh -i /var/lib/openhab2/.ssh/openhab.id_dsa openhab@127.0.0.1 -p 8101

If this works, go further.

debug1: Found key in /var/lib/openhab2/.ssh/known_hosts:1

means that ssh found a valid hostkey (of the host you want to connect to) in know_hosts - that’s good :wink:

So after many hours of messing with this I finally got this to work. Since I was logged as myself I had to run everything as openhab, because openhab is not allowed to sign in on the device. So my install looks something like this:
First I had to enable root access on the device and elevate my user to root using su

mkdir /var/lib/openhab2/.ssh
cd /var/lib/openhab2

Then I had to give openhab permissions to the .ssh directory

chown -R  openhab:openhab .ssh
chmod 700 .ssh
cd /var/lib/openhab2/.ssh

So far everything looks the same except for the order, but this is were it differs

generate the ssh-key:
sudo -u openhab ssh-keygen -t rsa -f openhab.id_rsa
Then give openhab permissions:
sudo chmod 400 openhab.id_rsa

If you noticed I had to go with an rsa key, because openssh version (7.0+) deprecated DSA keys and I had to change the chmod to 400, because ssh said it was too open. So now when I run the command I get this:

[11:06:09] goug@OpenHAB:/var/lib/openhab2/.ssh$ sudo -u openhab ssh -i /var/lib/openhab2/.ssh/openhab.id_rsa openhab@127.0.0.1 -p 8101 -v
OpenSSH_7.2p2 Ubuntu-4ubuntu2.4, OpenSSL 1.0.2g 1 Mar 2016
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 127.0.0.1 [127.0.0.1] port 8101.
debug1: Connection established.
debug1: identity file /var/lib/openhab2/.ssh/openhab.id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /var/lib/openhab2/.ssh/openhab.id_rsa-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.4
debug1: Remote protocol version 2.0, remote software version SSHD-CORE-1.6.0
debug1: no match: SSHD-CORE-1.6.0
debug1: Authenticating to 127.0.0.1:8101 as 'openhab’
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: ecdh-sha2-nistp256
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: aes128-ctr MAC: hmac-sha2-256 compression: none
debug1: kex: client->server cipher: aes128-ctr MAC: hmac-sha2-256 compression: none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ssh-rsa SHA256:xk9102G7I4leunPHgueJzdwMyUlOXZYajdIBBRe5hSc
debug1: Host ‘[127.0.0.1]:8101’ is known and matches the RSA host key.
debug1: Found key in /var/lib/openhab2/.ssh/known_hosts:1
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: keyboard-interactive,password,publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /var/lib/openhab2/.ssh/openhab.id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug1: Authentication succeeded (publickey).
Authenticated to 127.0.0.1 ([127.0.0.1]:8101).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: pledge: network
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8

                      __  _____    ____

____ ____ ___ ____ / / / / | / __ )
/ __ / __ / _ / __ / // / /| | / __ |
/ /
/ / // / __/ / / / __ / ___ |/ // /
_/ ./_// /// /// |/_____/
/_/ 2.2.0
Release Build

Hit ‘’ for a list of available commands
and ‘[cmd] --help’ for help on a specific command.
Hit ‘’ or type ‘system:shutdown’ or ‘logout’ to shutdown openHAB.

openhab>

Now when I run the script from a rule:

ssh -i /var/lib/openhab2/.ssh/openhab.id_rsa openhab@127.0.0.1 -p 8101 
bundle:restart 208

I get this in the log file:

2018-01-28 11:05:30.988 [WARN ] [lipse.smarthome.io.net.exec.ExecUtil] - Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program "/etc/openhab2/scripts/restart.Binding.sh" (in directory "."): error=13, Permission denied)

So I am not a 100% there, but I am close. Just need to figure out why it’s failing in the rule. By the way this is the rule:

rule "RestartBind"
when
	Item LgTvRestart changed from OFF to ON
then
	logInfo("RestartBind","RestartBind rule triggered")
	var String results = executeCommandLine("/etc/openhab2/scripts/restart.Binding.sh",1000)
	logInfo("RestartBind", "Results from binding restart: " + results)
	postUpdate(LgTvRestart, OFF)
end

Thank you for getting me this far, I do appreciate it greatly!

So I decided to forget the script since it’s just one or two lines and just ran everything straight from the rule and it work perfectly.

 `var String results = executeCommandLine("ssh -i /var/lib/openhab2/.ssh/openhab.id_rsa openhab@127.0.0.1 -p 8101 bundle:restart 208",1000)

Thanks again for all of your help. This was my first experience with the OpenHAB community and I am very impressed. Keep up the great work! This is going to be a great tool to help keep my bindings up to date for a totally automated setup.

Congratulations to your success.

Just to make life a little easier, you could use a config file for your ssh console connection:

pico ~/.ssh/config


Host console
        hostname localhost
        port 8101
        user openhab
        IdentityFile /var/lib/openhab2/.ssh/openhab.id_dsa

Now you can just use: ssh console

…and you have karaf :wink:

Why so complicated with ssh and sudo right?

I use openhab in the docker, but it should work with every installation.

rule"Restart Bundle org.jupnp"
when
  Item <TRIGGER> changed to <STATE>
then
  executeCommandLine("bash /openhab/runtime/bin/client bundle:restart org.jupnp")
end
4 Likes

Sound’s in deed much more easier :wink:
Thanks for this, I’m going to test it - by time.

Is this the bundle name which I can get with
bundle:list -s ?

The client is located under /usr/share/openhab2/runtime/bin/client for a debian installation - just for the notes.

yes

As usual in linux, nothing is straight forward

kris@localhost:/etc/openhab2$ cd /home/kris
kris@localhost:~$ cd .ssh
kris@localhost:~/.ssh$ ls
config  known_hosts
kris@localhost:~/.ssh$ cat config
Host console
        hostname 127.0.0.1
        port 8101
        user openhab
        IdentityFile /var/lib/openhab2/.ssh/openhab.id_rsa
kris@localhost:~/.ssh$ pwd
/home/kris/.ssh
kris@localhost:~/.ssh$ ssh console
no such identity: /var/lib/openhab2/.ssh/openhab.id_rsa: Permission denied
Password authentication
Password:

kris@localhost:~/.ssh$