Shutdown PC via ssh from HA to a Win11 box

Hi

Trying to run a ssh shell command (by script) from my HA (running on a RP5) to a Win11 box, to shut down the Win11 box.

Running this from HA terminal shuts down the PC:
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /root/.ssh/id_ed25519 [email protected] shutdown /s

But running the below script to run this shell command does not work:

Shell command:

shell_command:
  turn_off_yourpc: 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /root/.ssh/id_ed25519 [email protected] shutdown /s'

Script:

alias: "Shutdown: Comp Room PC"
sequence:
  - action: shell_command.turn_off_yourpc
    data: {}

This is the error in HA log

Logger: homeassistant.components.shell_command
Source: /usr/src/homeassistant/homeassistant/components/shell_command/__init__.py:129
integration: Shell Command (documentation, issues)
First occurred: 9:14:34 PM (1 occurrences)
Last logged: 9:14:34 PM

Error running command: `ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /root/.ssh/id_ed25519 [email protected] shutdown /s`, return code: 255
NoneType: None

You don’t have a “/root” inside HA.
Your private key must be under “/config”.

Mine does :slight_smile:

Also tried in /config/ - same error unfortunatly…

Hummm… not sure if you are suggesting something like this? No error (in HA log), but does not appear to do anything…

command_line:
  - switch:
      name: Computer Room PC Shutdown
      command_off: 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /root/.ssh/id_ed25519 [email protected] shutdown /s'

Home - Hass Agent Documentation is an alternative.

1 Like

Thank you… I had seen that - however, I must be close, as it works from terminal :slight_smile: So would be nice to be able to do this as a command line command.

I meant that unless you use the Core installation (pure python), the “/root” of HA is not the same that on the host or on the Terminal addon.

Do you use Core?

I am not sure what the ‘Core’ installation is…

I did start with the key in the /config/.ssh/ folder - when I am home from work, I will try /config/.ssh again… Thank you!

Changed to /config/.ssh/

From Terminal, it works.

From a shell command, unfortunately it still errors:

Logger: homeassistant.components.command_line.utils
Source: components/command_line/utils.py:36
integration: Command Line (documentation, issues)
First occurred: 5:16:56 PM (1 occurrences)
Last logged: 5:16:56 PM

Command failed (with return code 255): ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /config/.ssh/id_ed25519 [email protected] shutdown /s