Run ssh script remotely

Hi All,

Bit of background. I want to have a script in Home Assistant that opens my garage. My first PI is running hassbian and my second pi is just running raspbian. I’ve generated an SSH key for the root user in the docker container in hassbian and I can run my script and open the garage ok. When I press the button in the UI it does run as it logs out the time to the log but doesn’t seem to be able to run the ssh command.

SSH log on Raspbian:
Oct 1 14:36:31 pi2 sshd[2899]: Connection closed by 192.168.0.60 [preauth]

It’s like it isn’t using the generated SSH key. The only user that seems to be running anything in the docker container is root.

End of configuration.yaml

shell_command:
 open_garage: "/config/open_garage.sh"


group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml

scripts.yaml

'1506830366443':
  alias: open_garage
  sequence:
  - alias: Open Garage
    service: shell_command.open_garage

open_garage.sh

 #!/bin/bash
 date >> /config/open_garage.log
 ssh [email protected] -p 11588 sh n

So i added a whoami to echo out to that log file and it is running as root. So why isn’t it using root’s key?

root
Sun Oct 1 15:28:28 AEST 2017

1 Like

even pointing it directly at the private key doesn’t work…
ssh -i /config/.ssh/id_rsa [email protected] -p 11588 sh n

For the next poor person that struggles with this here is what I did.
Copy the /root/.ssh to /config/ssh

add a config file to that directory (stole this from someone here on the forums but I can’t find the thread again):
core-ssh:/config# cat ssh/config
ConnectTimeout 10
IdentityFile /config/ssh/id_rsa
UserKnownHostsFile /config/ssh/.known_hosts

modify last line of that script like so:
ssh -F /config/ssh/config -i /config/ssh/id_rsa [email protected] -p 11588 sh n

run the script once manually to accept the ssh key.

it now works for me from the webui

2 Likes

Hi there, I have a similar need, but I am using hassio.

What I need is hassio ----ssh—> pi3 , in other words, I need an Automation in Hassio to execute a command line in another Pi3 that I have.

If I understood correctly from your posts, this is what you did (but from your Hassbian), correct?

Hi vlamaranth
Did you manage to get anywhere with this ?
I have same issue. Using Hassio and need to run command on another Pi.
Have tried many attempts but missing something somewhere?
Thanks Dave

I am also searching for a solution for this

I kind of cheated, and used a REST Switch to turn something on and off via a command line on another pi: Created a REST Switch to control a Pi zero W running a calendar display

I needed (wanted?) status information on whether the display was on or off, so I used the REST switch. When you use shell_command with ssh, you get the response from the ssh command (did it make a connection or not, maybe the result of the command), not always the result from the command you ran with SSH, the REST API lets me do that.