[SOLVED] Help with command line sensor for identifying if devices are connected to guest network subnet

I’m attempting to create a command line sensor that ssh’s into my router and reads the output from an ip neigh show command to determine whether there are any devices connected to the 192.168.102.* subnet. the sensor would then display Yes or No, depending on if any devices are connected.

A very helpful user on reddit suggested this as a method to help me automate connections to a guest network. I think it’s a really novel solution to what i’m after, especially because the ASUSWRT integration is not particularly helpful in this case. I basically want to flip an input_boolean on whenever there is a device connected to that particular subnet, and flip it off when there are no devices connected.

However, I can’t seem to get the sensor to report correctly and wonder if it’s not an issue with syntax. I have set up the relevant SSH keys and am able to manually connect to my router (an Asus XT8) using them via the HA Terminal add-on. In fact, the sensor appears to be online but it reports that there are no devices connected when there are in fact several on that subnet.

What’s weird is that the guy helping on Reddit says that this code (and a few other variations we tried) is working for him. So i’m a bit stumped as to what to try next.

I was hoping to throw this dilemma out to the wider HA community in the hopes that someone might be able to look at the code and see anything we might have missed?

My config.yaml looks as follows:

sensor:    
  - platform: command_line
    name: Guests
    command: 'ssh -i /config/ssh/ssh_asus_private_key -o "StrictHostKeyChecking=no" [email protected] ip neigh show | grep 192.168.102 | grep -qv FAILED && echo "Yes" || echo "No"'
    scan_interval: 15

Sensor incorrectly reporting that it found no devices with the ip 192.168.102.*

Router log reporting that connection was successful:

EDIT: sorry, the yaml got a bit messed up because it thought my login was an e-mail address haha

What happens if you run the following command manually?

ssh -i /config/ssh/ssh_asus_private_key -o "StrictHostKeyChecking=no" [email protected] ip neigh show | grep 192.168.102 | grep -qv FAILED && echo "Yes" || echo "No"

Or even better, run only the first command in the pipeline

ssh -i /config/ssh/ssh_asus_private_key -o "StrictHostKeyChecking=no" [email protected] ip neigh show

and take a look at it’s output.
The rest of the pipeline should work like this:
The first grep takes a look at the output of ip neigh and passes all lines that contain 192.168.102 to the next program in the pipeline (the second grep).
The second grep exits with 0 if there is a line that does not contain “FAILED” and otherwise exits with 1.
If the second grep exited with 0, echo "Yes" runs. Otherwise, echo "No" runs.

It seems to work for me (outside of Home Assistant). Please try it in the terminal to rule out the possibility that the command is wrong.

thanks for your input and the explanation!

So… i ran both commands from Terminal as is and they both spat back the following:

sh: ip: not found

I then put a | between the ip address and ip neigh show as follows:

ssh -i /config/ssh/ssh_asus_private_key -o "StrictHostKeyChecking=no" [email protected] | ip neigh show

and this spat back a list of IP addresses showing various states (REACHABLE, STALE, FAILED) - so, progress!

Tried the same with the full command, however this only seemed to just hang, i got no output. It seems that there’s some kind of bottleneck between the ip neigh show and the first grep

EDIT: actually, for the full command (without the extra | added), the output is:

sh: ip: not found
No

If you put a pipe symbol | between the ssh command and ip neigh show, you are running ip neigh show locally and just piping ssh’s output to it’s stdin, which does nothing. You are not actually running the ip command on your router.

There could be something wrong with your PATH. Connect to your router through ssh and run which ip. It should print the full path to the ip command (e.g. /usr/sbin/ip). Put it in this command:

ssh -i /config/ssh/ssh_asus_private_key -o "StrictHostKeyChecking=no" [email protected] /usr/sbin/ip neigh show
1 Like

ahh, i see. that makes sense.

And, also, it seems we’ve had some success! ran which ip and got the same PATH you used as an example. added this to both commands and they both gave the right output. the shorter command spat back the list of IP address and the the full command outputted that elusive Yes that i’ve been searching for for days!

Will now try and put this into my yaml and see if it works…

OK, that seems to have moved the needle! Thanks so much, mate - if i could high-five you i 100% would!

Sensor is now reporting Yes

However, I took the device off the guest network to test and after about 2-mins it reported it as No but then jumped back to Yes and the No again.

I wonder if it has something to do with the following INCOMPLETE entry in the ip neigh show output:

EDIT: sensor seems to be jumping between Yes and No even though there is nothing connected on that subnet now. I guess, it could just be a matter of activating the resulting input_boolean i want to make if Yes is active for X seconds/minutes. Not ideal, but i’ll take what i can get haha

Yeah, it looks like that INCOMPLETE is causing the sensor to spit back a false positive. @ondras12345 Is it possible to have the sensor ignore any INCOMPLETE entries and just look at REACHABLE, FAILED and STALE?

Sure, just filter them out

ssh -i /config/ssh/ssh_asus_private_key -o "StrictHostKeyChecking=no" [email protected] /usr/sbin/ip neigh show | grep 192.168.102 | grep -v INCOMPLETE | grep -qv FAILED && echo "Yes" || echo "No"
1 Like

Amazing, thank you! That seems to have done the trick.

Honestly, i’m in awe of both the power and potential of Home Assistant but also the kindness of the community around it. I’ve been trying to sort out this particular guest network use case for a few weeks now and knew that there must be some way to do it. And, sure enough, with the help of kind strangers like yourself it was indeed possible.

I appreciate the help immensely. thanks again :slight_smile: