Shell Command not running

Hi guys,
I wanted to configure shell commands to execute a bash script.
That didn’t work. The shell command is saved in configuration.yaml.

That is the shell command code:

shell_command:
  awning_in: "/bin/bash /root/ownscripts/awning/ssh-awning-in.sh"
  awning_out: "/bin/bash /root/ownscripts/awning/ssh-awning-out.sh"
  awning_half: "/bin/bash /root/ownscripts/awning/ssh-awning-half.sh"
  awning_reset: "/bin/bash /root/ownscripts/awning/ssh-awning-reset.sh"

I even tested it with a touch command to the /root directory and without quotation marks.

To run it I made a button on the dashbord with following code:

show_name: true
show_icon: true
type: button
name: Markise einfahren
tap_action:
  action: call-service
  service: shell_command.awning_in

Thank you.
Jan

“Best practices” would be to put your shell script files in a subdirectory inside the configuration directory.
For example, if your HA installation is full-blown HA with HAOS/Supervisor/etc. then your config directory is /config/ so put your shell scripts for example at: /config/shell_commands/awning/ssh-awning-in.sh.

You’ve also have the command as well as the script encapsulated within quotes which may be a problem too.

Your shell command should look something like:

shell_command:
  awning_in: /bin/bash /config/shell_commands/awning/ssh-awning-in.sh

Agree with @wmaker

Also, are you seeing any errors?

How did you create your sh? If in HA File Editor, you probably need to run dos2unix on your sh.

Also, did you give them the ability to execute with chmod?

Making some assumptions on your install type, based on the path you indicated it appear you’ve logged in at the OS level and then placed the script under the root users home directory /root. That directory would not be visible within HA. Best way to debug this type of stuff is to install the “Terminal & SSH” add-on. You can then get access to a terminal window (bash shell) in the HA environment and see if you can access your shell command. Assuming you can access it you can try running it to verify it works, prior to trying to control it directly from HA.

1 Like

Good point. I could try this.

So. With the script still in the /root directory not working. How could I see errors with shell command? That is my code:

configuration.yaml:

#Awning control

shell_command:
  awning_in: /bin/bash /root/config/ownscripts/awning/ssh-awning-in.sh
  awning_out: /bin/bash /root/config/ownscripts/awning/ssh-awning-out.sh
  awning_half: /bin/bash /root/config/ownscripts/awning/ssh-awning-half.sh
  awning_reset: /bin/bash /root/config/ownscripts/awning/ssh-awning-reset.sh

ssh-awning-in.sh (for example):

sshpass -p "pass" ssh [email protected] "python3 /home/pi/Awning-control/main.py 15 in"

If I run it with the Terminal add-on it works finally. Now, I am going to test in another directory (for example /ownscripts)

After trying: Still not working. Could you send me a chmod command?#

Thanks, Jan

So the scripts ssh-awning-xxxx need to go in the HA directory structure. I create a directory called shell_cmds in /usr/share/hassio/homeassistant and put all of my shell commands there.

Then they are reference like this

shell_command:
     awning_in: /config/shell_cmds/awning/ssh-awning-in.sh

No need for the /bin/bash as HA knows to run them as shell commands.

If you’re going to do the ssh thing as you have it you should really set up a key to login via ssh, and in the authorized_keys file put a restriction on the commands that can be executed when using the key. When I do it, I restrict the host being logged into to only allowing a single command to be executed and then instead of passing the path to the specific command I want to execute on the ssh host I simply pass an argument that is interpreted by the ssh host shell command to run the desired command.

So in the HA terminal window you can generate a keypair via

ssh-keygen -b 2048 -t rsa

Then you push the public key from this pair to the host you need to run the commands on via running the following in the HA terminal window:

ssh-copy-id [email protected]

You can then verify this worked by doing the following in the HA terminal window:

ssh [email protected]

This should log you in without asking for a password. Assuming it does log back out with exit.
Next on the ssh host you pushed the key to you can edit /home/pi/.ssh/authorized_keys and modify the line that was placed in this file by ssh_copy_id. You want to pre-append the following to the beginning of the line:

command="/usr/share/hassio/homeassistant/os-support-apps/local_ctrl $SSH_ORIGINAL_COMMAND",no-port-forwarding,no-x11-forwarding,no-agent-forwarding 

The resulting line looks something like this:

command="/usr/share/hassio/homeassistant/os-support-apps/local_ctrl $SSH_ORIGINAL_COMMAND",no-port-forwarding,no-x11-forwarding,no-agent-forwarding ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDLUQh4g54XYM0HMBklLfKlPg6lmxgg7Y/bGmsO9bGb5eSJMiyydLVyhEz5h9Z+ssgDvoH88B68xRd2UG9Xrf9bJtATvhzLfs5prlTh+Qa4OSG7tEJ020XE+jCPVBfD3wTGt05GM2YJPBKGGx7r8LGPxX+k8DKgti/SI7gTN0MkhbqSM7OgjuR9KJBlyn+GbbFxsrO6ML1ducL6/gkkGtbstTaC5xriQgJ3oTQKBvsvwjBy6ITTzQ/Q11IoBplsTEib8MeBXsT7fcjXZIG7ZzP8AbdgesjsqRqs5SWFs+VxmQhkYS+gzfVOs2/kxQSGx0FoYwaAdF1UkHskzvGAuHJsMUuP4fI7eG+pl3l0JIcpi6Kine2k+FjDocUcN5Pf0i6TyAjYNl8q/ZCQdfOKCP8Ol1FW/mGYt+7DeailPnikd4tTokZ0NsP1x5g5ph/ZZwv1Ca0eqDTEoE/z+WFZxAyQ3NuYFgerRdZs+m19Udr/KCdPArwoDYZNM/jeM= root@core-ssh

The command that get’s executed now when you login with the key is /usr/share/hassio/homeassistant/os-support-apps/local_ctrl. The long string is the public key and will be different, my intent is to just give you an idea of what the full line in authorized_keys looks like.

That line will only allow the script /usr/share/hassio/homeassistant/os-support-apps/local_ctrl to be run when the use logs in and it will pass the command line argments passed on the ssh line to the script local_ctrl. For me the script file /usr/share/hassio/homeassistant/os-support-apps/local_ctrl is used to start and stop a VPN. Here’s it’s content:

#!/bin/bash
#exec 0<&- 1> >(logger -t $(basename $0)  >/dev/null 2>&1) 2>&1
echo Received: $@
if [ $1 == "start_vpn" ]; then
        # Just incase make sure any existing tunnel is shut down
        sudo killall openvpn  >/dev/null 2>&1
        # Now start tunnel that gives remote access when celluar is active
        sudo openvpn --daemon --config /etc/openvpn/client/phome.conf
elif [ $1 == "stop_vpn" ]; then
        sudo killall openvpn
else
        echo Request $1 unknown
fi

It uses the first command line argument to tell it if it’s starting or stopping the VPN.

So then in my shell_cmds directory I have another shell command called local_ctrl that looks like this:

#!/bin/bash
ssh -o StrictHostKeyChecking=no [email protected] $1

You probably notice above I don’t specify the command on the ssh host I want to run, and that’s because it will only run the one command in the authoried_keys file.
In the HA terminal window I can test this command by running it:

/config/shell_cmds/local_ctrl start_vpn

Assuming everything is right my VPN is started. You could modify this to execute your awning commands.

The shell_command line then looks like this

shell_command
  awning_ctl: '/config/shell_cmds/local_ctl {{ cmd }}'

Then a script to run this would look something like this:

start_vpn:
  alias: start_vpn
  sequence:
  - service: shell_command.local_ctrl
    data:
      cmd: start_vpn
  mode: single

Hope this helps.

1 Like

It asks for a password…

What point are you at when it asked for the password? It will ask for the password when you do the ssh-copy-id. After that you should be able to do the direct ssh into the box without it asking for a password. Are you saying it ask for the password when you do the ssh after you do the ssh-copy-id? Based on the example above the ssh-copy-id will take key in the file /root/.ssh/id_rsa.pub and put in the /home/pi/.ssh/authorized_keys file on the host 192.168.178.12. You can look at those two files and verify that authorized_keys include a line that looks like what’s in the id_rsa.pub.

Also in the above when it says to do things in the HA terminal window, that’s the terminal window you get by going through the HA user interface with the terminal & SSH addon.

It wants a password every time. But this doesn’t matter, the shell command doesn’t even run straight commands like

CMD: touch /cmd/t.txt

Or runs a bash file with that command

I’m assuming you’re running the touch command within HA GUI terminal window, which wouldn’t work unless you first made the directory /cmd within the terminal window. The terminal window runs within a container and thus can’t touch /cmd if it’s at the host OS level.