Synology NAS - SSH Shutdown issue

I tried to avoid this with a python script, but I can’t execute it… “python3 command not found”
I need to install python on Hassio???

I just used Python… I’ll need to check that tomorrow and let you know…

This was the shell command I created that works.

# Clear Out Gmail Alarm Notifications
shell_command:
  removealarm: python /config/emptyinbox.py

I then have a button card in lovelace I can use to call it.

No python command for me.

core-ssh:~# python /config/script/nas_shutdown.py                               -bash: python: command not found
core-ssh:~# python3 /config/script/nas_shutdown.py                              -bash: python3: command not found

If I try to execute my

shell_command:
  turn_off_nas: python3 /config/script/nas_shutdown.py

I get this error:
Error running command: python3 /config/script/nas_shutdown.py, return code: 1 NoneType: None

Same issue with python instead of python3…

The right way to install python on Hassio?

I tried also with python_scripts:

but if I execute my script (nas_shutdown.py):

import subprocess
import sys
cmd = "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /root/.ssh/id_rsa [email protected] 'sudo /sbin/shutdown -h now'"
x = subprocess.check_output(cmd, shell=True)
print(x.decode("utf-8"))

I get this error:

Error executing script: __import__ not found
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/python_script/__init__.py", line 166, in execute
    exec(compiled.code, restricted_globals, local)
  File "nas_shutdown.py", line 1, in <module>
ImportError: __import__ not found

Solved following this guide:

Hi
Could you tell us, how you did it?
I try to find the guide, but the link is broken.
Thank you

Hi

Please could you share with us the shell command that you use inside the emptyinbox.py?

I would like to create the same in order to shutdown my SynologyNass

Thanks

Hi

Can you give us more information? I tried to follow the guide that you reference

Problem for me is that i not able to connect SSH over port 22222 to the RaspberryPI host.
I follow the procedure to import the authorized_keys via USB but all the time i get the same problem
Conection Refused!!!

For me that´s means that the Raspeberry host not allow SSH conection over this port!
:frowning: I just spend hours to implement but no clear information or idea about what is the missing point here…

#!/usr/bin/python3
import imaplib
box = imaplib.IMAP4_SSL('imap.gmail.com', 993)
box.login("USER-NAME-HERE","PASSWORD-HERE")
box.select('Inbox')
typ, data = box.search(None, 'ALL')
for num in data[0].split():
   box.store(num, '+FLAGS', '\\Deleted')
box.expunge()
box.close()
box.logout()

Thank´s for sharing but this is not valid for me :frowning:
I need something to shutdown my Synology Nass from the HA

Hi, I use a simple ssh shell and sshpass to shutdown my Synology Nas.

#! /bin/bash
sshpass -p "thepassword" ssh -t  [email protected] "echo thepassword | sudo -S poweroff"

Just replace thepassword and user by your own credentials

Are there any prequisites to do on the NAS?
I typed in the command, there were no error messages, but nothing happened with the NAS

NAS Side:

Make sure the SSH service is enabled on your NAS
Control Panel > Terminal & SNMP > Terminal > Enable SSH
Make sure the user you use has admin role

On HA side:
First of all try just a normal SSH session to the NAS from your HA shell.
Most probably you will first be prompted to accept the NAS as a valid host (security fingerprint)
After this fingerprint acceptation the sshpass command should work fine

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