Hard time with shell command to shutdown Imac

hello,
I am trying to shutdown my Imac through a shell command. I first tried:

ssh -i /config/ssh_keys/id_rsa [email protected] "shutdown -h now"

I was getting the message “Not a super user”.
Of course it didn’t work as I had to use sudo.

So I gave up this method as I don’t want to login as root … (and you can’t pass root password over command line)

Then I found out that I can call osacript from command line but I am having hard time with quotes.

If I login to Imac and then run:

osascript -e 'tell app "System Events" to shut down'

It works! Imac shuts down without asking for a password! Bingo!

But when I try to run it from command line it doesn’t! :frowning:

ssh -i /config/ssh_keys/id_rsa [email protected] osascript -e 'tell app "System Events" to shut down'

Any help will be very appreciated!

can’t believe that nobody is shutting down imac through home assistant…Really? Nobody?

Nobody uses an iMac, but seriously - if ‘user’ can call sudo shutdown -h now, why don‘t you use

ssh -i /config/ssh_keys/id_rsa [email protected] "sudo shutdown -h now"

Thank you for your reply
That will be really easy, but unfortunately I can’t use sudo as it asks for root password confirmation.

Passwords can’t pass through a shell command…

That is a matter of configuration at the target machine.

Add an entry to your sudoers configuration file, like this:

user       ALL=(ALL) NOPASSWD:ALL

(use sudo visudo to avoid fatal mistakes when editing sudoers file)

Right,
personally I prefer the solution that is more unix-ish, than mac-ish.
and right, I would add that line to /etc/sudoers
(if you need only the shutdown, you can also specify it): something like: (“user” is the actual username)

user ALL = NOPASSWD: /sbin/shutdown

Concerning your initial request, using osascript, the issue is about argument values…
Simply add an echo at the beginning, and you will “see” what is executed:

echo ssh -i /config/ssh_keys/id_rsa [email protected] osascript -e 'tell app "System Events" to shut down'

gives

ssh -i /config/ssh_keys/id_rsa [email protected] osascript -e tell app "System Events" to shut down

as you can see single quotes are “lost” (because evaluated by shell on remote side)

osascript expects a single arg after the -e , but the ssh execution gives 3 strings to execute on remote side, which , after shell evaluation loses the quotes, so you can test this:

echo ssh localhost 'osascript -e '"'"'tell application "System Events" to get full name of current user'"'"

gives:

ssh localhost osascript -e 'tell application "System Events" to get full name of current user'

There are many ways to provide the proper quotes, the trick here is to end the initial single quote with a single quote , then start double with single inside, and terminate double and start single again…

So, remove the leading echo, and you have what you want:

ssh localhost 'osascript -e '"'"'tell application "System Events" to get full name of current user'"'"

(of course, replace to get full name of current user with to shut down, it’s easier to test without rebooting :slight_smile:

thank you very much both of you for the help!
Both comments helped me a lot.

I proceed with

ssh -i /config/ssh_keys/id_rsa [email protected] "sudo shutdown -h now"

after I corrected my sudoers file

I tried to do it this way, but I was doing it wrong.

Was trying

user ALL = NOPASSWD: /sbin/shutdown

but it didn’ work.

As soon as I tried

user ALL = (ALL)  NOPASSWD: /sbin/shutdown

it worked!!!
Thank you so much!

1 Like

follow up my previous post, another problem came up

When I run the following command from HA terminal everything works smoothly and imac shuts down.

ssh -i /config/ssh_keys/id_rsa [email protected] "sudo shutdown -h now"

So, I go ahead and I create the shell command as follows:

  ####shutdown imac right
  shutdown_imac_right: ssh -i /config/ssh_keys/id_rsa [email protected] "sudo shutdown -h now"

After that I create a script as follows:

alias: shutdown imac right
sequence:
  - service: shell_command.shutdown_imac_right
mode: single

and then add it to lovelace

image

Unfortunatelly when I run it from lovelace it doesn’t work.
Although it does work from terminal, all the times.

Here is the error I get, but I know that is not related



Logger: homeassistant.components.shell_command
Source: /usr/src/homeassistant/homeassistant/components/shell_command/__init__.py:115
Integration: Shell Command (documentation, issues)
First occurred: 4:49:38 PM (12 occurrences)
Last logged: 5:21:56 PM

    Error running command: `ssh -i /config/ssh_keys/id_rsa [email protected] "sudo shutdown -h now"`, return code: 255

NoneType: None


So, anyone out there knows why this is happening?

Anyone please ? I really need to solve this out …

try to put the full path for ssh too…
something like this:

/usr/bin/ssh -i /config/ssh_keys/id_rsa [email protected] "sudo shutdown -h now

thank you for your reply.
Unfortunately it makes no difference. From within Lovalace UI command is not been executed.
The same command runs smoothly from web terminal.

I tried to enclosure the command to single or / and double quotes but it made no difference.

Am lost…

this is what I am using now, without success

shutdown_imac_left: /usr/bin/ssh -i /config/ssh_keys/id_rsa [email protected] "sudo shutdown -h now"

Anyone please?
I am one step before I solve this …

I can’t believe the nobody is shutting down his iMac from within HA.
Especially, when the community of HA has grown up so much …

The problem come from how H.A. starts an external command.

Look at the implementation:

https://github.com/home-assistant/core/blob/d52137cc1a1518bba5b07a08f32182ac7ec260b6/homeassistant/components/shell_command/__init__.py#L44

It might not be exatly what you expect (shlex.split)

So, to resolve that, create a shell script, e.g. /home/myself/myshutdown

#!/usr/bin/bash
ssh -i /config/ssh_keys/id_rsa [email protected] "sudo shutdown -h now"

make it executable:

chmod a+x /home/myself/myshutdown

Check that it executes ok (for example to edit it, add echo in front of the ssh and execute it, then remove the echo.)

Then, specify the script instead of the full ssh command.

  ####shutdown imac right
  shutdown_imac_right: /home/myself/myshutdown

Thank you for your time and for this reply.
Although I had to wait for a quiet a while I finally got an asnwer.

So as per your advise, I created a script under /config/scripts/imac_shutdown_right.sh

#!/usr/bin/bash
echo ssh -i /config/ssh_keys/id_rsa [email protected] "sudo shutdown -h now"

I gave permissions

[core-ssh scripts]$ ls -l
total 8
-rwxr-xr-x    1 root     root           107 May 11 21:44 imac_shutdown_right.sh

When I try to test it I get the following error:

[core-ssh scripts]$ ./imac_shutdown_right.sh
-bash: ./imac_shutdown_right.sh: /usr/bin/bash: bad interpreter: No such file or directory

I found the problem myself. :slight_smile:
As soon as I corrected the path for the bash it worked!

I replaced the /usr/bin/bash with /bin/bash.

Now, from HA command line (ssh) it does work indeed

cd /config/scripts
./imac_shutdown_left.sh

But from UI although I add the shell command under scripts it is not running.

alias: shutdown imac left
sequence:
  - service: shell_command.shutdown_imac_left
    data: {}
mode: single

Can’t figure out why from Terminal works and from UI it doesn’t
please englight me!

Can we see the bashscript? And have you tried it within the homeassistant container?

docker exec -it homeassistant /bin/bash

and is there anything in your HA logs?

sure this is not a problem
under /config/scripts
there is a file called imac_shutdown_left.sh

it is executable and when I run ./imac_shutdown_left.sh it does the job!

here it is:

#!/bin/bash
ssh -i /config/ssh_keys/id_rsa [email protected] "sudo shutdown -h now"

when run it from UI it doesn’t work. It drives me mad

your shell_command looks like this?

shutdown_imac_right: '/config/scripts/imac_shutdown_right.sh'

And you took a look at your HA logs?

Try to actually connect to the homeassistant docker and then run it:

docker exec -it homeassistant /config/scripts/imac_shutdown_right.sh

My shell commands looks like this (without single quotes)

 shutdown_imac_left: /config/scripts/imac_shutdown_left.sh

when try to connect to docker I get the following

εικόνα