Command line switch: shutdown remote via ssh

Hi,
I’m trying to turn off my mac with a command line switch.
Following command is working via ssh from my hass machine:

ssh user@host sudo /sbin/shutdown -h now

So I setup a switch:

switch:
  - platform: command_line
    switches:
      Mediacenter:
        oncmd: sudo etherwake so:me:th:in:ge:ad
        offcmd: ssh user@host sudo /sbin/shutdown -h now

But unfortunately this does not work.

Any ideas?

Thanks,
Tom

1 Like

Do you have ssh set up to work between your HA box and your Mac without passwords?

linuxproblem.org/art_9.html

Thanks for the hint.
Yes, it is already setup and it’s working from the command line (ssh a → b).
It’s just not working as hass switch.

Further ideas?

Can you give it a try with the command wrapped in quotes?

offcmd: ssh user@host 'sudo /sbin/shutdown -h now'

Thanks Fabian, but this did also not work :frowning:

Could it be related to insufficient user rights?
Hass runs on a Raspberry and user is Pi.

Usually users are allowed to use ssh. Let me do a little guessing…the private key is in the .ssh folder of user ‘Pi’? There is no password set for the keys? ‘user@host’ is allowed to use sudo on ‘host’ without password?

Good guessing :wink:

  • id_rsa, id_rsa.pub and known_hosts are located in /home/pi/.ssh :heavy_check_mark:
  • what do you mean with “no password set for keys”? I did not set any pw on purpose, even changed for testing rights to 777 :question:
  • if logged in via ssh, user is able to do sudo without pw :heavy_check_mark:

In addition I tried to put the ssh into a bash script and run the bash via command line switch.
This did also not work.

You can login into the remote host, so it seems that the permissions are right. Are you launching Home Assistant as pi?

No password protection of your private key. It could be if you use ssh directly from the commandline that a tool like ssh-askpass is providing your password.

No luck with that one :wink:

Ha, I got it work :slight_smile:
Thing is, root account was not enabled on Raspbian.
Adding “PermitRootLogin yes” into “/etc/ssh/sshd_config” and set a password for root user does the trick.
Thanks @fabaff for the hint.

So is HA running as root or as pi on your system?

It runs as root (insralled with sudo su).

I’m having the same issue here despite having everything tested (to my knowledge limit)

and here’s the error msg from HASS log
16-04-03 01:27:37 homeassistant.components.switch.command_switch: Running command: ssh [email protected] ‘poweroff’
16-04-03 01:27:37 homeassistant.components.switch.command_switch: Command failed: ssh [email protected] ‘poweroff’

switch:
      oncmd: "wakeonlan E8:39:35:2D:E8:FD"
      offcmd: ssh [email protected] 'poweroff'

Also tried without the text wrapper, also tried creating bash script to call the ssh.

I am running HASS as root, when I run the command manually via CLI it worked just fine.

Appreciate the help

Hi @kwetiaw,
have you enabled ssh without pw?
You have to do this with the same user as hass runs on (root).

If you run hass on Raspbian you have first to enable root user (only sudo su did not work for me).
That was the solution for my scenario.

Here my working switch:
- platform: command_line
switches:
MacMini:
oncmd: sudo etherwake 47:11:25:a4:3e:ad
offcmd: ssh [email protected] ‘sudo /sbin/shutdown -h now’

1 Like

My hass on raspbian is running as root. I have also enabled the ssh without password. When I putty to raspbian, running the command manually works, but not through HASS switch.

I have also tried using sudo.

My raspbian root user is active and has current password. Also permitted root login. My switch ON command using wakeonlan works perfectly, not the ssh

Im running out of ideas.

I have a WOL switch for switching on my NAS and a ssh switch for switching it off. Is there any easy way of combining the two? so that I can get the sate from the WOL AND also switching on and the switching off from command line?

switch:
  - platform: wake_on_lan
    mac_address: [redacted[
    name: "NAS"
    host: "192.168.178.XXX"

  - platform: command_line  
    switches:
      switch_off_NAS:
        offcmd: "ssh -i /config/cert/id_rsa [redacted]@192.168.178.XXX sudo /sbin/poweroff"   

Hey
Hope this help

My config is below and I followed this guide to enable ssh between the two without needing a password http://linuxproblem.org/art_9.html

Switch:
    Some Server:
      oncmd: "wakeonlan E1:32:36:2D:E8:FD"
      offcmd: "sudo ssh [email protected] poweroff"

So it is a single On/Off switch

Thanks.

What I’d like to have is that the state of the PC would be determined via WOL. the WOL switch component does this nicely…

You only have stateless switches… BTW. I got the ssh without password working nicely. this is what the -i id_rsa is for.

After a number of hours and a great degree of frustration, the problem for me turned out to be OpenSSH.

I switched to a Bitvise server on my windows machine and tried the same exact command without issue.

No idea why…

Hi folks,
Apologies, I know this is an old thread…
I’m having trouble with this myself. I’m unclear as to the actual resolution reading through this.
I’m running HA in a Linux Docker (HASSIO).
I’m just wanting to shutdown Octoprint on an RPi.
The switch config is:

switch:
  - platform: command_line
    switches:
      octoprint_switch:
        command_on: ssh [email protected] 'sudo /sbin/shutdown -h now’

No scripts or anything fancy with my setup.
When I try and execute from LoveLace it fails (via a simple entity switch).

I can SSH into the HA docker and manually run the command okay. It works this way. I am however logging in as root when done manually. Not as the HA user.
I’ve gone looking for a HA user - can’t find anything that might be… I’ve issued the following command to look:
I am trying to log in as ‘pi’ anyway?

I’ve enabled password-less access into the Pi by setting up key pairs. Works when I manually log via the HA CLI.

I’m sure I’m close but doing something stupid…
Help!

I wrote up how to perform remote shutdown of a Netgear ReadyNAS which may well help others.

How to start up and shutdown a Netgear ReadyNAS from Home Assistant