Running a shell command from Home Assistant to remote linux PC

Hello, unfortunately nothing helps to me. I am running preconfigured HA in virtualbox and it seems that .ssh and config folders are on different place. Seems like .ssh or /.ssh are not found by shell script.

To be expected, that’s why you have to put the SSH key in the config folder, and use the command line that tells it where to find the key.

1 Like

I can send SSH commands through Terminal in HA (via the Terminal/SSH add on), but I cannot get any of these service calls to work. Any help would be much appreciated!!

This is in my configuration.yaml:

shell_command:
  restart_pi: "ssh -l pi 192.168.1.74 'sudo reboot'"
  reboot_test: ssh -l [email protected] 'sudo reboot'
  test3: ssh -i /config/ssh/id_rsa -o 'StrictHostKeyChecking=no' [email protected] sudo reboot
  test4: ssh -i /config/ssh/id_rsa -o 'StrictHostKeyChecking=no' [email protected] "sudo reboot"
  test5: "ssh -i /config/ssh/id_rsa -o 'StrictHostKeyChecking=no' [email protected] sudo reboot"
  test6: "ssh -i 'StrictHostKeyChecking=no' [email protected] sudo reboot"
  test7: "ssh -i 'StrictHostKeyChecking=no' [email protected] 'sudo reboot'"
  test8: ssh -i /config/ssh/id_rsa -o StrictHostKeyChecking=no [email protected] 'sudo reboot'

Well, only four of those have any hope of working. The ones that specify the path to the private key and that turn off the checking.

When it fails there should be some information in the log file detailing the problem.

I try to stop and start the surveillance package on my Synology via shell command from home assistant

Shutdown of my NAS is working perfect with

ssh -i /config/.ssh/id_rsa -o StrictHostKeyChecking=no [email protected] "echo password| sudo -S poweroff"

but if I do the same with following I get an error

ssh -i /config/.ssh/id_rsa -o StrictHostKeyChecking=no [email protected] "echo password| sudo -S synopkg stop SurveillanceStation"

error:

Password: sudo: synopkg: command not found

I’m not sure what I have to change either on the NAS or on my HA

If I logvi via ssh from my home assistant and using home_assistant_ssh as a user I can exectue the stop/start

The command “synopkg” can not be found!
You can simply try to use the full path for this command.

2 Likes

I tried but the same problem
If I log in with this user it works

You tried with the path from „/“ for synopkg?

Add full Path to synopkg (which synopkg should show the full path)

That is what I wrote here: https://community.home-assistant.io/t/running-a-shell-command-from-home-assistant-to-remote-linux-pc/135221/81
:slight_smile:

still the same issue
I guess it is more a rights problem with the ssh login

ah. I had the wrong path. seems to work now

Good to hear!

Hi! thank you for youe post, I’m trying to follow, and in stage 4 I’m getting this error:

bash-5.1# ssh-copy-id -i /config/ssh/id_rsa.pub [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/config/ssh/id_rsa.pub"
mktemp: (null): No such file or directory
/usr/bin/ssh-copy-id: ERROR: failed to create required temporary directory under ~/.ssh

any idea what can be the sorceof the problem?

thanks!

Is pi a root user?
You could try and mkdir ~/.ssh first on the remote machine. I don’t remember if it already existed.

I would like to add, if someone is still encountering issues:

In my case the shell command couldn’t resolve local hostnames.

The terminal add-on could ssh to pi@raspberrypi, but the shell command was giving errors. Checked the log (debug) and saw that I had to use [email protected].***

Very Helpful!!! works!

shutdown_pi_103: ssh -i /config/ssh/id_rsa -o UserKnownHostsFile=/config/ssh/known_hosts [email protected] 'sudo poweroff' 2> /config/command.log is for debugging

Gives me an error in my command.log: “Too many arguments.”

@Nikotine thanks so much!! This was really bugging me for a long time and finally the perfect instruction! One hint: At first it still didn’t work, but than I restarted the HA container completely (not only reloading the configuration.yaml) and THEN it worked!!

I had the same problem with “ERROR: failed to create required temporary directory under ~/.ssh” and came across this solution that seemed to work for me:

Apparently ssh-copy-id when its mktemp sees ~/.ssh it thinks that means /root/.ssh, so you just need to create directory .ssh under /root, that solved it for me

(From thread on Stack Exchange)