I spent the better part of a month troubleshooting this issue and only today found the solution. Since I browsed dozens of posts here for an answer to what seemed like the exact same problem, but not finding the solution, I wanted to post this here so the next person doesn’t lose a month of their life trying to figure out why their SSH command won’t run in a shell_command script within HA despite seeming to run just fine from console!
So, here’s the setup:
- .sh script with an ssh command inside
- a shell_command integration added in configuration.yaml
- an Automation/Script calling that shell_command
My script ran just fine via SSH console (Advanced SSH & Web Terminal), but would simply do nothing when ran inside HA via script or automation. I saw the running_script: false in the Step Details that many others see in this situation.
I tried literally everything mentioned in every post I could find here, Reddit, etc. Nothing would get ssh to work. Then I happened upon a seemingly unrelated post that spawned a thought: HA is multi-layered, especially in the case of HAOS: Host > Supervisor > HA inside a container. The SSH Add-on places you in the Supervisor layer but HA is still running one layer deeper.
Yes, the shell script runs fine inside the Supervisor layer…but I never directly tried it within the HA container layer of this whole cake!
You have to disable Protected Mode in the SSH Add-on so you can access the docker command. Then, I ran this command to access HA’s container directly so I could try my shell script:
docker exec -it homeassistant bash
When I ran my shell script in there, it all became clear:
The authenticity of host 'blah-blah-blah (10.10.0.6)' can't be established.
ED25519 key fingerprint is SHA256:AintNoWayImShowingYouThis84f53485fo345f8y.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
While the key was already known within the Supervisor (I confirmed a similar prompt back when I was first testing), it was NOT yet known within the HA container, itself!!! Once I manually said yes to this prompt, my shell script started running just fine in my automation!
You will only have to do this step once for any given ssh host, regardless of how many scripts contact that host, as the key will be permanently stored in known_hosts within the HA container.
My quest is now complete. I genuinely hope this helps others avoid such a long journey.