How to execute a script in sudo on the host?

Hi,
I’m running Hass. io in docker on ubuntu.
The objective is to have a button that will transfer playback to another speaker with spotify connect.

To do that I would like to run a script with sudo on my host (the ubuntu one) so I use sshpass like this :

sshpass -p mypassword ssh -t [email protected] "echo mypassword | sudo -S /bin/bash /usr/local/bin/test.sh"

This command line works well when I run it in ssh from the hass. io docker but impossible to make it works when I run it from automations with shell_command.

Same result without sshpass, after using ssh-keygen & ssh-copy-id :

ssh -i /config/scripts/id_rsa -t [email protected] "echo mypassword | sudo -S /bin/bash /usr/local/bin/test.sh"

I’ve made a lot of test and I know that the script “kitchen.sh” is launched by hass. io but it doesn’t seems to launch the ssh command :expressionless:
Even with an easier remote command line it’s not working :

sshpass -p mypassword  ssh -t [email protected] "echo mypassword > /tmp/mytmp.txt"

config.yaml :

shell_command:
  transfer_kitchen: '/config/scripts/kitchen.sh'

automations.yaml

    - id: 1000000000000
      alias: Spotify Connect in Kitchen
      trigger:
      - event_data:
          entity_id: binary_sensor.switch_1000000000000
        event_type: click
        platform: event
      action:
      - data: {}
        service: shell_command.transfer_kitchen

kitchen.sh :

sshpass -p mypassword ssh -t [email protected] "echo mypassword | sudo -S /bin/bash /usr/local/bin/test.sh"

What can I do to know what is the problem ? I tried to redirect output of the script without success.
The script on the host is not executed.

1 Like

@schmurtz Did you ever found a solution for this?

i failed to use ssh command with certain args.So i switched to nodered and it works great.What you need in nodered is
exec node

My example of a remote sudo command:

2 Likes