SSH Sensor

I’ve been having some issues with the site to site VPN on my UniFi USG and some details are only available at the command line after logging in via SSH. There is no apparent way to access this data any other way.

The result is a SSH sensor, which performs a remote login to a server, executes a command and returns the result.

Based on the Unifi AP Direct device_tracker source and the pexpect library.

Now available at

4 Likes

Very nice,
is there a way to query this sensor only on demand. In my case i need a value once a day or only when i trigger it.

Geetings from germany

This is in 0.81.0

We have a new service home_assistant.update_entity to request the update of an entity, rather than waiting for the next scheduled update. This means you can set a really long scan_interval in the platform_options and then update on demand, ideal for those services where you have low API limit.

1 Like

Sensor has now belatedly been update to the new file structure

Hi

SSH password is still required. Kinda not safe…How about SSH key??

Is it possible to use several commands to get data?

I have modem which gives response only after several commands:
When logging I have this:

ADB#

to get data I need to enter:

ADB#configure (enter)
ADB(cfg)#interface (enter)
ADB(cfg-intf)#dsl Line1 (enter)
ADB(cfg-intf-dsl:Line1)#show_dslChannels (enter)

only then I get data:

DSL Channel1                   |  LowerLayer Line1
----------------------------------------------------------------------
Current upstream bit rate      |  5119
Current downstream bit rate    |  42091
----------------------------------------------------------------------

Would it be possible with this sensor?

I believe you should be able to simply put in semi colons to issue a series of commands, e.g.

cd testdirectory;ls

would show the contents of the sub-directory rather than the current one. I believe that should work, although I havn’t tested. Please let us know how you get on.

Tried with command: configure;interface;dsl Line1;show_dslChannels

Got this error:

Error while setting up platform ssh
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 149, in _async_setup_platform
    await asyncio.wait_for(asyncio.shield(task), SLOW_SETUP_MAX_WAIT)
  File "/usr/local/lib/python3.7/asyncio/tasks.py", line 442, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.7/asyncio/coroutines.py", line 120, in coro
    res = func(*args, **kw)
  File "/config/custom_components/ssh/sensor.py", line 48, in async_setup_platform
    dev.append(SSHSensor(hass, config))
  File "/config/custom_components/ssh/sensor.py", line 66, in __init__
    self._connect()
  File "/config/custom_components/ssh/sensor.py", line 137, in _connect
    password=self._password, port=self._port)
  File "/usr/local/lib/python3.7/site-packages/pexpect/pxssh.py", line 434, in login
    self.before, self.PROMPT,))
pexpect.pxssh.ExceptionPxssh: could not set shell prompt (received: b" unset\r\nERROR: unknown command\r\nADB# \x07PROMPT_COMMAND\r\nERROR: unknown command\r\nADB# \x07PS1='[PEXPECT]\\$\r\nERROR: unknown command\r\nADB# \x07'\r\nERROR: unknown command\r\nADB# \x07set\r\nERROR: unknown command\r\nADB# \x07prompt='[PEXPECT]\\$\r\nERROR: unknown command\r\nADB# \x07'\r\nERROR: unknown command\r\nADB# \x07", expected: '\\[PEXPECT\\][\\$\\#] ').

In last line it does get to ADB# part so I guess login was successful.

John,
Thanks for the project. It was almost exactly what I needed, but I couldn’t get it working with the pexpect library for my usage needs. So, I developed a similar sensor and also a switch component that works across the ssh tunnel using the paramiko library. Any interest in consolidating and submitting to the core code of hass?

To all,
So far, I am still working out the final bugs, so, use my code at your own risk.

1 Like

Yes my time to spend on this is very limited currently, but happy for you to take things on and will help where I can!

Just wanted to follow up with anyone who might find this thread. I’ve created an alternative ssh custom_componet that allows both a sensor and/or switch.

Hope it provides value to someone else. Cheers!

2 Likes

Thank you! I’ve installed your component and was able to create a switch that enables/disables internet on particular device using OpenWRT firewall rule. Here is my config that might save somebody some time as an example:

switch:
  - platform: ssh
    scan_interval: 60
    host: 192.168.0.1
    name: 'Tablet Internet'
    username: root
    password: !secret openwrt_password
    key: AAAAB3Nza...qhgPknn
    command_on: '/root/enable_tablet_internet.sh'
    command_off: '/root/disable_tablet_internet.sh'
    command_status: '[[ `uci get firewall.cfg2b92bd.enabled` = 0 ]] && echo on || echo off'

I had issues with key field. It is mandatory but in case base64.b64decode function fails there is “Connection refused. SSH enabled?” error anyway. So it’s important for key to be in base64 format.

2 Likes

Can you share info about base64 key ? i have same problem

[custom_components.ssh.sensor] Connection refused. SSH enabled?

Sorry, I’ve already shared everything I can. As I remember, that kind of error happens not only if connection is really refused, there might be other reason as well.

How do you get firewall.cfg2b92bd ? When I do uci show firewall I only get firewall.@rule[13] etc, how to get some constant rule name ?

I found a name at unapplied changes inside Luci interface. Just pressed Save instead of Save&Apply and checked the name of the rule. Not sure it is truly persistent but it didn’t change since that time.

1 Like

your project seems dead but this one works:

Why it stopped to work ?

Logger: homeassistant.components.sensor
Source: custom_components/ssh/sensor.py:136
Integration: Сенсор (documentation, issues)
First occurred: 20:30:53 (1 occurrences)
Last logged: 20:30:53

Error while setting up ssh platform for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 249, in _async_setup_platform
    await asyncio.shield(task)
  File "/usr/local/lib/python3.9/asyncio/coroutines.py", line 124, in coro
    res = func(*args, **kw)
  File "/config/custom_components/ssh/sensor.py", line 48, in async_setup_platform
    dev.append(SSHSensor(hass, config))
  File "/config/custom_components/ssh/sensor.py", line 66, in __init__
    self._connect()
  File "/config/custom_components/ssh/sensor.py", line 136, in _connect
    self._ssh.login(self._host, self._username,
  File "/usr/local/lib/python3.9/site-packages/pexpect/pxssh.py", line 386, in login
    raise ExceptionPxssh('Could not establish connection to host')
pexpect.pxssh.ExceptionPxssh: Could not establish connection to host

image