Use SSH shell command in a switch

Hello,

I would like to be able to switch my Proxmox server on and off using a switch.
This is basically not a problem.
Switching on should work via WakeOnLan, switching off should be done via SSH.
I have created the following shell command for this purpose:

shell_command:
   shutdown_proxmox: 'ssh -i /config/.ssh/id_rsa -o StrictHostKeyChecking=no [email protected] "/sbin/shutdown -h now"'

The corresponding script is added:

shutdown_proxmox:
  alias: Shut down Proxmox server
  sequence:
  - service: shell_command.shutdown_proxmox

I use a ping sensor for the state of the switch.

And this is what the corresponding switch looks like:

      switch_proxmox:
        friendly_name: "Switch Proxmox server on/off"
        value_template: "{{ is_state('binary_sensor.proxmox', 'on') }}"
        turn_on:
          - service: wake_on_lan.send_magic_packet
            data:
              mac: '70:85:C2:83:3D:B2'
        turn_off:
          - service: script.shutdown_proxmox

The problem is that the shutdown does not work. I get the error 255 back.
But I have already created SSH keys and imported them on the Proxmox server.
When I use the command:
ssh -i /config/.ssh/id_rsa -o StrictHostKeyChecking=no [email protected] “/sbin/shutdown -h now”
directly on the Home Assistant terminal and this works without any problems.

I don’t know what to do.

I assume the backtick at the end of the shell command is a c/p typo?
Note that the intermediary script step is unnecessary. you can call the shell_command directly form an action.

that was an error, last should be ’

How can i directly call a shell command from an action?
What would be the best way?

        turn_off:
          - service: shell_command.shutdown_proxmox

Okay, i removed the script and now i use the shell command directly in the switch.
But the result ist the same:

Error running command: ssh -i /config/.ssh/id_rsa -o StrictHostKeyChecking=no [email protected] "/sbin/shutdown -h now", return code: 255

When you are in the terminal you are not in the same environment that HA runs in, HA runs within a docker container. So from your terminal session you need to use docker exec -it to get a shell session within the HA container. This will then be the same environment and you can test your command from the shell. This will tell you if the problem is the command (e.g. the ssh keys are not there).

How can i copy my key files to the docker container?

I have found the solution.
I first had to install the SSH Community Addon.
From there I opened a shell in the homeassistant container and then recreated and copied the keys.

I know you’ve solved it in a somewhat opaque manner, but part of the issue could be that the connection terminates when you execute the shutdown. This is then seen as an abnormal situation by your SSH client. My workaround is to use a shell script with my SSH command in there, redirecting errors to /dev/null and returning a 0 status/exit code.

So divided up as I had it at the beginning?
How should my script look like then?

As long as your script exists with a 0 you’ll be good.

In my case, I just echo something I use for debugging, e.g.:

#!/bin/bash

/usr/bin/ssh $1 'sudo shutdown -h now'
echo "Shutting down $1"

In my case, I pass the host as a parameter.

shell_command:
  shudown_remote_host: ".../scripts/shutdown_remote_host.sh {{ host }}"