Updated on 3/14/2025

Remote NAS Shutdown

In the event of loss of power, I want to gracefully shutdown my Synology NAS's, while my UPS still has battery capacity.

However, there are two catches.

  1. SSH expects an interactive session. SSH prevents users from supplying a password via a script by using direct TTY access.
  2. You can not loggin to a Synology NAS as root (since DMS 6.2-23739), and only root can issue a shutdown command.

You can get around the first problem by installing sshpass.

sudo apt install sshpass

This allows you enter the password on the command line. Sshpass uses a deicated tty. Windows user can use plink, which is included with putty.

The syntax is:

sshpass -p your_password ssh-command

However, if the remote host is not in the client's ~.ssh/known_hosts file, you will receive a prompt asking you if you want to accept the remote host's fingerprint and continue.

To get arround the prompt, you can use the ssh option, -o StrickHostKeyChecking.

That is:

sshpass -p your_password ssh -o StrickHostKeyChecking=no user_name@IP_Address

ssh also alows, you to issue a command on the same line as the loggin. We can use this, to change the shell to a root shell. The command is:

sudo -i

But, using the sudo command prompts you for a password.

You can not prevent the prompt, but can you pipe the password into the sudo command using echo and the sudo -S option, which directs sudo to get the password from the standard input.

echo "your_password" | sudo -i -S

Finally, the sudo command allows you to optionally issue another comnand on the same line.

The finally command to shutdown a Synology NAS is:

sshpass -p your_password ssh -o StrickHostKeyChecking=no NAS_user_name@NAS_IP_Address 'echo "password" | sudo -i -S shutdown now -h'

In summary:

sshpass - Allows you to login to shell from a script.

StrickHostKeyChecking - Is an SSH option that enabled by default. It verifies the key supplied by the server is in the clients "known hosts" file (.ssh/known_hosts).

ssh user@ip_addreess 'command1; comand2' - To ssh into a host and immediately execute commands surround them the either single quote or double quotes [5].

sudo -i or sudo --login - simulate a login into the root account.

sudo -S or sudo --stdin - Write the prompt to the standard error and read the password from the standard input instead of using the terminal device.

Implementation and Code

I decidd to implement this using a Raspberry Pi Zero 2W that is UPS backed up. It executes a root cron job every 3 minutes. It monitors (pings) two non-battery Pi Zero 2W that are used as wireless printer drivers.

The cron job code is below:

!/bin/bash
count="0"
if ! (ping -c 1 -W 1 192.168.37.32 >/dev/null); then
  echo "$hostname_or_ip_address is dead"
  count=$[$count+1]
  sleep 60
  if ! (ping -c 1 -W 1 192.168.37.32 >/dev/null); then
     count=$[$count+1]
  fi
fi

echo "The value of count is $count"
if  (( $count==2 )); then
    echo "shuting down"

    # shutdown NAS's
    # the damn "$" in my password, again, cost me time 
    sshpass -p'1819$passord' ssh -o StrictHostKeyChecking=no 1819-username@192.168.xx.4 \
    'echo "1819\$password" | sudo -i -S shutdown -h now'

    # shutdown computers
    sshpass 31-password ssh -o StrictHostKeyChecking=no 31-username@192.168.xx.31 \
    'sudo shutdown -h now' 
fi
            

My cronjob script is:

*/5 * * * * /Full_Path_to_Script
           

References:

  1. How to shutdown a Synology DSM 6 NAS using a script
  2. Poweroff Linux based NAS (Synology, ecc) remotely from Windows by command line
  3. SSH password automation in Linux with sshpass
  4. Understanding SSH StrictHostKeyChecking Option
  5. SSH Command - Usage, Options, Configuration
  6. SSH: Execute Remote Command or Script – Linux
  7. Bash Single vs Double Quotes: Key Differences Explained
  8. Stack Overflow - What is the cleanest way to ssh and run multiple commands in Bash?
  9. What are the differences between "su", "sudo -s", "sudo -i", "sudo su"?
  10. Synology Community -Automatically logon with root access via SSH to remotely shutdown Diskstation