Turn off PC via SSH - problem

Hi guys. I have a problem with shell_commands.yaml in Hass.io. I tried to create script that reboots Windows machine, just like in video:

https://www.youtube.com/watch?v=5ohqIY4uxMI&t=428s

… but this is a solution for hassbian. When i log into ssh, run moboSSH and reconnect - everything works fine.

The script for shutdown is:
test_name: ssh -l windows-user 10.10.10.220 “shutdown -r now” (eg. 10.10.10.220 is an IP for my PC. Connection test passes so my data (username + IP) are correct)

When I try to open MY script for restart WindowsWITHIN Hass.io - nothing happens (no errors, nothing). It’s strange because when I use the sequence in Putty - this script works and Windows is rebooted…
What’s going on? Maybe Hassio has different method for running shell scripts? Please help…

The problem is that hassio runs in a docker container. Docker containers, by design, don’t have default access to the host OS.

I figured that out too because I have a normal non-hassio installation running in docker andf none of my extermnal shell commands worked either.

I was able to grant access to the host from the container but be aware that it’s not really supposed to work tht way.

There was a procedure written by @juan11perez that I (mostly) followed to get mine working. I don’t use hassio but it should work for you too.

https://hastebin.com/sojasolite.sql

there were a couple of differences that I had to deviate from his procedure but googling around allowed me to figure things out.

The other resources I used were:

https://megamorphf.github.io/homeassistant/hyperion/ssh/hassio/2018/01/22/controlling-anything-via-ssh.html

https://wiki.mcs.anl.gov/IT/index.php/SSH_Keys:authorized_keys

Thanks I will try to do it this way and I will give feedback

I can’t speak to SSH to shutdown windows, but what I used was PSTools since my HA is running on a windows server, I was able to create a .bat file on the server that HA then calls psshutdown to remotely sleep any of the computers in the house.

I realize that this probably doesn’t exactly help you, as I don’t think there is a linux alternative to PSTools, but someone else may come along and be looking for a way to do this from windows -> windows

Thanks guys I thing this post “A thorough guide on controlling anything SSH with Homeassistants’s Hass.io” has all the answers, but after 3 tough days of fighting with SSH I’m stuck in connecting to port 22222 (port 22 works perfectly). I did everything just like in tutorial. I’ve created private / public keys and sent public key as name: authorized_keys inside root in SD Card (I guess this is roob - inserting my SD card in my PC shows only some files like boot, config etc and one folder Overlays - drive name hassos-boot. Some other people shows images with more folders. Maybe I don’t see everything). This public key authorized_keys has been copied good from Puttygen - there is no problem there).
After reboot Raspberry Pi I am still not able to open port 22222 (port 22 works well, as I mentioned before). Maybe I didn’t see the good root directory on SD Card? I don’t know I’m confused

It looks like this image - link

Again, I don’t use hassio but…

I’m not sure why you need to use port 22222. I’m pretty sure that’s the port the guy in the tutorial had set up for ssh. If your ssh works on port 22 then use that port to do the tutorial with .

Also there is no reason to pull your SD card out to do anything in the procedure. It’s all done on an active machine thru ssh.

The guide is outdated in terms of describing what to do with authorized_keys. With HassOS, you need to put that same file on a USB stick, not the SD. Also with HassOS, you can have Hassio up and running for this particular import.

The USB needs to be named “CONFIG” (yes, caps). Put the authorized_keys file on it, then insert it into the pi.
In the UI, go to hass.io > system and click on “import from USB”. You can refresh the logs below on that same page to see that the config has been imported. Then you should immediately be able to ssh as root using 22222, no restart or reboots are needed.

Thanks guys again. Today “by accident” I’ve found this threat New Hass.io images, based on HassOS that discuss this structural changes. It’s good to know (after 3 hard nights) that I’heading in the wrong direction.

I’ve placed my public key in usb and import all in hassio panel and… Works :slight_smile:

So… One problem solved. Now there is another. After connecting to port 22222 I try to use command “docker ps” and it says “docker is not hassio command.”. Very frustrating. What to do next?

Btw I tried a plugin called RPC Shutdown (sorry if I mispelled this) but this doesn’t work either.

You then also need to type login before the docker command to login to the host… I have no idea why but when you first login you can only access hassio commands until you login…

@DavidFW1960 How can I do this? After login to port 22222 what do I need to type? I connect using Putty as root@ipaddress and port 22222 with private/public key

Have you tried looking into the command_line switch? I used that to turn off my PC through HASS.IO until I discovered the RPC Shutdown add-on. Never had use of reboot, but I know it was possible through the command_line switch

when you see the prompt, type login and press enter…

Hmm. I don’have that login prompt twice. After connecting to port 22222 using SSH i write root once and then in command line i type docker… And it wont work.

@WonderMoose i’ve tried RPC Shutdown but it doesn’t work either (I made serious changes in win 10 like disabling firewall and changing registry but no luck using this method).

Still - I would like to have possibility to do every other shell prompts also, not just reboot…

When you log in using ssh to port 22222 you have to use the authorised keys file and you don’t enter anything in for login (at least I don’t) maybe I have that set in my connection. In any case, when you get a command prompt in the terminal, look at the text on the screen and that tells you that you need to login for the host (I think) so on that screen type login and press enter and you can do the docker stuff. Are you sure you are using port 22222 and not 22?

Thanks. Yes I’m sure about the ports because i fought with this for 3 nights now… I will try to apply what you said in couple of hours and give you fedback. I would like to thank everyone for sharing knowledge and time for this problem to be solved

@DavidFW1960 you were right again. I should type “login” to login.
Hassio docker login screen

Two problems solved. I will try to work with this solution and I will give you feedback how it’s going.

Hi Again. I was able to create public key just like in https://megamorphf.github.io/homeassistant/hyperion/ssh/hassio/2018/01/22/controlling-anything-via-ssh.html tutorial and stopped in 1.2 SLAVE part.
I need to create a new user so I connect to my computer using Putty ssh [email protected] port 22 and write my password. SSH connection log me to MobaSSH server and I cannot use command like sudo, useradd and so on

sudo useradd mister.slave sudo
message
-bash: -sudo: commands not found

Maybe should delete MobaSSH or maybe there is another workaround that I don’t know?

UPDATE: I’ve found a little trick to create user in Windows 10 and pass 1.2 SLAVE part with success. I just create new username inside Windows Control Panel and create password. In C:\Users\newUsername I’ve created folder .ssh and placed all public keys. I was able to do this using SSH also, but this methods works the same.

*Important - try not to create username with latin characters like śćłó and so on. Since my admin name is having one of the character - I’m unable to create and open files in my Windows 10 folder using Putty.

Three problems solved. Now I’m stuck on removing password:
“You will probaly be interested in running some commands with sudo without a password. For this we will need add these lines to /etc/sudoers file.”

Then in tutorial is sudo visudo command. It doesn’t work for me… Maybe I am on bad ip or port?

Update: I try to do next step - connect to docker and run command with -v to chech what’s going on.
344132d73ea1 is my HA docker

docker exec -it 344132d73ea1 /bin/bash
ssh -i /config/ssh/id_rsa -o StrictHostKeyChecking=no [email protected] -v ‘sudo reboot’

Please take a look at this img (login to user brostudio correct but it stopped in one place)
Hassio login

  • I cannot login using ssh while I’m in docker also. maybe this is the issue? Please help
    ssh [email protected] trying to connect using port 22 and no luck

I tried to remove password in brostudio username also, but no luck
SSH login from docker