Configuring Script to send SSH command to Remote Machine

Hi All,

I’ve been struggling with this issue for around a week now and am hoping the community can help. I would like to have a switch or script that sends an SSH command to a remote raspberry pi that can open a window. I used the guide here to set it up: https://siytek.com/home-assistant-shell/

I have successfully created a key and can run my command via terminal in Home Assistant OS. The command is very simple as it triggers a bash script on the remote raspberry pi to do the hard work. The command that executes without issue in home assistant terminal is the following:

ssh [email protected] '~/open_window_increment'

I copied the keys into my config ssh folder using the following in home assistant terminal:

cp ~/.ssh/id_rsa ~/config/ssh

I confirmed that this created an ssh file in my config folder. I have then configured a switch according to the guide. This means that I added the following to my configuration.yaml file:

shell_command:
  open_window_increment: 
    ssh -i /config/ssh/id_rsa -o 'StrictHostKeyChecking=no' [email protected] '~/open_window_increment'

My script then reads as follows:

script:
  alias: Open Window Increment
  sequence:
  - service: shell_command.open_window_increment
  mode: single
  icon: mdi:window-closed-variant

However when I run my script, I get the following error in my logs:

Logger: homeassistant.components.shell_command
Source: /usr/src/homeassistant/homeassistant/components/shell_command/__init__.py:115
Integration: Shell Command ([documentation](https://www.home-assistant.io/integrations/shell_command), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+shell_command%22))
First occurred: 10:08:22 AM (1 occurrences)
Last logged: 10:08:22 AM

Error running command: `ssh -i /config/ssh/id_rsa -o 'StrictHostKeyChecking=no' [email protected] '~/open_window_increment'`, return code: 255

NoneType: None

As far as I can tell I’ve done everything right. The script triggers without issue (according to the home assistant UI) and I don’t think I have any syntax errors or have missed any steps. Based on prior reading in this forum it could be related to permissions but I don’t know how to start diagnosing any of that.

Any help would be greatly appreciated.

2 Likes

There’s a guide on how to setup SSH here on the forum:

I would start here:

Also if you are having trouble the command line integrations all log stdout and stderr if you set the log level of the integration you are trying to use to debug.

1 Like

The name hassio was dropped over two years ago. Please stop calling Home Assistant OS that. It confuses newcomers.

2 Likes

Updated - thanks for the heads up

I do have the same Error " 255 " in my logs

Logger: homeassistant.components.shell_command
Source: /usr/src/homeassistant/homeassistant/components/shell_command/__init__.py:115
Integration: Shell Command (documentation, issues)
First occurred: 00:10:54 (3 occurrences)
Last logged: 00:13:51

Error running command: `ssh -i /config/.ssh/id_rsa [email protected] 'bash /boot/config/plugins/user.scripts/scripts/HAVMReboot/script'`, return code: 255
NoneType: None

My entry in the configuration.yaml

# SSH Shell Command
shell_command:
  ssh_test: ssh -i /config/.ssh/id_rsa [email protected] 'bash /boot/config/plugins/user.scripts/scripts/HAVMReboot/script'

When I try to use the " Shell Command: ssh_test " Service in the developer tools it doesn`t work and the Error above occurs.
But the command from the configuration.yaml does work in the Terminal addon.

@SSHhelp do you have a solution?

1 Like

I am facing a similar issue… did you figure this out to run a ssh command from the config.yaml?

Yes, its important to use the non authentication method " -o StrictHostKeyChecking=no '"

# SSH Shell Command
shell_command:
    ssh_test: ssh -i /config/.ssh/id_rsa -o 'StrictHostKeyChecking=no' [email protected] 'bash /boot/config/plugins/user.scripts/scripts/HAVMReboot/script'

With this config.yaml entry I was able to use it in automations without any errors.

I’m having similar issues and have tried everything.

I have a shell_command setup like this:

test: ssh -i /config/.ssh/id_rsa -o 'StrictHostKeyChecking=no' [email protected] 'ls'

When I try to call this from HA, I get an error 255 in the log file, and on the docker.lan host, it says this in the /var/log/auth.log

Apr 21 16:14:41 docker sshd[3984722]: Failed password for derek from 127.0.0.1 port 50848 ssh2
Apr 21 16:14:41 docker sshd[3984722]: Failed password for derek from 127.0.0.1 port 50848 ssh2
Apr 21 16:14:41 docker sshd[3984722]: Connection closed by authenticating user derek 127.0.0.1 port 50848 [preauth]

However, I can docker exec into the HA container and run exactly the same command and it works just fine, showing that everything should be setup ok.

Any ideas?

In case it helps others, I finally managed to debug my setup.
It was down to file ownership and permissions. I had to make sure the .ssh folder and the id_rsa and id_rsa.pub files had the correct permissions, then everything started working again.

For anyone following the siytek guide, I found a detailed missing that may cause 255 code to be thrown.

When executing this command, cp ~/.ssh/id_rsa ~/config/ssh you must have created the ssh directory in config first (mkdir ~/config/ssh) or use file editor in ha, or else it will copy the id_rsa file and create a file named ssh (copy of id_rsa) in config directory. That’s what Op was describing (a file named ssh in the confit directory) You can fix it by creating the ssh folder and copy the id_rsa (rerun the cp command) file in to the newly created folder. Alternatively if you don’t want to copy the file, just change the parameter after -i to -i /config/ssh

Hi, I crated a simple test command which I use in the SSH add-on (security-mode disabled). It works fine all keys are available.

ssh -i /root/.ssh/id_rsa [email protected] md test

If I crate the shell command like this:
ssh_command: "ssh -i /root/.ssh/id_rsa [email protected] md test”

and execute it. I receive this error:
stdout: “”
stderr: >-
Warning: Identity file /root/.ssh/id_rsa not accessible: No such file or directory.
Host key verification failed.
returncode: 255