iPhone Presence Detection with hping3 and ARP

Have you tried a variation of the script, that logs its output to a file and see what happens? See my modified example script down here. Make sure user openhab has write access to the log file.

#!/bin/bash

# v1.1 2017-04-11: VARIATION WITH LOGGING INCLUDED

# detect iphone by IP and MAC address.
# use MAC address too, to prevent false positives if IP might change
# return ON or OFF so output can be directly bound to a switch item

# number of retries, less is faster, but less accurate
MAXRETRIES=20

# path to log file - to use logging, uncomment all lines containing 'echo >> logfile'
LOGFILE=/etc/openhab2/scripts/iphone.log

# exit immediately if no parameters supplied
if [ $# -lt 2 ]
  then
    # echo `date '+%F %T'` - Required parameters not specified >> ${LOGFILE}
    echo "UNDEF"
    exit 1
fi

# Set variables
IP=$1
MAC=$2

# echo `date '+%F %T'` - Script started for ${IP} and ${MAC} >> ${LOGFILE}

COUNT=0
while [ ${COUNT} -lt ${MAXRETRIES} ];
do

  # echo `date '+%F %T'` - Try ${COUNT} >> ${LOGFILE}

  # Change dev and eth0 if needed
  sudo ip neigh flush dev eth0 ${IP}

  sudo hping3 -q -2 -c 10 -p 5353 -i u1 ${IP} >/dev/null 2>&1
  sleep .1

  # Only arp specific device, grep for a mac-address
  STATUS=`arp -an ${IP} | awk '{print $4}' | grep "${MAC}"`

  if [ ${#STATUS} -eq 17 ]; then
      # exit when phone is detected

      # echo `date '+%F %T'` - Phone detected >> ${LOGFILE}

      echo "ON"
      exit 0
  fi
  let COUNT=COUNT+1
  sleep .1
done

# consider away if reached max retries 
# echo `date '+%F %T'` - Phone not detected >> ${LOGFILE}
echo "OFF"

Thank you for help. I am not sure but I assume that the script is not running up to the last line of code if it is not finding a phone. The log contains just the last ‘Try’ entry but it is never achieving the command ‘echo off’.
Also the script is not running correct. It results in ‘Phone detected’ although I am not connected to the network. Otherwise If I run the script from the command line the detection is correct.

2017-04-12 22:10:00 - Try 0
2017-04-12 22:10:00 - Try 1
2017-04-12 22:10:01 - Try 2
2017-04-12 22:10:01 - Try 3
2017-04-12 22:10:02 - Try 4
2017-04-12 22:10:02 - Try 5
2017-04-12 22:10:03 - Try 6
2017-04-12 22:10:03 - Phone detected
2017-04-12 22:10:03 - Try 0
2017-04-12 22:10:04 - Try 1
2017-04-12 22:10:04 - Try 2
2017-04-12 22:10:05 - Try 3
2017-04-12 22:10:05 - Try 4
2017-04-12 22:10:06 - Try 5
2017-04-12 22:10:06 - Try 6
2017-04-12 22:10:07 - Try 7
2017-04-12 22:10:07 - Try 8
2017-04-12 22:10:08 - Try 9
2017-04-12 22:10:08 - Try 10
2017-04-12 22:10:09 - Try 11
2017-04-12 22:10:09 - Try 12
2017-04-12 22:10:10 - Try 13
2017-04-12 22:10:10 - Try 14
2017-04-12 22:10:11 - Try 15
2017-04-12 22:10:12 - Try 16
2017-04-12 22:10:12 - Try 17
2017-04-12 22:10:13 - Try 18
2017-04-12 22:10:13 - Try 19
2017-04-12 22:11:00 - Try 0
2017-04-12 22:11:00 - Try 1
2017-04-12 22:11:00 - Phone detected
2017-04-12 22:11:01 - Try 0
2017-04-12 22:11:01 - Try 1
2017-04-12 22:11:02 - Try 2
2017-04-12 22:11:02 - Try 3
2017-04-12 22:11:03 - Try 4
2017-04-12 22:11:03 - Try 5
2017-04-12 22:11:04 - Try 6
2017-04-12 22:11:04 - Try 7
2017-04-12 22:11:05 - Try 8
2017-04-12 22:11:05 - Try 9
2017-04-12 22:11:06 - Try 10
2017-04-12 22:11:06 - Try 11
2017-04-12 22:11:07 - Try 12
2017-04-12 22:11:07 - Try 13
2017-04-12 22:11:08 - Try 14
2017-04-12 22:11:08 - Try 15
2017-04-12 22:11:09 - Try 16
2017-04-12 22:11:09 - Try 17
2017-04-12 22:11:10 - Try 18
2017-04-12 22:11:10 - Try 19
2017-04-12 22:12:00 - Try 0
2017-04-12 22:12:00 - Try 1
2017-04-12 22:12:01 - Try 2
2017-04-12 22:12:01 - Phone detected
2017-04-12 22:12:01 - Try 0
2017-04-12 22:12:02 - Try 1
2017-04-12 22:12:02 - Try 2
2017-04-12 22:12:03 - Try 3
2017-04-12 22:12:03 - Try 4
2017-04-12 22:12:04 - Try 5
2017-04-12 22:12:04 - Try 6
2017-04-12 22:12:05 - Try 7
2017-04-12 22:12:05 - Try 8
2017-04-12 22:12:06 - Try 9
2017-04-12 22:12:06 - Try 10
2017-04-12 22:12:07 - Try 11
2017-04-12 22:12:07 - Try 12
2017-04-12 22:12:08 - Try 13
2017-04-12 22:12:08 - Try 14
2017-04-12 22:12:09 - Try 15
2017-04-12 22:12:09 - Try 16
2017-04-12 22:12:10 - Try 17
2017-04-12 22:12:10 - Try 18
2017-04-12 22:12:11 - Try 19

I have noticed some glitches in my own use of the modified script, too. I will have a look at it the coming days.

What I experienced during tests with the original script is that if you do the pings too close after each other, the results are often not reliable. Therefore I use an interval of one minute between the tests. It might help to test with one minute or 30 secs to get a reliable reading.

Little question :

It work really well with my girlfriend iPhone I have 0 issue ! But with mine … My iPhone goes offline really often … I try to set up a rules that said wait 20 min before declare my iPhone as gone but it does not fix it …

Could it be something like my iPhone 7 have more protection from ping when he sleep ? Or a physical issue with my iPhone ?

Does anyone have an iPhone 7 and make this work well ?

Hey all,

I have extended the network binding to be able to use arping. Additionally I send an empty packet to udp port 5353, as suggested here in this thread. I do not own an iPhone though, so cannot test if it actually works. The device presence detection for my android 6 phone works now, thanks to arping.

I need to know which parameters need to be configurable as apparently different parameters worked for different phones. At the moment it works like this:

Repeat the following every REFRESH_TIME in ms (default 60000):

  • Increase attempt by one
  • Send one empty packet to port 5353
  • Wait 50ms
  • Perform some arp pings with ARPING, but don’t wait longer than TIMEOUT in total

A device is deemed offline if attempt >= MAX_ATTEMPTS.

All uppercase parameters are configurable.

Is there a reason why the preferred solution here is to flush the arp table + use arp later instead of using the arping tool?

The pull request:

Cheers, David

4 Likes

Hi David,

Is there still a jar file which I can use to Test the arp ping feature of the network binding?

Have a nice weekend!

No not atm. I’ll make one later if time permits.

Cheers

@David_Graeff I’m really looking forward to the arping patch landing, is there an ETA? This would help dramatically with cross platform (i.e. Mac) support

The test suite does not run with the maven configuration on the test server (travis) and stalled the whole merge. Someone with maven knowledge need to have a look. The binding is working though, but I do not have time to manually generate jar files.

The ETA is: As soon as someone else than me understood the maven problem, the binding will be merged. Might be tomorrow or next year :confused:

Cheers, David

As soon as someone else than me understood the maven problem, the binding will be merged. Might be tomorrow or next year

You are lucky @johnofcamas haha. It is more likely tomorrow than later, the error got fixed and the Pull Request got approved.

Cheers, David

3 Likes

:+1:

Have you been able to resolve the glitches discussed on your 4/13 post?

I have followed your setup below:
Openhabian on RPi3
OH2
iPhone 7

  1. Installed hping3
  2. Installed Exec 2.1.0 binding
  3. Created the iphone.sh script and placed it in my scripts folder and assigned permissions
  4. Added the items
  5. Added rule with my MAC and IP addresses

In my rules for the exec line I’m getting the error below on the following line:

   var String result1 = executeCommandLine('/etc/openhab2/scripts/iphone.sh@@192.168.2.117@@B8:53:AC:8D:33:33', timeout)

Type mismatch: cannot convert from number to int

Thanks for your assistance!

You don’t show how timeout is defined but I bet if you changed the line to:

...:33', timeout.intValue)

it will work.

I’ve added the intValue and that did take care of the error in Eclipse.

However, I think I may have a permissions issue because I’m now seeing the errors/warning below in FrontTail:

2017-08-28 21:05:00.146 [WARN ] [lipse.smarthome.io.net.exec.ExecUtil] - Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program "/etc/openhab2/scripts/iphone.sh" (in directory "."): error=2, No such file or directory)
2017-08-28 21:05:00.153 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule Find My iPhone: The argument 'state' must not be null or empty.

and this one too

2017-08-28 21:28:05.771 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'iphone.sh' has errors, therefore ignoring it: [1,1]: missing EOF at '#'

I checked the directory listed above via Putty and the iphone.sh file is there.

I copied the code from the comments above, but here is what I have in my iphone.sh file:

#!/bin/bash

# v1.1 2017-04-11: VARIATION WITH LOGGING INCLUDED

# detect iphone by IP and MAC address.
# use MAC address too, to prevent false positives if IP might change
# return ON or OFF so output can be directly bound to a switch item

# number of retries, less is faster, but less accurate
MAXRETRIES=20

# path to log file - to use logging, uncomment all lines containing 'echo >> logfile'
LOGFILE=/etc/openhab2/scripts/iphone.log

# exit immediately if no parameters supplied
if [ $# -lt 2 ]
  then
    # echo `date '+%F %T'` - Required parameters not specified >> ${LOGFILE}
    echo "UNDEF"
    exit 1
fi

# Set variables
IP=$1
MAC=$2

# echo `date '+%F %T'` - Script started for ${IP} and ${MAC} >> ${LOGFILE}

COUNT=0
while [ ${COUNT} -lt ${MAXRETRIES} ];
do

  # echo `date '+%F %T'` - Try ${COUNT} >> ${LOGFILE}

  # Change dev and eth0 if needed
  sudo ip neigh flush dev eth0 ${IP}

  sudo hping3 -q -2 -c 10 -p 5353 -i u1 ${IP} >/dev/null 2>&1
  sleep .1

  # Only arp specific device, grep for a mac-address
  STATUS=`arp -an ${IP} | awk '{print $4}' | grep "${MAC}"`

  if [ ${#STATUS} -eq 17 ]; then
      # exit when phone is detected

      # echo `date '+%F %T'` - Phone detected >> ${LOGFILE}

      echo "ON"
      exit 0
  fi
  let COUNT=COUNT+1
  sleep .1
done

# consider away if reached max retries 
# echo `date '+%F %T'` - Phone not detected >> ${LOGFILE}
echo "OFF"
     
1 Like

The snapshot version of the network binding might replace this script based solution :slight_smile:

Cheers, David

2 Likes

I read on the forums that the network binding didn’t work well with an iPhone. Has that now changed with the latest binding?

Yes. Check a few postings up.

The /etc/openhab2/scripts folder is intended for openHAB scripts, not shell scripts that one would execute with the exec binding. When OH starts up it changes the permissions on all the files in /etc/openhab2 and strips off the execute permissions.

Move this file somewhere else (e.g. /var/lib/openhab2/scripts which is equivalent to ~openhab I believe) and make sure it has exec permissions.

Does one need to run OH as root for the arping to work? If not do you have any idea whether this might work with OH running in Docker? I’ve never been able to get the dhcplisten to work from Docker so am not hopeful.

Is there anything to install besides the Snapshot binding to make arping work?