Sending ssh commands to remote computer

Hello! I have added a remote computer (lubuntu) in my items.

/* SSH */

Switch Network_NAS “NAS [MAP(]” (Network, Status, GF_Kitchen) { nh=“”, exec=“OFF:sudo ssh USERNAME@ shutdown -p now”}

I can see the item on the web UI, and i can use the button to turn it off.

But nothing is happening when i turn it off.

Here is the log from openhab:

2016-01-07 13:52:43.609 [INFO ] [.o.b.exec.internal.ExecBinding] - executed commandLine ‘sudo ssh USERNAME@ shutdown -p now’

I can not see any incoming connections at the remote computer, i have set up ssh keys so that i dont need a password when sending the command.

Have you configured sudo to not need a password (usually not a great idea if you did)?

The sudo is just for the client where the ssh command is executed.

After a bit of searching on the web i have got as far as this:

Jan 7 15:44:57 rosander-Aspire-one sshd[1775]: Connection closed by [preauth]

cant seem to get the command to execute. I can execute it when logging in manually via ssh.

The exec binding will execute the string you give it on the local host in a child process of the openHAB server. The command you are asking the binding to execute is “sudo …” (the ssh-related arguments are for the sudo command). Are you trying to to “ssh sudo shutdown …” rather than “sudo ssh shutdown …”?

I don’t know how to interpret the log message you included without more context.

1 Like

Thank you for your reply.
Do you know how i can get some more debuginfo from the logs?

The reason i use sudo is because the ssh server one the remote machine asked for password when not using the sudo to start the ssh connection. I realised that i used sudo when creating the key with ssh-keygen so the key was made for the root account.
I did it again (made the keys) and got i done correct so i removed the sudo, but i cant connect to the ssh server.

I have done som searching and found some information about folder/user permissions so i will have another look later tonight.

This works for me:

Switch RebootXbmc {exec=">[ON:/bin/sh@@-c@@sudo -u pi ssh xbmc -l pi sudo /sbin/reboot] >[OFF:/bin/sh@@-c@@sudo -u pi ssh xbmc -l pi sudo /sbin/poweroff]", autoupdate=“false”}

In the sitemap, ON is mapped to ‘Reboot’, OFF to ‘Off’.

1 Like

So what you are saying is that the machine where OH is running requires sudo to execute ssh? That seems odd but I’ve seen stranger things. However, what is common is to need sudo to execute shutdown which I think was @steve1’s point.

Assuming this is right, have you changed sudoers so a password is not required by openhab to execute ssh? Otherwise it will just sit there and eventually time out waiting for a password.

Assuming you are running OH on this error means it timed out waiting for you to enter a password.

Have you set up your ssh keys so you can ssh to without a password? If so have you set up the openhab user with those keys as well? Remember that OH runs as the openhab user, not pi or root or any other user you might have and running the command from manually.

The easiest way to debug these sorts of issues I’ve found are to convert the switch to a dumb switch, execute the command in a rule using executeCommandLine(cmd, timeout) (remember the timeout or else it won’t return anything) and log what it is printing.

I solved a tricky problem this way that was caused by the private key in openhab’s .ssh folder not having the right permissions. It worked fine from the command line even when I used runuser but when openhab tried to ssh it generated the error.

Here is my code for reference.


//Switch  S_N_GarageMqttReporter "Reset mqttReporter on Garage" <network>         { exec=">[*:/etc/openhab/configurations/scripts/]"}
Switch S_N_GarageMqttReporter "Reset mqttReporter on Garage" <network>


rule "Reset Garage mqttReporter"
        Item S_N_GarageMqttReporter received command
        val String results = executeCommandLine("/etc/openhab/configurations/scripts/", 5000)
        logInfo("Network", "Resetting garage mwttReporter. Results:\n" + results)

Once you figure out what the error is and correct it you can go back to the Exec binding on the Item.

1 Like

Thank you!
I did set up ssh keys for both osmc account and the root account. I did also create keys for the openhab account BUT when you log in for the first time after creating a key you have to type yes/no!

You can not log in with the user “openhab” by default so i had to enable login and connect via ssh manually the first time.