Use Arp intead of Nmap

Hi,

I hope this will be the correct section of the forum, if not please move it.

I’ve been using HA on a Pi for a while, using nmap as main presence detection entity for LAN as I don’t own any of the supported routers.

The thing is that using nmap, I’m not able to “discover” all the devices, specially mac/iphone/ipad/windows neither with different settings in “scan-options” (some takened from this or other forums and some from myself). The same is if I run the commands form the pi’s command line.

But I realized that if I run the command “arp” in the command line, obviously I’m able to know all the connected devices, as this command check the connectivity in another OSCI layer and any firwall will block my query, (Actually I have a python program linked to ttasker that tells who is home after get all the IP’s associated to a mac address with the arp command)

So, it’s possible to run the “arp” command in HA as a device tracker option?

I also noticed arp issue, it can be a good option.

Watch though that things being in the arp cache doesn’t mean they’re currently present, it could be 10 minutes, or more, before a departed item is no longer listed.

Typically we want to trigger on first arrival, but a lagging departure is ok. In such a case the arp cache is not a big problem.

I’m not an expert when it comes to networking. But having looked at arp-caches before I noticed, that entries only are being added when communication with the device has happened before. So using arp as a method to determine presence would work if the machine which cache would be looked at would always be contacted by all relevant clients. That would be the case if the machine acts as an firewall, dns etc… But a simple pi just sitting there in the network wouldn’t notice if a smartphone was in the network as long as the phone wouldn’t communicate with the pi.

Maybe in real life it’s different. But those were my observations in the past.

Good point, I run pi-hole on the same rPI so arp cache should work.

@danielperna84 is correct. The ARP cache only gets updated when the HA box tries to connect to the remote box. Thus the ARP cache does not necessarily contain mappings for all of the devices on the network. However, a combination of NMap (to fill the ARP cache) and then querying the ARP table might work.

I would think about the possibility to ping by MAC address, as that one is not changing over the time. First advise would be to ping the whole subnetwork and then analyse ARP table, but I would prefer to use something like arping (that one is an enhanced clone of arping from iputils).

For presence sensing, all you need to do is ping the known device’s IP address, once you’ve bound or ‘reserved’ a static IP address for it in your router. Add this to configuration.yaml :

binary_sensor:   # ping monitoring by IP ---- ADD, SAVE, RESTART HOST
  - platform: ping 
    host: 192.168.0.1
    name: Router .1
    count: 3
    scan_interval: 15

You will have to know the devices MAC address first, which you can also determine using you router. The router will always give it the reserved IP address every time it re connects, even if the device itself is not configured to be static. Then you can use this sensor however you want. At a glance, I can see if any critical devices are offline.

That is clear from the beginning and probably the right way to do. However has it’s drawbacks: every time I change a phone, I need to make a new entry in a router. And any time when internet provider updates the router, I need to copy the table :pensive: All of those do not happen often, say, once a year. What is more disappointing that configuration (or “knowledge”) is spread across several devices.