Error when executing SSH command using command line switch

I am trying to get the following to work:

switch:
  - platform: command_line
    switches:                                                                      
      living_room_security_light:
        friendly_name: Living Room Security Light                                    
        command_on: "ssh -i /config/id_rsa pi@cam '/home/pi/brightpi-control-master/white-on.sh'"
        command_off: "ssh -i /config/id_rsa pi@cam '/home/pi/brightpi-control-master/all-off.sh'"

I get the following error:

...homeassistant.components.switch.command_line] Command failed: ssh -i /config/id_rsa pi@cam '/home/pi/brightpi-control-master/white-on.sh'

I tested the command by hand and it works fine. Any idea what could be wrong?

I put the private key to use inside /config. Is HA running in its Docker using the same path?

I am using hass.io with HA 0.52.1

Update: I added 2> /config/command.log to the end of the command and can now see what is going wrong: Host key verification failed. I now know what to do to fix this.

Update 2: Final fix. I use the following command (replace <…> with your own):

ssh -F /config/ssh_config -i /config/id_rsa <user>@<host> '<command>' 2> /config/command.log

Make sure /config/ssh_config and /config/id_rsa have 400 set as their permissions:

chmod 400 /config/ssh_config /config/id_rsa

The file /config/ssh_config should contain:

Host *
    StrictHostKeyChecking no
7 Likes

Hello,
I have in root/.ssh/ folder files: authorized_keys, id_rsa, id_rsa.pud, known_hosts.
Should I copy id_rsa to config folder a create there file ssh_config ?

Thank you.

Yes. Copy all files to there and make sure the permissions are correct.

Hey thanks a lot for this… fixed my issue right up…

In case it wasn’t clear for others and it isn’t a ssh key issue, the final fix doesn’t have quotation marks around it in the .yaml file.

At least I assume that because that’s how I fixed my issue.

Does HA log an error for you when you run your command?

Thank you!!!

Hello,

Can you help me ?

I will wish to obtain the temperature from another raspberry.

Copy to clipboard

    - platform: command_line
      name: Température_Raspberry
      scan_interval: 300
      command: ssh -i /config/.ssh/id_rsa -o StrictHostKeyChecking=no -q [email protected] sensors
      unit_of_measurement: '°C'

Not working
However in the terminal on my HA :
ssh -i /config/.ssh/id_rsa -o StrictHostKeyChecking=no -q [email protected] sensors
works !

Best regards

Use the 'ssh addon" for execute ssh commands

Example
- id: '15898884'
  alias: Name Of Automation
  description: ''
  trigger:
  condition: []
  action:
  - data:
      addon: a0d7b954_ssh
      input: ssh -i /config/id_rsa pi@cam '/home/pi/brightpi-control-master/white-on.sh'
    service: hassio.addon_stdin

thannnnnnnk you, I search for a long time to solve this issue, the 2> /config/command saved my life :slight_smile: