Power on / off NAS by using WOL and SSHPASS

Hello togehter,

I just switched from OpenHAB to HomeAssistant (HassIO on Raspberyy PI4 4GB).

Now I try to adapt my OpenHAB switches etc. to HomeAssistant.

One of the “problems” I’ve run into is a switch to power on / off my synology nas.

It seemed to be quite complicated due to the lack of permissions etc… Also everything i read on did not realy work for me. So I got a bit deeper (first time to use docker etc.)

And found the following Solution for my problem which I will show you below.

  1. 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
  1. Create the script “SCRIPTNAME.sh” for powering off:
#! /bin/bash
sshpass -p "REMOTEPASSWORD" ssh -t REMOTEUSER@REMOTEIP "echo REMOTEPASSWORD | sudo -S poweroff"
  1. Install the “SSH & Web Terminal” Add-On and deactivate the “protection mode”.

In Terminal run:
docker ps -a

Look for the one with two times “homeassistant” in the name by “Image” and copy the “CONTAINER ID”

  1. Run from Terminal:
    sudo docker exec -it CONTAINERID /bin/bash

  2. Run from Terminal:
    apk add sshpass

  3. Connect to remote host to verify the fingerprint
    ssh -t REMOTEUSER@REMOTEIP

type “yes” to confirm the fingerprint and add your password.

Quit by typing:
exit

  1. Optional: Turn on “protection mode” again.

You’re done now. The switch will power on your NAS by using WOL and power it off by using sshpass.

NOTE: You have to do step 4 to 6 after every Update!

Hope that will help someone else!

Greetings

6 Likes

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

Hi @Lucan,

Minor correction to your code above:

    turn_off:
      service: shell_command.turn_off_YOURNAME

should be

    turn_off:
      service: shell_command.turn_off_storage

matching the shell_command:

shell_command:
    turn_off_storage: bash /path/to/script/SCRIPTNAME.sh

pretty much sure it was a typo …

thanks for your code. I can now easily wake up my NAS and switch it off when needed through HA.

Forgot to rename the name i choose (storage) with “YOURNAME” in the tutorial above. I changed it now - thank you.

It is faster and easier creating a switch like this:

switch:
  - platform: wake_on_lan
    name: "NAME_NAS"
    mac: 00-AA-BB-CC-DD-EE
    host: 192.168.1.2
    turn_off:
      service: synology_dsm.shutdown

Of course, you need first to integrate the Synology NAS in Home Assistant: Menu: Configuration -> Integrations . Search for “Synology DSM”, fill in the configuration form with your username and password, and then click Submit .
MAC and host addresses above are examples, you will need yours.
Wake on lan service also needs to be in configuration.yaml of home assistant:

# Example configuration.yaml entry
wake_on_lan:

True, nice for synology - did not know that there is a service included for that.

But mine could be adapted for almost every linux computer - so both is usefull.

Is there also a way to turn on the synology?

The Switch turns it on with WOL - so yes

I tried doing this but to connect to my windows machine. I generated a key using “ssh-keygen” on windows and copied the contents of the id_rsa to a file in my config “/config/ssh_keys/id_rsa”, so my shell command is:

ssh -i /config/ssh_keys/id_rsa -o StrictHostKeyChecking=no username@ip_of_windows "powercfg -h off & rundll32.exe powrprof.dll,SetSuspendState 0,1,0"

However it still asks for the password when I test it in the SSH terminal on the Home Assistant.

Any assistance on how to get this working would be greatly appreciated!
Cheers,
Josh

worked for my qnap thank you

Hello gomble,
I have a QNAP TS 251+.
can you describe in a bit more detail your solution for the WOL of the QNAP in Home Assistant?

Do I need the ssh files? if yes, where can i get the keys and where do I have to put it on the qnap?
And where do I have to put the script? e.g. /config/scripts/ SCRIPNAME.sh ?

thanks in advance!

switch:
  - platform: wake_on_lan
    mac: 24:5E:BE:0C:A9:88
    name: QNAP
    host: 192.168.178.195
    turn_off:
      service: shell_command.turn_off_qnap

This switch starts my qnap with wol

The shell command stops it


shell_command:
  turn_off_qnap: "bash /config/qnapshutdown.sh"

Qnapshutdown.sh:

ssh -i /config/ssh/qnap/id_rsa -o StrictHostKeyChecking=no [email protected] "poweroff"

You have to generate ssh keys with the terminal addon. I have saved my ssh keys under /config/ssh/qnap. Then you have to activate ssh in qnap and upload the ssh keys via gui to the qnap. This is described in this thread.

Hey all!

I am trying to figure out how to create ssh key on the QNAP 464 so that my HA can connect directly to it in an automation in order to shut it down gracefully.

Since HA knows when my UPS is on battery or not I want HA to gracefully shut down the QNAP NAS when UPS is on battery.

These are the steps needed as I understand it:

  1. Log on to the QNAP with ssh as admin.
  2. Generate a ssh key ← This is where I am stuck. How do I do that?
  3. Download client key from QNAP server.
  4. Log on to the HA server with ssh and upload the client key ← Where do I store it?
  5. Run “shell_command” with command “halt” in my automation.

Can anyone help me with step 2 and 4?

I solved this myself by doing this:

  1. ssh in to Home Assistant.
  2. Ran following command
ssh-keygen -t rsa -C "QNAP HA"

Make sure not to use any password (just press Enter when prompted twice).

  1. From HA I ssh in to QNAP server:
ssh admin@nas01
  1. I trusted the login
  2. I disconnected the ssh session from the QNAP server and was back in HA ssh session.
  3. Now I copied the key from HA to QNAP server using:
ssh-copy-id admin@nas01
  1. Done! I was now able to ssh from HA to QNAP server without any password.

Is there anyway to keep the sshpass from disappearing upon reboots/ OS updates?
This method works great for NAS shutdown otherwise.
Can someone please explain why sshpass (or any other package?) is not retain after reboots (on HassOS)

Well, I had the same need, and solved it by installing sshpass with apk, and after that copying the binary into homeassistant folder (where all YAML files are). I can’t say if it is the right/best/optimal solution, but it is retained after the reboot/upgrade