iPhone Presence Detection with hping3 and ARP

Hi!

Thanks for the script @Maurits28 . This helped me out a lot and improved my presence, I’m using a modified version, posting data using MQTT. Basically I have followed @rlkoshak presence guide: Fine tuning of wifi presence detection

Thought I would share it in case someone is looking to modify in a similar manner

Some differences

  • Loop to be able to do presence for multiple devices, I run this on a standalone Linux Server
  • I only flush the arp table for the device I’m looking for (not all, since my server have a lot of arp entries)
  • I look the MAC-address up, you only need to know the IP, which could be good and bad, this solution might not be as robust
  • I only arp the specified device, probably not an issue to do arp -an for all.
  • Relaying on mosquitto_pub to publish messages, which is a bit hard coded.
#!/bin/bash

 #Enter your ip of the devices here, separator space
DEVICES="10.1.1.20 10.1.1.21"
MQTT_SERVER="some ip"

for i in `echo $DEVICES`; do
    # Change dev and eth1 if needed
    ip neigh flush dev eth1 $i
    hping3 -2 -c 10 -p 5353 -i u1 $i -q >/dev/null 2>&1
    sleep 1
    # Only arp specific device, grep for a mac-address
    status=`arp -an $i | awk '{print $4}' | grep "..:..:..:..:..:.."`
    statusMessage="OFF"
    #A mac will be 17 characters including the ":"
    if [ ${#status} -eq 17 ]; then
        echo "Phone $i is detected!"
        statusMessage="ON"
    else
        echo "Phone $i is not present"
        statusMessage="OFF"
    fi
    if [ $i == "10.1.1.20" ]; then
        mosquitto_pub -h $MQTT_SERVER -m $statusMessage -t presence_sensors/network/person1 -q 1
	echo "Pub: $statusMessage -t presence_sensors/network/person1 -q 1"
    fi
    if [ $i == "10.1.1.21" ]; then
	mosquitto_pub -h $MQTT_SERVER -m $statusMessage -t presence_sensors/network/person2 -q 1
	echo "Pub: $statusMessage -t presence_sensors/network/person2 -q 1"
    fi  
done
3 Likes