SSH from HA using the Command_Line sensor

Hi everyone,

I run my homeassistant (v0.95.1) instance in an iocage jail on my FreeNAS server.
I’m looking to add temperature monitors to homeassistant for the CPU and hard drives of my server via the command_line sensor.
This is what I have within my configuration.yaml at present:

  - platform: command_line
    name: FreeNAS_cpu_temp
    unit_of_measurement: '°C'
    command: ssh -i /root/.ssh/id_rsa -o StrictHostKeyChecking=no -q [email protected] sysctl dev.cpu.0 | grep temperature | tr '.' ' ' | awk '{print $5"°"}'

I have set up ssh using key authentication (ssh-keygen + adding public key to the root FreeNAS user) and have verified that the above command functions as expected when run manually by ssh-ing into my freenas (through cmd), changing into my homeassistant jail (so that the commands are executed from ‘root@homeassistant’) and testing. This returns 26 (°C). However on the homeassistant front end, a blank value is returned (as below). There are no related entries in the home-assistant.log (which doesn’t help with troubleshooting!)

Does this mean that the SSH connection was successful (no error) and there is most likely an issue with a blank return value or a value that couldn’t be parsed? Or is is still possible that SSH fails and just doesn’t throw an error? I’ve tried removing the ‘-q’ from the command but this still doesn’t give any errors. Perhaps there is a permissions problem?

I’ve tried a few steps from other forums around the net but I haven’t has any success.
Any thoughts would be appreciated :slight_smile:

Ok I figured this out. Turned out to be a permissions related issue with the private key. No change to the config as posted above was required.

Hello,

I have hassio running on ubuntu on an old PC. There’s also a Raspberry Pi running rtl_433 getting data from a few 433 MHz sensors and sending them over MQTT.

What I’m trying to do is get a reading of the Pi’s core temperature using a command line sensor. I have SSH keys setup from the hassio machine to the Pi, everything works well when trying the command from hassio shell:

core-ssh:~# ssh [email protected] cat /sys/class/thermal/thermal_zone0/temp
66604
core-ssh:~#

Problem is that the command won’t work from the command line sensor. I get:

2019-11-23 17:02:23 ERROR (SyncWorker_4) [homeassistant.components.command_line.sensor] Command failed: ssh [email protected] cat /sys/class/thermal/thermal_zone0/temp

I already tried specifying the path for id_rsa as you do in your command, but the error is the same.

Can anyone see what I’m doing wrong? Permissions on ~/.ssh/id_rsa are 644.

Thanks!
Tales

Hi @MrSir - I’m trying to achieve something similar via SSH. Feels like I’ve been going round in circles for most of the day now… trying this via both shell_commands & command_line.

I’m running home assistant OS on a RPi4 and want to SSH to a Lubuntu VM, the end game is to monitor the VPN status on the Lubuntu box so I can create automations based on this. I’m using HA OS.

I’ve shared the public key between the HA and the Lubuntu VM. When on the CLI within the HA web interface, I can SSH without any issues, even without including -i /data/.ssh/id_rsa. But when I create a command_line sensor (shown below) the SSH logs on the Lubuntu box indicate the authentication is failed based on password.

Config:

`` - platform: command_line

name: 'VPN Tunnel Status ssh command'

scan_interval: 60

command: ssh -i /data/.ssh/id_rsa -o StrictHostKeyChecking=no -q [email protected] uptime ``

Logs:

Jul 14 18:25:52 ubuntu sshd[7942]: Connection from 192.168.0.102 port 35238 on 192.168.0.101 port 22
Jul 14 18:25:52 ubuntu sshd[7942]: Failed none for matt from 192.168.0.102 port 35238 ssh2
Jul 14 18:25:52 ubuntu sshd[7942]: Failed password for matt from 192.168.0.102 port 35238 ssh2
Jul 14 18:25:52 ubuntu sshd[7942]: Failed password for matt from 192.168.0.102 port 35238 ssh2
Jul 14 18:25:52 ubuntu sshd[7942]: Connection closed by authenticating user matt 192.168.0.102 port 35238 [preauth]

Am I missing something due to the way that SSH is ran in a container on the HA? I’ve looked at various articles on SSH config and cannot seem to crack this by either changing the command, permissions on the files or with sshd config? Did you manage to solve @thermseekr?

Thanks

Hi @thermseekr & @crofthey,

Since posting this, I actually migrated all my SSH sensors over to either REST API’s, or MQTT calls to Node-Red (which in turn does the SSHing). So unfortunately I no longer have any active command_line platform components in my HA anymore to do any testing…

However, have you tried running the SSH command manually within the container as the HA user? It may wind up being a permissions issue or something as well…
@thermseekr - the permissions on the file are one thing, but the permissions in the whole directory structure leading to the key may need to be set properly as well.

I run my HA inside a FreeBSD jail (NOT HASSIO), so that may have some bearing on what does and doesn’t work. I don’t know for sure.

Good luck!