[Solved] Run ssh command in Hass.io

Hi there,
Im Running Hass.io on HassOs on my raspberry pi.

Unfortunately I have the problem that I can’t run an ssh command from HA (using ssh adding and running it there works fine).

For ssh I’m using an certificate without password.

Already tried running it from within an python file, which results in this error:

2019-07-23 13:27:15 ERROR (SyncWorker_3) [homeassistant.components.python_script.roborock_floor1_mop.py] Error executing script: __import__ not found
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 159, in execute
    exec(compiled.code, restricted_globals, local)
  File "roborock_floor1_mop.py", line 1, in <module>
ImportError: __import__ not found

Running it as a shell_command inserting the ssh command directly there isn’t working, too.

roborock_floor1_mop: ssh -i ../config/ssh/id_rsa [email protected] 'sh ../mnt/data/valetudo/maps/floor1.sh'

Result in the log:

2019-07-23 13:25:44 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `ssh -i ../config/ssh/id_rsa [email protected] 'sh ../mnt/data/valetudo/maps/floor1.sh'`, return code: 255

Does anybody has any idea what could be the problem here? I’m looking and trying all solutions for this here but unfortunately nobody is using HassOS :confused:

You’re confounding several things here.

Hassio (Home Assistant) is running in its own container. That is where you problem lies. Not the SSH add-on (which is a completely different container), and not the host OS (HassOS). HassOS isn’t making the SSH call, your hassio container is.

Focus on that, and not HassOS.

Thanks, maybe I’ve written down it a bit confusing.

I know, that the ssh add on is a different container.

But isn’t the Hass.io container not able to ssh to another client?

Found the solution :slight_smile:

shell_command:
  roborock_floor1_mop: 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /config/ssh/id_rsa [email protected] "bash ../mnt/data/valetudo/maps/floor1_mop.sh"
2 Likes

I am also fighting with the same problem.

I have a shell script which is executing wonderfully in terminal but nowhere else.

I tried the hint from hagenuck and some others from felt 1000 websites since the last 6 hours.

This is what I did:

shell_command:
  volspotreconnect: 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /config/id_rsa [email protected] sudo systemctl restart volspotconnect2.service'

Absolutely no chance to get this working in the UI (in the File Editor it works).
Not with hassio.addon_stdin, not with the shell_command not in an automation or in Node-RED. Nothing. Put it in a shell file, chmod +x and it runs wonderfully in terminal. Granted all access for testing: Nothing. Not with core_ssh or with the ssh addon.

May anybody help me out here? I am running mad at the moment :frowning:

Thank you!

DUDE! You just resolved my 48 hour nightmare. THANK YOU.

I’ll add my own shipwreck here as a lighthouse to others. You can’t put this in a Bash script and call it with shell_command - I couldn’t get that to work. Without the ability to log in to Hassio outside the Docker container, I could not find a way to path ANY .sh file such that it worked. Here’s my working snippet from configuration.yam:

switch:
  - platform: command_line
    switches:
      mute_tv:
        command_on: ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /config/.ssh/id_rsa [email protected] "bash /home/pi/mute.sh"
        command_off: ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /config/.ssh/id_rsa [email protected] "bash /home/pi/mute.sh"
        friendly_name: Mute

I copied my id_rsa and id_rsa.pub keys from /data/.ssh to /config/.ssh

So wish Hassio would allow us to SSH into the pi itself instead of the docker container. I know that opens up a can of risk but damn this shouldn’t have been so hard.