Hass.io shell_command error

Is there any known issue with hass.io and shell commands? I am unable to get the WOL switches turn off service to work.

When i use the switch in the UI, just nothing happens and i get this from the logs: (no matter what cmd im running)

Error running command: `touch /home/hass/test.txt`, return code: 1
02:10 /usr/lib/python3.6/site-packages/homeassistant/components/shell_command.py (ERROR)

Return code varies. Sometimes 1, sometimes 255.

1 Like

Yes, there are fairly restrictive limitations due to running in a docker environment.

https://home-assistant.io/hassio/run_local/

If you’re attempting to shutdown a Windows machine, check out this add-on:

I am trying to shutdown a linux machine.

Ok, so there’s no way to get the WOL Switch to work in hass.io out of the box?
https://home-assistant.io/components/switch.wake_on_lan/

Do you know of any custom components that i could use instead? The link you provided is still a little too high level for me, since i just started playing around with HA.

You could do something similar to this thread to issue the shutdown command over an ssh connection.

That is almost the same as the WOL Switch I linked earlier. And there is the issue: I seem to be unable to run any command. I keep getting an error and nothing happens. This time I tried your linked solution with the command_line switch and I got:

Command failed: ssh -i /home/hass/id_rsa [email protected] 'sudo poweroff'
14:59 components/switch/command_line.py (ERROR)

Im my first post you can see that even a simple touch command fails. I used that one because I thought my ssh might be misconfigured. meanwhile i made sure that the ssh stuff is working. I can login into my HA server and poweroff my homeserver using the ssh command that is in the error log.

/home doesn’t exist in the container. Use /config as mentioned in that other thread.

1 Like

Oh, i must have missed that one. But still:

Error running command: `ssh -i /config/.ssh/id_rsa [email protected] sudo poweroff`, return code: 255
17:00 /usr/lib/python3.6/site-packages/homeassistant/components/shell_command.py (ERROR)

Any more ideas on this issue?

Hi. I’m also trying to call SH scripts from Home Assistant (HASSIO in docker).

In my case I gave up running the scripts on my HASSIO host (Ubuntu virtual machine)

From my docker container I run the following command which connects to another server, referencing a private key and then run a script on it.

ssh -i /config/.ssh/id_rsa [email protected] sh /home/km/testing.sh

I’m no longer prompted for a password when I log into docker as root and run the above command since I copied the private key to /config directory. Everything works great, until I reboot.

After reboot I get same error as you, 255.

The problem now is known_hosts. Post reboot, the first time I tried to run the command from shell as root in docker I was prompted to add target machine, 192,168.1.200 to known_hosts. After I type Yes and added it to known hosts I was able to run the command above perfectly. From within home assistant I can call on the shell command! It’s awesome, until I restart and have to do it again.

Anyway, I think we need to figure out a way to keep the known_hosts file persistent. I believe it is getting wiped out periodically from restarting.

You can confirm this by logging into your docker container as root and trying to connect the same way. Once you add the host to known_hosts you should be fine.

I checked the SSH options hoping there is way to reference a specific known_hosts file but came up empty handed.

Edit: I got it! Here is the command I use:

ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /config/.ssh/id_rsa [email protected] sh /home/km/testing.sh

So, in summary - I have this command setup as a shell_command in Home Assistant. I can reboot Home Assistant, the docker container, and system running docker and it works on every try!!

8 Likes

Brilliant! My server now shuts down using the WOL Switch.
Thanks a lot!

BUT:
I am still getting the error, even though it worked. :slight_smile:

Error running command: `ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /config/.ssh/id_rsa [email protected] sudo poweroff`, return code: 255

Does the command work from within home assistant? If so I would probably ignore the error message :slight_smile:

The stuff I read yesterday suggests 255 means destination host has responded but it’s public key(?) is unknown. I’m not sure what can be done. There may be a way to suppress the error in the command.

Also, is task working for you after reboot?

Yes, i can run that command within HA. As i said, I am using the WOL Switch for my homeserver to turn it on and off. Works like a charm now, except for the error I am getting. Will ignore it though, lol.

It is still working after a reboot.

My setup:

# Wake on LAN Switch Homeserver
wake_on_lan:

switch:
  - platform: wake_on_lan
    name: "Homeserver"
    host: 192.168.9.4
    mac_address: "....."
    broadcast_address: 192.168.9.255
    turn_off:
      service: shell_command.turn_off_homeserver

shell_command:
  turn_off_homeserver: 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /config/.ssh/id_rsa [email protected] sudo poweroff'

Hey,

in the last two weeks i tried to shutdown my mac and also the other pi’s running rasplex and volumio with the wol-switch. i also tried the command_line switch but i’m still struggling to get one of the switches working.

I am able to ssh into the pi’s or main user account of my mac without entering a pw (as described here

WOL-Switch) and shut them down. On my mac i also edited the sudoers file like it is described here. But still i get the 255 error in hassio-log :frowning:

Would one of you be so kind and tell me step by step what to do exactly? :slight_smile:

Btw, on my pi running hass.io there is no .ssh directory (?)

you have to create that .ssh folder on your pi running hass.io.

mkdir /config/.ssh

then put your key-pair (e.g. id_rsa and id_rsa.pub) into that folder. (the key that you need to login to the other machines)

and i posted above what your switch in hass.io should look like.

2 Likes

ok, thanks for your fast answer! i will try that!
i guess i have to do what is described here

dont you have that already when you are able to login without password?

login into your hass.io pi

ssh root@[HASSIO IP]

create the folder

mkdir /config/.ssh

create key

ssh-keygen

tell it to store it in

/config/.ssh/id_rsa

do not set a password for the key!

copy public key to target machines (repeat for all macs and pis that you want to control)

ssh-copy-id -i /config/.ssh/id_rsa [TARGET USER]@[TARGET IP]

try to ssh into the targets from you hass.io pi and see if it works without a login.
then copy my WOL switch config posted above and make it match your network setup

13 Likes

oh my god! it works! you just made me really happy! thank you!

I thought my quick controls to restart my Plex server, Plex daemon, and VPN were forever broken! Thank you for the solution!

Apologies for thread revival. These instructions:

login into your hass.io pi

ssh root@[HASSIO IP]

create the folder

mkdir /config/.ssh

create key

ssh-keygen

tell it to store it in

/config/.ssh/id_rsa

The default location for storing this key with Hass.io seems to be /root/.ssh instead of /config/.ssh. My keys stay there after reboot and seem to work - is there a reason you’ve put this in your /config directory?

I thing I got stuck on for quite a while… On hassio, the scripts run as the user hassio. So make sure your id_rsa is owned by hassio by running the command “chown hassio:hassio /config/.ssh/id_rsa”.
That fixed it for me :slight_smile: