Unable to send SSH commands

So, I’ve been trying to get this to work for a couple of hours without success.

I’m running hassio on a Raspi3 b and I try to turn my Ubuntu HTPC off with a shell command.

switch:
    platform: wake_on_lan
    name: HTPC
    mac_address: "50:E5:49:55:B7:A1"
    host: 192.168.1.5
    turn_off:
        service: shell_command.turn_off_htpc        
        
shell_command:
    turn_off_htpc: "ssh [email protected] 'sudo poweroff'"

I’ve followed this tutorial: https://home-assistant.io/components/switch.wake_on_lan/

If i use Putty on my Windows computer to access my Raspi (hassio system), and then, from within the terminal on hassio execute these two commands:
ssh [email protected]
sudo poweroff
Then my HTPC turns off.

But when I try to use the switch I’ve set up, nothing happens.
What am I doing wrong?

Don’t you need a password for your SSH user?

Follow this guide.
https://megamorphf.github.io/homeassistant/hyperion/ssh/hassio/2018/01/22/controlling-anything-via-ssh.html

3 Likes

Thank you so much @oriolism ! I got it up and running now :smiley:

@Gab, can you remember what you did? The link to the guide that @oriolism posted isn’t working anymore.

I had a similar, if not the same, issue. I followed every guide on setting up ssh keys I could find and none would work.

In the end, I was advised that I should be using authorized_keys rathar than known_hosts. As soon as I got that up and running, it worked from hassio.

This is a command I have that checks one of my pi’s power status.

ssh -i /config/ssh_key [email protected] cat /sys/devices/platform/soc/soc:firmware/get_throttled

The -i /config/ssh_key part refers to my private key that I have saved in my HA config directory. The rest is the same as always.

On the pi that I’m sending the command to, in ~/.ssh I have a file called authorized_keys and that has my public key in it.

@DavidFW1960 helped me out with this one. He might be able to check what I’ve written here to make sure I outlined it correctly.

3 Likes

Thanks amigo!

1 Like

How did you create the ssh_key on the Hass.io server

Thanks Rien

This was a while ago now, but from memory I created the keys on another server of mine and copied them across.

It took me some time to get this to work. Since the instructions page above is not working anymore I hope these instructions will help the next person that tries to get this to work :slight_smile:
What I want to get out of this is the option to shutdown or restart a remote pi using a HA automation.

- On your HA open the terminal and give this command:
ssh-keygen -t rsa -C "[email protected]"
When it asks for the file path you want the key to be saved to use: /config/ssh_key

image

- Open the ssh_key.pub with a text editor and copy its content.
- On the remote system edit the authorised keys file:
sudo visudo ~/.ssh/authorized_keys
- Insert the key from the ssh_key.pub and save the authorized_keys.
- restart the ssh service on the remote machine
sudo service sshd restart

- To test if this works go to the HA file editor, click the gear icon in the top right of the screen and select “execute shell command”. Try this command (with the correct IP address ofcourse):
ssh -i /config/ssh_key [email protected] "hostname"
This should return the hostname of your remote system.

In your config file you can now create a service for your shell command that can be used by your automations:
</s></s> <s><s>shell_command:</s></s> <s><s> hyperion_shutdown: 'ssh -i /config/ssh_key [email protected] "sudo shutdown"'</s></s> <s><s>
that part doesn’t work… yet

Got it fixed using this guide: