SSH from shell_command

So I have 2 pi’s, one running hass.io and the other running Lovelace in kiosk mode. I want to have a shell_command SSH into the kiosk pi and restart it.

I generated keys and have them on both devices, and I can get it working with a SSH command typed into the GUI from the SSH addon in home assistant. I figured that would mean it would work as a shell command, but unfortunately I’m getting a 255 error in the log when I trigger it.

shell_command:
  kiosk_reboot: 'ssh [email protected] sudo reboot'

Again, the above command in quotes by itself run on the GUI of the SSH add-on within home assistant works perfectly.

Error:
Error running command: `ssh [email protected] sudo reboot`, return code: 255

NoneType: None

What am I missing?

1 Like

ssh will most likely require a password, which HA won’t be able to enter.
You need to create an ssh key that is saved locally so it doesn’t get lost with updates:
https://www.ssh.com/ssh/keygen
Since you’re on hass.io, you’ll need to ssh to the pi as the HA user to do this

I did that from the SSH addon gui - is that not the HA user?

I’m not sure. Also where did you save the RSA key? if you save it in the default location, you’ll lose it every time HA updates, so you need to save in a similar location to the way your config is saved

here is what i use in my shell commands

/usr/bin/ssh -i /home/hass/.ssh/id_rsa <user>@<ip> '<command>'

this is after you set up the ssh keys
you will also have to add that user to the sudoers file

Ok I’ll try saving it in the location noted in your command and then I’ll use your command. Thanks!

Hello,
I have the same issue.

ssh works by GUI but not working with shell_command or command_line.

ssh -i /config/.ssh/id_rsa -o StrictHostKeyChecking=no -q [email protected] sensors

I think that “command” not includ id_rsa.
But I don’t know how to fix

UPDATE: No longer working! SSH updated and now v.1 is no longer supported. No idea what that means but it breaks everything.

Finally made progress.

Generate SSH public/private key pair (just Google it. No need to download anything extra for mac or windows, there is a terminal/command prompt command. It will generate 2 files.

Place both files in /config/ssh_keys on the Hassio pi (Create folder if needed)

CHMOD the key using this: chmod 600 ~/config/ssh_keys/id_rsa (CHANGE DIRECTORY AS NEEDED)

Add public key to the second pi (not Hassio one)

  1. SSH into it via terminal: ssh pi@###.###.#.###
  2. Once you’re in, run: nano ~/.ssh/authorized_keys
  3. Go to the bottom of whatever keys are in there and paste in the public key text (open one of the two files you generated earlier… the public one won’t say private at the top. Pasting is actually really hard! Just keep trying different key combos, Google it. I think ctrl + u ended up working finally)
  4. Save that file (I think ctrl + X, but it should say at the bottom)

Now you can run the following command on a shell script:
ssh -1 /config/ssh_keys/id_rsa -a StrictHostKeyChecking=no pi@###.###.#.### sudo reboot

3 Likes

Change the command to:
ssh -i /config/ssh_keys/id_rsa -o 'StrictHostKeyChecking=no' pi@###.###.#.### sudo reboot

Got it from here: Running a shell command from Home Assistant to remote linux PC

3 Likes