# iPhone Presence Detection with hping3 and ARP

**URL:** <https://community.openhab.org/t/iphone-presence-detection-with-hping3-and-arp/18171>\
**Category:** Tutorials & Examples\
**Tags:** tutorial, exec, iphone\
**Created:** [December 13, 2016, 8:12pm UTC](https://community.openhab.org/t/iphone-presence-detection-with-hping3-and-arp/18171 "2016-12-13T20:12:33Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![Seaside](https://community.openhab.org/user_avatar/community.openhab.org/seaside/32/70249_2.png) [@Seaside](https://community.openhab.org/u/Seaside)\
**Post date:** [December 14, 2016, 8:37pm UTC](https://community.openhab.org/t/iphone-presence-detection-with-hping3-and-arp/18171/3 "2016-12-14T20:37:45Z")

</div>

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 - #4 by rlkoshak](https://community.openhab.org/t/fine-tuning-of-wifi-presence-detection/12362/4)

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.

```auto
#!/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

```

---

_[View the full topic](https://community.openhab.org/t/iphone-presence-detection-with-hping3-and-arp/18171)._
