iPhone Presence Detection with hping3 and ARP

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?

The binary arping needs to be installed as explained in the snapshot readme. The executable needs suid permission to be usable for OH.
Dhcplisten always starts now. But without the setcap permissions it runs on port 6767 and needs port forwarding.

Cheers David

I moved the file and I still get the same error. I’ll try to use the Network Binding to see if I can get presence to work for the iPhone.

Just to be clear, you moved the file, and made sure it has read and execute permissions for the openhab user. And you updated the path in your rule that calls the script. After all that it gives the same error?

Yes, the file is now in /var/lib/openhab2/scripts.

I ran the commands below (my user is OpenHABian):
sudo chown openhabian iphone.sh
sudo chmod +x iphone.sh

Items:

Switch Venice "Karen iPhone [%s]"  <present> (Presence)  
Switch Peggy  "Peggy iPhone [%s]"  <present> (Presence)  

Rule updated to:

rule "Find My iPhone"
when
    Time cron "0 */5 * * * ? *" // every 5 minutes
then
    val Number timeout = 10*1000 // give the script some time to finish, to capture the result

    // replace 0.0.0.0 with your ip address
    // replace 00:00:00:00:00:00 with your mac address
    var String result1 = executeCommandLine('/var/lib/openhab2/scripts/iphone.sh@@<192.168.2.117@@B8:53:AC:8D:33:33', timeout.intValue)
    var String result2 = executeCommandLine('/var/lib/openhab2/scripts/iphone.sh@@192.168.2.132@@4C:74:BF:68:7B:A1', timeout.intValue)

    Venice.postUpdate(result1)
    Peggy.postUpdate(result2)

end

No, not your user, the user that openHAB runs as, i.e.openhab.

sudo chown openhab:openhab iphone.sh
sudo chmod ug+rx iphone.sh