Trying to reboot remote Windows PC from within Home Assistant - Brain Hurts

I’m running into a brick wall with this.

  • I’ve installed MobaSSH on my Windows PC and it’s running as a service at x.x.x.150

  • I have the SSH server plugin running and configured on my HA install on my Rpi 3 B+ at x.x.x.12

  • Logged into HASS.IO as root I have generated my RSA keys and saved the file to/config/id_rsa

  • I’ve copied my public key to my windows machine and verified I can SSH to that machine from my Rpi with [email protected] without being challenged for a password.

  • while logged in as that user, I have verified I can successfully execute ‘shutdown -r now’

However, when I try and run this same command from within Home Assistant, I get a 255/timeout error.

I have my shell command saved in shell_command.yaml and I can access it as a service command in the dev section of the UI. My shell command is…

reboot_windows: ssh -l -i /config/id_rsa <user>@x.x.x.150 "shutdown -r now"

Calling this service from the dev section results in a timeout

My script to call that service looks like this…

reboot_windows:
  alias: Reboot Windows Media Server
  sequence:
    - service: shell_command.reboot_windows`

Neither calling the service nor running the script results in a successful execution of the command on the remote machine.

Try adding the parameter -o StrictHostKeyChecking=no to the command.

Unfortunately no difference when adding this to the command

Strange I had the same symptoms some time ago. I ended up sshing in to hassio host and then running a shell in the home assistant docker container it’s self. From there I tested my ssh command. Maybe you can do the same. What happened for me is it asked for confirmation that the host key was correct. Adding the above switch prevented this.

Here’s an example of what works for me.

rebootrouter: ‘ssh -o StrictHostKeyChecking=no -i /config/shellcommands/id_rsa [email protected] reboot’

Does it make a difference that Home Assistant is trying to run the command rather than root on the Pi? Is there a possibility that a key generated by root would be no good when used by another system user - or does Home Assistant run as root already?

I’m not sure tbh. I don’t remember how I generated my key as it was so long ago. When you say you are sshing in as root, do you mean to port 22 or port 22222 of the hassio device? If you want to really test as the same context as home assistant you need to ssh to port 22222 (sd card will need keys on it) you can only auth to 22222 with keys and not password. Once you are in here you can use docker to run a shell in the home assistant container and test.

I ssh’d into port 22 as root when I generated my keys. I don’t have keys on my SD card for HA.

I removed -l from my command and received better info back from the Windows PC and it is definitely a permissions problem, executing this command from HA is not the same as executing from root it seems.

Command executed: ssh -o StrictHostKeyChecking=no -i /config/id_rsa [email protected] "shutdown -r now": 255
Warning: Permanently added 'x.x.x.150' (ECDSA) to the list of known hosts.
Permission denied, please try again.
Permission denied, please try again.
[email protected]: Permission denied (publickey,password,keyboard-interactive).

This is a good resource for more info https://megamorphf.github.io/homeassistant/hyperion/ssh/hassio/2018/01/22/controlling-anything-via-ssh.html

Yeah it looks like a key generated in HASS.IO is no good for use from within the docker container.

As i understand it on hassio home-assistant does not run as root. So trying to log into another PC with root’s key will not work.

Yes that’s what seems to be going on, I have to try and follow the article linked above so I can generate a key from within HA as that logged in user.

If you are running Hassio the rpc shutdown addon works well for me and wake on lan provides a nice wake up option.

This looks almost perfect for what I am trying to do, but can I do a restart with this? I don’t want to shutdown the machine, I want to perform a full restart with this command.

I don’t believe the addon supports this, sorry. You can let it shutdown and then start back up with the WOL switch but I don’t believe sending a restart command is available.

I previously had a setup where this was possible. Basically I had a command line script that created a ssh connection from the HA container to the host system and then called a “net rpc shutdown…” command directed at the Windows machine. The ssh part was only necessary because the net command was not available from within the HA container.

A different solution could be a dedicated tool running as a service on Windows that offers shutdown/restart/hibernate via HTTP call such as the first one from here https://www.raymond.cc/blog/how-to-remotely-turn-off-computer-from-lan-or-wan/

This might be an option too Mqtt Control - small mqtt client service to connect my windows based pc to home assistant

After chatting with a mod in Discord today, we came up with a modified version of RPC shutdown which instead sends a restart command, works great. If anyone wants a copy just let me know.

1 Like

Yes, please :raising_hand_woman:

@griffindodd hello I would also be interested. Thank you in advance

Trying to get this for you guys, but I can’t remember which directory it’s stored in on my install. Where do you put local addons so they show in the addon section of the admin area? DOH!!