Power on / off NAS by using WOL and SSHPASS

I did not want to perform the “apk add sshpass” after every update, so I changed the connection to SSH with Keyfiles. For a HowTo to do so on synology nas, you can follow this one: Log in to a Synology DiskStation using SSH keys as a user other than root

First of all, you have to add the SSH Keyfiles on the Hostsystem (the private key needs to be openssh format) and then

Put the Private Key FIle
(private_key)

to

/config/ssh_keys/
on Home Assistant

CHMOD it to 600

chmod 600 /config/ssh_keys/private_key
(use the Console Addon)

Create a SWITCH for powering on / off.

switch:
  - platform: wake_on_lan
    name: YOURNAME
    mac: YOURMAC
    host: YOURIP
    turn_off:
      service: shell_command.turn_off_YOURNAME
shell_command:
    turn_off_YOURNAME: bash /path/to/script/SCRIPTNAME.sh

Create the script “SCRIPTNAME.sh” for powering off:

ssh -i /config/ssh_keys/private_key -o StrictHostKeyChecking=no SSHUSERNAME@HOSTIP "echo REMOTESERVERROOTPASSWORD | sudo -S poweroff"

SSHUSERNAME = Username to connect via SSH to the remote Host
HOSTIP = IP of the remote Host
REMOTESERVERROOTPASSWORD = RootPassword of the Remote Host to have enough rights to run the poweroff

DONE :wink:

3 Likes