Failing to execute an SSH command in automation

Hello!

I’m using Hassio

Quite some trouble setting this up, what i want to achieve:
Have an input boolean and an automation for when it turns on and off;

on = WOL ( this works to wake up my computer )
off = ssh and suspend ( this works via hassio cli with ssh keys setup )

what i’ve tried so far are shell_commands (also indirect calling a bash script) but i get an error code 255, command line switch fails also with no additional info in the logs.

Could anyone shine a light?
Cheers!

1 Like

Can we see your shell commands and automations?

For sure, forgot to post it, sorry. After work tomorrow ill post it. Thanks

So this is the command i can run from CLI within Hassio but not via the above mentioned methods.

/usr/bin/ssh -i /data/.ssh/my.key [email protected] 'systemctl suspend'

Can you please post your actual shell_command config?

Thanks guys.
This is in configuration.yaml:

shell_command: !include shell_commands.yaml

This is my shell_command.yaml (someone mentioned to have 1 space at the beginning of the line):

 create_keys: ssh-keygen -t rsa -N "" -f my.key

 laptop_suspend: "/usr/bin/ssh -i /data/.ssh/my.key [email protected] 'systemctl suspend'"

tried it with and without double quotes and a valid IP ofcourse.

Is anything logged when you run the command?

shell_commands: error code 255,
command line switch: fails with no additional info in the logs.

Take a look here Hass.io shell_command error

2 Likes

You’re my hero!!! Not at home until Wednesday but will try it out asap. Thanks bro!

Unfortunately just tested it, and it still fails with error code 255. Debuggin now.

Edit: Don’t know what went wrong, had to toggle the switch in the front end about 5 times (out of frustration) and now it works!

Thanks again.

So I was having the same issue trying to send an ssh command to restart another rasp pi I have. Here is what I found.

I think the answer is to issue the ssh key within the home assistant environment and also adjust the command to run as home assistant environment.

My Setup: Raspbian Buster and I have my home assistant running in it’s own environment. I am sorry if I use any terms incorrectly, I am new to Linux and don’t understand everything. Just wanted to share what I did to get mine to work.

Step one: Setting up SSH Key (I did this both on the homeassistant pi and in the environment)

ssh-keygen -t rsa

Then I left the RSA ID blank and pressed enter, left password blank and pressed enter, left confirm password blank and pressed enter.

I then copied the key to the machine that I watned to run the command on. Pi below is the username of the other machine.

ssh-copy-id [email protected]

Verify that the ssh connection works without prompt for password. You should now have an ssh connection to the other pc

ssh [email protected]

type exit to exit the ssh connection.

Now I do the same step above but under the home assistant user / environement

sudo -u homeassistant -H -s
ssh-keygen -t rsa

Enter, Enter, Enter past the ID name, Password, confirm password

Now copy the key over to the other machine you want to issue commands on. I also copied the key over to the PI where my home assistant is running. Not sure if I needed to but since it is running in an evironment I did any ways. The 10.0.0.128 is where homeassistant is installed and running.

ssh-copy-id [email protected]
ssh-copy-id [email protected]
exit

Now you should be signed in as the pi user and not the home assistant user

SSH Key’s are finished.

Add line to Configuration.yaml /home/homeassistant/.homeassistant

cd /home/homeassistant/.homeassistant
sudo nano configuration.yaml

Then add in the following line. I added it in at the bottom.

shell_command: !include shell_command.yaml

Save the Config

Now we need to create the shell_command.yaml file

sudo nano shell_command.yaml

I added the follow in the shell_command file

reboot_test: sudo -u homeassistant -H -s ssh [email protected] sudo reboot

Now we need to create the HA script that issues the command

sudo nano script.yaml

I added in the following

reboot_test:
 alias: Reboot Test
 sequence:
  - Service: shell_command.reboot_test

Tested and it now works.

I think the key is to run the ssh command as the homeassistant user or in the homeassistant environment. Not sure what term is correct.

4 Likes