Synology NAS - SSH Shutdown issue

Ah, thank you. It was the hint with connecting from the HA shell to the NAS first to accept the security fingerprint.

switch:
  - platform: command_line
    switches:  
      diskstation3cmd:
        command_on: 'wakeonlan 00:11:32:1A:A2:70'
        command_off: 'sshpass -p "password" ssh -t [email protected] "echo password | sudo -S poweroff"'

Hm, it works manually over the linux shell.
But with the above configuration, i can only start the NAS, but the ‘off’ command seems to be ignored. Anything missing or is this the wrong approach with a command-line switch?

EDIT:
I also tried this way, but no effect either:

shell_command:
  diskstation3_shutdown: '/usr/bin/sshpass -p "password" ssh -t [email protected] "echo password | sudo -S poweroff"'

switch:
  - platform: wake_on_lan
    mac: "00-11-32-1A-A2-70"
    name: diskstation3
    host: 192.168.1.67
    turn_off:
      service: shell_command.diskstation3_shutdown

I always had problems with HA and quotes in shell_command and prefer to use bash script ran by HA.
This is why you have #! /bin/bash at first line.
I suggest you try the same approach…

I´m quit unfamiliar using bash scripts - I just tried putting your two lines of code into “ds3_shutdown.sh” and put that file into /config/scripts and replaced the shell_command into:

shell_command:
  diskstation3_shutdown: bash /config/scripts/ds3_shutdown.sh

Should this work? Unfortunately it´s still not shutting down.

Make sure the script works by executing manually from the shell
you may need to trim the #! /bin/bash depending on your platform.
Verify the path for bash in linux console and adjust accordingly if different

whereis bash
bash: /bin/bash /etc/bash.bashrc /usr/share/man/man1/bash.1.gz

This is an extract of my configuration.yaml and it works like a charm:

switch:
  - platform: template
    switches:
      nas_power:
        friendly_name: Synology NAS
        value_template: >-
          {% if is_state('timer.nas_delay', 'idle') %}
          {{ states("binary_sensor.nas_ping") }}
          {% else %}
          {{ states("input_boolean.nas_power_status") }}
          {% endif %}
        turn_on:
          - service: shell_command.nas_command_on
          - service: input_boolean.turn_on
            data:
              entity_id: input_boolean.nas_power_status
          - service: timer.start
            entity_id: timer.nas_delay
        turn_off:
          - service: shell_command.nas_command_off
          - service: input_boolean.turn_off
            data:
              entity_id: input_boolean.nas_power_status
          - service: timer.start
            entity_id: timer.nas_delay
        icon_template: mdi:network-attached-storage
 


timer: # It takes 50 seconds for my NAS to connect / disconnect from the network
  nas_delay:
    duration: '00:00:50'



input_boolean: # This stores the temporary  power state while we are waiting for the ping to update
  nas_power_status:
    name: NAS Power Status


binary_sensor:
  - platform: ping
    host: 192.168.1.121
    name: nas_ping
    scan_interval: 2
    count: 2

shell_command:
        nas_command_on: /config/wake-nas.sh
        nas_command_off: /config/stop-nas.sh

I have no clue… :sob: I can execute the script manually from the shell and it works.
within home assistant, nothing happens exept always the same error code in the log:
(I tried several configs)

This might be a stupid question, but i just don´t know better. Which shell is being used to execute the command? I have installed ubuntu, and on top of that, Home Assistant with docker.
Is this the ubuntu shell, or the Home Assistant CLI the command runs in?

2020-05-11 12:19:13 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `bash /config/scripts/ds3_shutdown.sh`, return code: 127
2020-05-11 12:45:49 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `/bin/bash /usr/share/hassio/homeassistant/scripts/ds3_shutdown.sh`, return code: 127
2020-05-11 13:07:09 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `./config/ds3-shut.sh`, return code: 127
2020-05-11 13:13:51 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `bash /config/ds3-shut.sh`, return code: 127

Docker is the issue…
Your docker version of HA is another linux instance, it’s like running a virtual machine within your Ubuntu and most probably sshpass is not installed in the docker container.
try this command:

docker exec -it homeassistant /bin/bash

this will open a terminal within the docker container and try executing the script
just type “exit” to come back to Ubuntu shell

You can install sshpass with the following command inside the docker console but most probably you will need to do it again after any HA upgrade…

apk --update --no-cache add sshpass openssh

Hallelujah, thank you for you patience and help!
Finally it´s working.

Good to know you’re happy :hugs:

Well, hello, I´m back again with the same issue in another scenario and can´t get it work after hours of searching:

I moved from my ubuntu-docker-hosted installation to an esxi installation with the hassos VM.
Again, I can execute the shutdown scripts from the CLI, but not from a lovelace card, as the SSH Plugin has a different container, right?

The former solution was to install sshpass within the docker container, but how is this possible from the limited HA shell? It doesn´t even recognize the docker command?

You should be able to setup a shell command and use that to docker exec into any container…

Would you mind sharing an example to help me understand the syntax so I can adapt it to my needs, please?

I tried it this way, but no luck either. Maybe something is missing?

shell_command:
  diskstation3_shutdown: "docker exec -it 44ecde239303 /bin/bash /config/ds3-shut.sh"

Is that the correct container name for ssh?

I tried both the container ID at the beginning and the name at the end.
Should it work that way anyway?

44ecde239303        hassioaddons/ssh-amd64:7.5.0                         "/init"                  2 hours ago         Up 2 hours                                                                               addon_a0d7b954_ssh

Here is one of my scripts:

#!/bin/bash

# Checking ip_bans exists

if [[ -e "/usr/share/hassio/homeassistant/ip_bans.yaml" ]]
then
  sudo rm /usr/share/hassio/homeassistant/ip_bans.yaml
else
  echo "EXITING. *****No ip_bans.yaml found***** Restart not needed"
  exit
fi

# Restart Home Assistant after removing ip_bans.yaml
echo "RESTARTING Home Assistant"
docker exec -it addon_a0d7b954_ssh ha core restart

(It’s one I use to remove ip_bans and then restart HA.)

I would then have a shell_command like here:

shell_command:
  clean_knowndevices: /bin/bash /config/clean_knowndevices.sh

Except calling the restart script.

Hope this helps. Remember use the CONTAINER name as above, not the image name

Thank you, I have it almost identical adapated:

shell_command:
  diskstation1_shutdown: /bin/bash /config/ds1-shut.sh

and the script:

#!/bin/bash
docker exec -it addon_a0d7b954_ssh sshpass -p "password" ssh -t [email protected] "echo password | sudo -S poweroff"

but nothing happens, except and error entry in the HA Log:

2020-08-27 09:41:30 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `/bin/bash /config/ds1-shut.sh`, return code: 127

Have you tried that script if you go into the ssh terminal?
What are your addon settings? Off the top of my head I use compatibility mode and allow sftp. (Only way I can get WinSCP to work correctly). I also use a private key instead of the user/password…

Also you can use a ha command to shutdown the host…

Except of the private key (don´t know how to do that yet), I enabled compatibility mode, sftp and also turned off the protection mode.
And yes, the script works well from the ssh terminal:

Jesus, I simply want to shut down a NAS device, not doing rocket science. :weary:
I wonder if no one else has faced this problem with synology nas devices.

1 Like