Running a shell command from Home Assistant to remote linux PC

@Tinkerer I have try to lunch form HA Raspberry a script into my MacMini with this code:

  - platform: command_line
    switches:
      telecamere_switch:
        command_on: "ssh -i /config/ssh_keys/id_rsa -o StrictHostKeyChecking=no [email protected] 'sudo -u leo osascript /Users/leo/Desktop/Scripts/ArmaTelecamere.scpt'"
        command_off: "ssh -i /config/ssh_keys/id_rsa -o StrictHostKeyChecking=no [email protected] 'sudo -u leo osascript /Users/leo/Desktop/Scripts/DisarmaTelecamere.scpt'"
        friendly_name: Telecamere

But HA log say:

Logger: homeassistant.components.command_line
Source: components/command_line/__init__.py:23
Integration: command_line (documentation, issues)
First occurred: 15:20:13 (1 occurrences)
Last logged: 15:20:13

Command failed: ssh -i /config/ssh_keys/id_rsa -o StrictHostKeyChecking=no [email protected] 'sudo -u leo osascript /Users/leo/Desktop/Scripts/ArmaTelecamere.scpt'

See the discussion on Discord :wink:

  1. You’re connecting as leo - why are you using sudo to become leo?
  2. Have you confirmed that the SSH key works?

I tried removing “sudo” but it always asks for the password. i think at this point it is an ssh key problem. Is there a guide to follow to configure correctly?

Quoting from a response to somebody else the other day:

When using keys with SSH the host you’re connecting to needs to be told to accept a key
Typically that’s done by adding the public key to the .ssh/authorized_keys file to the specified account on the remote host
There should be quite a few good guides on this findable via any search engine:
https://www.cyberciti.biz/faq/how-to-set-up-ssh-keys-on-linux-unix/
How To Set Up SSH Keys on Ubuntu 12.04 | DigitalOcean
What is an SSH Key? An Overview of SSH Keys

Thanks now if I type in HA terminal:

ssh [email protected] 'osascript /Users/leo/Desktop/Scripts/ArmaTelecamere.scpt'

Now work from terminal… But if I use in configuration.yaml:

- platform: command_line
    switches:
      telecamere_switch:
        command_on: "ssh [email protected] 'osascript /Users/leo/Desktop/Scripts/ArmaTelecamere.scpt'"
        command_off: "ssh [email protected] 'osascript /Users/leo/Desktop/Scripts/DisarmaTelecamere.scpt'"
        friendly_name: Telecamere

Don’t Work:

2020-10-23 22:48:42 ERROR (SyncWorker_11) [homeassistant.components.command_line.switch] Command failed: ssh [email protected] 'osascript /Users/leo/Desktop/Scripts/ArmaTelecamere.scpt'

Why

The

Heck

Did you remove all the arguments to ssh? I didn’t say to do that… I said to remove the sudo -u leo part

1 Like

Thanks Tinker, I have try:

ssh -i /config/ssh_keys/id_rsa -o StrictHostKeyChecking=no [email protected] 'osascript /Users/leo/Desktop/Scripts/ArmaTelecamere.scpt'

Is the same…

You have the ssh-key in the correct file of your Mac?

If you replace the command with just date does it work?

If not, the problem is with the key

Thanks for your answers I think I have installed the keys correctly, in fact if I try from the HA terminal to access SSH to the MacMini it works and does not ask me for a password. I needed to access via SSH to control the cameras via SecuritySpy software. I found a specific plugin and solved it. Thanks to everyone anyway!

Hi guys, totally new here.

I’m running HA as supervised (hassio) install on Docker on Raspbian (RPI 4A). As I use the RPI for gaming (steamlink) as well as media center (kodi) I need a way to switch between those when needed. I figured out that ssh’ing to console to run commands will be the best option here. I have ssh setup with personal keys already from my win10 PC. I have generated public key via hassio terminal as well. Even though I could not localize root folder somehow, I used cp to copy the id_rsa.pub to config/ssh. I have added hassio public key to RPI ssh known_hosts. I can ssh from hassio terminal without any problems (no password needed as I disabled it). When running shell_command from automation it does not work though.

I have tried different commands like:

All of above result with Error 255. When running from hassio terminal simple ssh [email protected] kodi works straight away.

I suspect it has something to do with user that is running those commands. I presume that running those from hassio terminal makes me acting as root (I think it is confirmed in ssh public key that I generated from terminal as it has root@core-ssh as acting user at the end of key. Automation will not run as root as I think :smiley: . The thing is I have no idea what is the user that runs shell_command in automation. I have tried find something useful in Portainer data for both hassio_core and homeassistant containers to no avail.

Any hints to where should I place my next steps? Also sorry for this terribly long post.

Please see point 16 in here :wink:

I clearly didn’t follow the rules as I should, sorry for that. Edited previous post :slight_smile:

Odds are high that the problem is your use of the public key in those commands :wink: The public key goes to the authorized_keys file on the remote host, the private key is the one you use when connecting.

1 Like

Thanks for quick reply! And well… that’s a really good point! :smiley: Networking is total magic to me.

I have cp’ed the private key to /config/ssh folder and tried to run commands again with it. Though again Error 255 pops up. I have wondered whether quotation marks have something to do with that as I have seen mentions on that above (though as I’m running simple “kodi” command I guess that no quotation marks should be required, right?), but that’s still not that.

Examples of errors I receive when running those:

[homeassistant.components.shell_command] Error running command: `ssh -i /config/id_rsa -o 'StrictHostKeyChecking=no' [email protected] `kodi``, return code: 255 NoneType: None
[homeassistant.components.shell_command] Error running command: `ssh -i /config/id_rsa -o 'StrictHostKeyChecking=no' [email protected] 'kodi'`, return code: 255 NoneType: None
[homeassistant.components.shell_command] Error running command: `ssh -i /config/id_rsa -o 'StrictHostKeyChecking=no' [email protected] kodi`, return code: 255 NoneType: None
[homeassistant.components.shell_command] Error running command: `ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /config/.ssh/id_rsa [email protected] kodi`, return code: 255 NoneType: None
[homeassistant.components.shell_command] Error running command: `ssh -o UserKnownHostsFile=/ssh/known_hosts -o StrictHostKeyChecking=no -i /config/.ssh/id_rsa [email protected] kodi`, return code: 255 NoneType: None

When I look at above there seems to be no sense in quotation marks as I still cannot go through validation phase to run those anyway.

Did you add the public key to the authorized_keys file for pi on the remote host?

Where is your private key? You seem to be confused about where it might be.

Gosh, please disregard above. In the mess of the code I messed up the path to priv key. Changed that to:

run_kodi: ssh -i /config/ssh/id_rsa -o 'StrictHostKeyChecking=no' [email protected] kodi

And now it works w/o problems :smiley:

Tinkerer thanks a lot for your help :wink:

1 Like

To recap so it might be useful for somebody looking for answer:

Supervised install running on Docker on RPI.

  • Keys generated in Hass.io terminal (not RPI terminal) (addon Terminal & SSH used here) via ssh-keygen command.
  • Keys copied to /config/ssh via cp /.ssh/id_rsa /config/ssh/id_rsa and cp /.ssh/id_rsa.pub /config/ssh/id_rsa.pub (in Hass.io terminal)
  • Pub key (id_rsa.pub) copied to PI’s home/pi/.ssh/known_hosts
  • SSH settings edited not to use password auth (note that if you use different machines to ssh to PI you will need to add them by public key as well, you will not be able to log in with password after that). Edit /etc/ssh/sshd_config with: PasswordAuthentication no
  • Command run with use of priv key (id_rsa) - in my case command is: run_kodi: ssh -i /config/ssh/id_rsa -o 'StrictHostKeyChecking=no' [email protected] kodi

Aaand that’s it I guess. Hope that I didn’t miss anything. Thanks again Tinkerer for help. I finally kinda understand how priv and pub keys in ssh work. Next task - figure out how to shut kodi down from terminal without ending up with black screen :smiley: . That’s totally separate topic though.

4 Likes

Hi, I also have home assistant installed on Docker running on RPI. I followed your steps and it worked perfectly when launching the following command from Terminal&SSH add-on:

ssh -i /config/ssh/id_rsa -o 'StrictHostKeyChecking=no' [email protected] sudo systemctl stop kiosk.service 

After launching the command, it prompts the RPI password and then it is executed correctly.

If I add the following to my configuration.yaml instead:

shell_command:
  close_kiosk: ssh -i /config/ssh/id_rsa -o 'StrictHostKeyChecking=no' [email protected] sudo systemctl stop kiosk.service

Nothing happens.
Maybe because password is missing in the shell command and it cannot prompt for it to execute the command ?

Did you copy the public key to the remote machine? Did you copy the private key to the home assistant directory?