Synology NAS - SSH Shutdown issue

Hi guys,
I can’t shutdown my Synology from Hassio GUI (HA 0.89.1).
My shell_command is:
turn_off_nas: ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /root/.ssh/id_rsa [email protected] '/sbin/shutdown -h now'

The command works fine in Terminal (SSH access without password on Synology NAS), but not from the switch.
I get this error:

Error running command:
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /root/.ssh/id_rsa [email protected] '/sbin/shutdown -h now', return code: 255 NoneType: None

I tried also to add this line at the bottom of the nas sudoers file:
admin ALL=(ALL:ALL) NOPASSWD: /sbin/shutdown

but nothing…
Help please?

1 Like

Why don’t you use a hass.io command?
Hassio ho shutdown ?

You won’t get access to the host with regular commands (mostly anyway)

Hi, thanks.
Ok, I found https://www.home-assistant.io/hassio/commandline/
but I’m confused…
How can I use this command to shutdown my Synology NAS? Hassio runs on Rpi.

Ah I misread it… for some reason I thought hass.io was running on your NAS.

Ok, don’t worry :wink:
The strange thing is that I was able to shutdown my Nas wis Hassbian, but not with Hassio.

The problem is with Hassio you don’t get access to a lot of functions outside the container… hassbian doesn’t care as it’s a full Linux district with full Linux… hass.io just doesn’t give you this…

The other week I was trying to run a bash script to delete email from my Gmail - couldn’t do it. I then found a Python script and it works… Maybe look on Google and see if you can find an alternate script that does the same thing…

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