SSH'ing from a command line sensor or shell command

I want to achieve the following:

  1. Create command_line sensor which connects to another docker container (running on the same host homeassistant container is running (HASS OS)) and gets some information

How can I do this? I used this guide to remotely access other SSH servers.
This time everything happens on the same host.

Can homeassistant containers SSH and root SSH be configured so homeassistant container can password-less login to root SSH (host SSH)? From a security point of view not the best way to go I guess.

command: "docker exec addon_a0d7b954_influxdb du -shm /data/influxdb/data/homeassistant | cut -f1"
→ This fails as it is run from HA in homeassistant container - where even no docker command exists. This command works on the root SSH very well.

Workflow I´m thinking about would look like:
HA container → SSH to host → login to another docker container → get information → done

(background: Unreliable InfluxDB size sensor - #34 by e-raser)

Oh wait, I see the problem. You said SSH so I assumed you had a container running an SSH service but that’s not it. You don’t actually want to SSH, you want to docker exec into a container.

As you noted the HA container does not have access to the docker CLI so that command won’t work from a commandline sensor. You will need to SSH from HA into somewhere else with docker CLI access and then execute that from there. Since it looks like you are running the InfluxDB add-on I would think that you have two options here:

  1. The workflow you outlined (SSH to host, run the docker exec command from there)
  2. Use the SSH & Web Terminal add-on (the community one) with protection mode turned off, that has access to the Docker CLI when you do that. Then use this workflow:
    • HA container → SSH to SSH & Web Terminal Add-on → login to another docker container → get information → done

Followed this example, but I am still getting:

Failed to add the host to the list of known hosts (/config/.ssh/known_hosts).

Did you create the /config/.ssh folder first? I tested just now and I am able to reproduce this error when the folder I want to put my known_hosts file in does not exist. As long as I make the folder first then it works without error.

That solved it. Wow, the .ssh folder permissions are really touchy! Thank you again.

1 Like

Hi @all, I could use some help with this.

I’m running HA core on Proxmox (LXC) and also have a RPi with 7" touchscreen that I use as a dashboard (running Raspbian desktop).
I would like to be able to switch the touchscreen on/off from HA when nobody is home & overnight.

From the HA shell I can achieve this with:

 ssh rpi 'echo 1 | tee /sys/class/backlight/rpi_backlight/bl_power'

and

 ssh rpi 'echo 0 | tee /sys/class/backlight/rpi_backlight/bl_power'

I have created this switch:

- platform: command_line
  switches:
    rpi_screen:
      command_off: "ssh rpi echo 1 | tee /sys/class/backlight/rpi_backlight/bl_power"
      command_on: "ssh rpi echo 0 | tee /sys/class/backlight/rpi_backlight/bl_power"

But this doesn’t work.
Don’t know if this is of any importance: in HA I’m logged with my own name, the command on the shell is as root.

Any idea what might be wrong?
Thanks for looking at this.

  1. Did you generate an SSH key and save it somewhere in the /config directory, preferably /config/.ssh/
  2. Did you use ssh-copy-id to copy the key to the Pi you want to control?
  3. Did you verify that you can definitely SSH in to Pi by pointing to the key (using -i ) in the SSH command, and ensure that it connects without asking for a password?
1 Like

Hi @mobile.andrew.jones, thanks for helping out!

I don’t understand what is happening:

root@v-hass:/config/.ssh# ls -l
total 9
-rw------- 1 root root 1876 Dec 18 23:46 id_rsa
-rw-r--r-- 1 root root  393 Dec 18 23:46 id_rsa.pub
root@v-hass:/config/.ssh# ssh-copy-id [email protected]
/usr/bin/ssh-copy-id: ERROR: No identities found
root@v-hass:/config/.ssh# ls
id_rsa  id_rsa.pub
root@v-hass:/config/.ssh# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /config/.ssh/id_rsa
/config/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /config/.ssh/id_rsa.
Your public key has been saved in /config/.ssh/id_rsa.pub.
...
root@v-hass:/config/.ssh# ssh-copy-id [email protected]
/usr/bin/ssh-copy-id: ERROR: No identities found

Right so the first problem is that just like you need to use the -i flag to point to the identity file when you use the ssh command, you ALSO have to do the same with the ssh-copy-id command, otherwise it is trying to find the key inside the docker container.

So:

ssh-copy-id [email protected] -i /config/.ssh/id_rsa

(Also I hope that you didn’t enter a passphrase when generating the key, otherwise it can’t be used in a command line)

1 Like

I did enter a password initially, did it again without.

root@v-hass:/config/.ssh# ssh-copy-id [email protected] -i /config/.ssh/id_rsa
/usr/bin/ssh-copy-id: ERROR: no ID file found

I’m not following you where you say using the -i flag when using the ssh command
Do you mean in the config of my command line switch?

Yes, the -i flag must need to go first, that’s fine:

ssh-copy-id -i /config/.ssh/id_rsa [email protected]

You will need to edit your ssh commandlines to look like this:

"ssh -i /config/.ssh/id_rsa pi@rpi echo 1 | tee /sys/class/backlight/rpi_backlight/bl_power"
1 Like

Thanks for sticking with me man!

The key is not copied to the remote /config/.ssh

root@v-hass:/config# ssh-copy-id -i /config/.ssh/id_rsa [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/config/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

When I try to login to the RPi, it asks me to enter a password.
(gotta go now, will check back later)

You probably typed the command it suggested you type:

ssh [email protected]

But as I previously indicated - you need to use the -i flag and tell it where to find the identity file:

ssh -i /config/.ssh/id_rsa [email protected]

that should not ask you for the password, hopefully :slight_smile:

1 Like

Just an FYI you’re commenting on a guide that essentially explains the issues you’re facing, did you read it? For example:

Since you told the command to generate the key file inan unusual location (/config/.ssh) then you need to tell it where it is for all future ssh commands with -i. That includes ssh-copy-id as Andrew noted.

In addition I notice on your commands you don’t seem to be specifying -o UserKnownHostsFile or -o StrictHostKeyChecking=no on your command. Which means your command will work for now but break next time you update since the known_hosts file is in /root. Would recommend reading the section called “Handling the known_hosts file” for why that’s an issue and the options to fix it.

Don’t do this.

  1. It doesn’t do what you think it does. There’s a user with the id “all”. You pinged them.
  2. If it did do what you thought it did (pinged everyone) a moderator would certainly be here warning you to never do that again.

Replying on a post notifies everyone that follows that post, you don’t have to @ anyone for that. Just for future reference.

1 Like

Hi @CentralCommand, thanks for stepping in and rubbing my nose in it! :blush:

I started over, but still not working.
This is what I did:

  1. generated the key, stored in /config/.ssh/id_rsa with no password
  2. copied the key with ‘ssh-copy-id -i /config/.ssh/id_rsa [email protected]
    If I understand it correctly: the -i /config/.ssh/id_rsa in the above command searches in that spot for the key.
    What about the remote directory, is that of any importance?
  3. Tested with ‘ssh -i /config/.ssh/id_rsa [email protected]’ which let’s me connect without asking a password.

When I try the command form HA shell:

root@v-hass:~# "ssh -i /config/.ssh/id_rsa [email protected] echo 1 | tee /sys/class/backlight/rpi_backlight/bl_power"
-bash: ssh -i /config/.ssh/id_rsa [email protected] echo 1 | tee /sys/class/backlight/rpi_backlight/bl_power: No such file or directory

and

root@v-hass:~# ssh -i /config/.ssh/id_rsa [email protected] echo 1 | tee /sys/class/backlight/rpi_backlight/bl_power
tee: /sys/class/backlight/rpi_backlight/bl_power: No such file or directory
1

and

root@v-hass:~# ssh -i /config/.ssh/id_rsa [email protected] 'echo 0 | tee /sys/class/backlight/rpi_backlight/bl_power'
0
tee: /sys/class/backlight/rpi_backlight/bl_power: Permission denied


So I don’t know if it’s a matter of syntax or something else.

Try it with " not ’ because bash is probably seeing the | and trying to pipe the command:

root@v-hass:~# ssh -i /config/.ssh/id_rsa [email protected] "echo 0 | tee /sys/class/backlight/rpi_backlight/bl_power"

nope… :roll_eyes:

root@v-hass:~# ssh -i /config/.ssh/id_rsa [email protected] "echo 0 | tee /sys/class/backlight/rpi_backlight/bl_power"
0
tee: /sys/class/backlight/rpi_backlight/bl_power: Permission denied

OK, but that is a response from the Pi though, so the login is at least working. Permission denied is coming from the Pi saying that you can’t do that.
Try removing the entire command so you are just doing:

ssh -i /config/.ssh/id_rsa [email protected]

and see if it will let you type the command then?

Yessss, first login in and then running the command works but I have to add sudo to both echo and tee. :star_struck:

I did put this in my switches.yaml but nothing happens.

This works:

root@v-hass:~# ssh -i /config/.ssh/id_rsa [email protected] "sudo echo 0 | sudo tee /sys/class/backlight/rpi_backlight/bl_power"
0
root@v-hass:~# ssh -i /config/.ssh/id_rsa [email protected] "sudo echo 1 | sudo tee /sys/class/backlight/rpi_backlight/bl_power"
1

This not:

- platform: command_line
  switches:
    rpi_screen:
      command_off: ssh -i /config/.ssh/id_rsa [email protected] "sudo echo 1 | sudo tee /sys/class/backlight/rpi_backlight/bl_power"
      command_on: ssh -i /config/.ssh/id_rsa [email protected] "sudo echo 0 | sudo tee /sys/class/backlight/rpi_backlight/bl_power"

OK a few things, you don’t need to use sudo to echo, so you can happily remove sudo from the echo commands, you only need the sudo to write (tee) to the /sys/ tree.

Second try it like this:

command_off: "ssh -i /config/.ssh/id_rsa [email protected] \"sudo echo 1 | sudo tee /sys/class/backlight/rpi_backlight/bl_power\""

Because Home Assistant (python) is trying to execute the command in bash, but needs to be told the " must be present in the command, so we use \" to tell whatever language we are coding with - that they must be escaped so that they pass through to the command line properly.
I’ve left the sudo there on the echo for now, until we verify we can get the command working as is.