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
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:
The workflow you outlined (SSH to host, run the docker exec command from there)
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
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.
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'
Did you generate an SSH key and save it somewhere in the /config directory, preferably /config/.ssh/
Did you use ssh-copy-id to copy the key to the Pi you want to control?
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?
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.
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)
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.
It doesn’t do what you think it does. There’s a user with the id “all”. You pinged them.
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.
Hi @CentralCommand, thanks for stepping in and rubbing my nose in it!
I started over, but still not working.
This is what I did:
generated the key, stored in /config/.ssh/id_rsa with no password
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?
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
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:
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.
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.