Run a Shell Command via Script

I can’t seem to get my shell command to run, and I’m not sure why. Here is my config:

script:
  vpn_on:
    alias: Turn VPN on
    sequence:
    - service: shell_command.vpn_on

shell_command:
  vpn_on: ssh [email protected] "vbash /config/scripts/vpnon.sh" 
  vpn_off: ssh [email protected] "vbash /config/scripts/vpnoff.sh"

Here is my log output when I trigger the “Turn VPN on” script via the Home Assistant front end.

2017-11-25 18:02:08 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=script, service=turn_on, service_data=entity_id=script.vpn_on, service_call_id=4373756504-24>
2017-11-25 18:02:08 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=script, service=vpn_on, service_data=None, service_call_id=4373756504-25>
2017-11-25 18:02:08 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]: service_call_id=4373756504-24>
2017-11-25 18:02:08 INFO (MainThread) [homeassistant.helpers.script] Script Turn VPN on: Running script
2017-11-25 18:02:08 INFO (MainThread) [homeassistant.helpers.script] Script Turn VPN on: Executing step call service
2017-11-25 18:02:08 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=shell_command, service=vpn_on, service_data=, service_call_id=4373756504-26>
2017-11-25 18:02:08 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]: service_call_id=4373756504-26>
2017-11-25 18:02:08 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]: service_call_id=4373756504-25>
2017-11-25 18:02:08 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=script.vpn_on, old_state=<state script.vpn_on=off; last_triggered=2017-11-25T17:28:34.902372+00:00, friendly_name=Turn VPN on @ 2017-11-12T15:54:57.089196+00:00>, new_state=<state script.vpn_on=off; last_triggered=2017-11-25T18:02:08.245378+00:00, friendly_name=Turn VPN on @ 2017-11-12T15:54:57.089196+00:00>>

I have verified that I can run ssh [email protected] "vbash /config/scripts/vpnon.sh" from the server outside of home assistant, so I know that it works. I’m just not sure whey I can’t get home assistant to do it. Any assistance would be appreciated. Thank you!

It maybe that HA doesn’t know where vbash is (so put the full path in), it may be that HA runs in a venv and doesn’t have permissions to run vbash.

1 Like

You were absolutely right. Adding the full path to vbash took care of the problem. Thanks!

1 Like

I am having issues with this, it was working and now does not. I have 2 shell commands that arm and disarm my CCTV system, which runs on my Mac:

ss_arm: ssh -o StrictHostKeyChecking=no [email protected] 'osascript "/Users/media/SecuritySpy/SS_Arm_MotionCapture.scpt"'
ss_disarm: ssh -o StrictHostKeyChecking=no [email protected] 'osascript "/Users/media/SecuritySpy/SS_DisArm_MotionCapture.scpt"'

However, when I try and run these scripts from the HA console I receive this error:

ERROR (MainThread) [homeassistant.components.shell_command] Error running command: ssh -o StrictHostKeyChecking=no [email protected] ‘osascript “/Users/media/SecuritySpy/SS_Arm_MotionCapture.scpt”’, return code: 255

I assumed in the past this would be because SSH has not used the password, but I have followed the guidelines here https://serverfault.com/questions/241588/how-to-automate-ssh-login-with-password

This works when I run it from a SSH session command line, but does nothing from within HA itself.

Can anyone help?

Thanks

Fixed my own issue by including the -i /config/.ssh/id_rsa command in the ssh syntax :slight_smile: