[SOLVED] Problem running EXEC command to shutdown Synology Diskstation via SSH

Hello community,

after setting up scripts and items to start and shut down my Windows PC, I wanted to do the same with my Synology Diskstation.

As preparation, I made sure that user “Openhabian” can ssh to the Diskstation and login there as root without password using keys (setting up keys, put them in Synology’s authorized_keys file). With the following command, user “Openhabian” can connect to my Synology as root:

[16:12:14] openhabian@openHABianPi:~$ ssh root@192.168.75.10 -i /home/openhabian/.ssh/id_rsa_root
root@DiskStation:~#

Next, I extended the command to shutdown diskstation:

[16:18:36] openhabian@openHABianPi:~$ ssh root@192.168.75.10 -i /home/openhabian/.ssh/id_rsa_root sudo poweroff

After issuing this command, the Diskstation starts to shutdown. Everything ok so far.

Next, I set up a thing with EXEC command, containing the very same ssh-command:

Thing exec:command:synooff [command="ssh root@192.168.75.10 -i /home/openhabian/.ssh/id_rsa_root sudo poweroff", interval=0, autorun=false]

Items:

Switch 			Presence_DiskStation		"DiskStation" 				<network> 	{channel="network:pingdevice:192_168_75_10:online" }
Switch			Synology_ON_OFF				"Synology"					<network>   ["Switchable"]
Switch 			Synology_ON					"Synology ON"				<network>	{wol="192.168.75.255#00:11:32:2D:94:C5" }
Switch 			Synology_OFF 				"Synology OFF" 							{channel="exec:command:synooff:run" }

Rules:

rule "Synology onoff"

when Item Synology_ON_OFF received command
then switch (receivedCommand)
	{
	case ON: { 
	Synology_ON.sendCommand(ON)
	logInfo("default.rules", "Synology_ON.SendCommand(ON) executed")
	}
    case OFF: { 
	Synology_OFF.sendCommand(ON)
	logInfo("default.rules", "Synology_OFF.SendCommand(ON) executed")	}
	}
end

Sitemap:

Switch item=Synology_ON_OFF label="Synology Diskstation"

Log-Output when switching off:

2018-12-16 16:09:33.395 [INFO ] [smarthome.model.script.default.rules] - Synology_OFF.SendCommand(ON) executed

So I can see in the log that the command was running, but the Synology does not shut down. The WOL does work, by the way.

Now I wonder why the command

ssh root@192.168.75.10 -i /home/openhabian/.ssh/id_rsa_root sudo poweroff

is working when issued as user “Openhabian” on the command line, but not when used with the EXEC-Binding. The EXEC-Bindung itself is working, because my command for shutting down Windoes by net rpc shutdown works fine.

Any ideas?

No expert here, but did you try to experiment with your interval and timeout settings? Maybe remove the interval and include a timeout of 5000

I think this posting led me on the right way:

I think my mistake is:

I mixed users “openhabian” and “openhab”. The user the exec-binding is using is indeed “openhab”, not “openhabian”. So I have to make sure ssh works for user openhab. I will try to change configs and report.

My assumption was right. To make things work, I had to to the following:

Make the public key of the destination known to user openhab by

sudo -u openhab /usr/bin/ssh root@192.168.75.10 

and answering “yes”.

Copy the private keyfile to /var/lib/openhab2/.ssh/ and change ownership to openhab:

cd /home/openhabian/.ssh
sudo cp id_rsa_root /var/lib/openhab2/.ssh
sudo chown openhab id_rsa_root
sudo chgrp openhab id_rsa_root

Then, change the .things accordingly:

Thing exec:command:synooff [command="ssh root@192.168.75.10 -i /var/lib/openhab2/.ssh/id_rsa_root sudo poweroff", interval=0, timeout=30, autorun=false]

Now everything works fine, and I will never forget that EXEC-commands run under user “openhab” and NOT “openhabian” :wink: