iPhone Presence Detection with hping3 and ARP

actually I did both, result is when use sudo, I don’t require password, but arping still require sudo,

I was fooled by this fact:

When testing the arping command from root@shs2:/home/omr# su -l openhab -s /bin/bash running just the /usr/sbin/arping command seems to work, but providing actual parameters it will tell you you need to be root.

After fiddeling with visudo and entries in /etc/sudoers for hours, I ended up running root@shs2:/home/omr# chmod u+s /usr/sbin/arping. Then it worked!

I can even see in top (-V) that arping is called regularly. But, my Android phone is not seen, even though manual arping detects it. I suspect that is because on Ubuntu, my lan port is not named eth0, but enp3s0:

openhab@shs2:~$ /usr/sbin/arping -c 1 -i enp3s0 192.168.1.50
ARPING 192.168.1.50
60 bytes from 60:f1:89:47:b9:b7 (192.168.1.50): index=0 time=190.029 msec

Could this make the arping call from the binding fail do you think @David_Graeff?

Will try out DHCP tip next :slight_smile:

Could you please share how you did this?

I tried chmod u+s /usr/sbin/arping. but result still the same
it’s even getting worse, I got same warning even use sudo,

Hmm, I also, in the heat of the battle, issued a chmod 777 /usr/sbin/arping command. Maybe the combo is golden?

I gave up, I found run script at router response and reliability much better,
I shall monitor closely for 48 hours.

I modify @Seaside script and run it on router, result looks more reliable than I think



#!/bin/bash

 #Enter your ip of the devices here, separator space
DEVICES="192.168.0.1 192.168.0.2"

for i in `echo $DEVICES`; do
    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 == "192.168.0.1" ]; then
        curl --max-time 2 --connect-timeout 2 --header 'Content-Type: text/plain' --request PUT --data '${statusMessage}' http://192.168.0.255:8080/rest/items/iPhoneA_Online/state
		echo "A's iPhone is ${statusMessage}"
    fi
    if [ $i == "192.168.0.2" ]; then
        curl --max-time 2 --connect-timeout 2 --header 'Content-Type: text/plain' --request PUT --data '${statusMessage}' http://192.168.0.255:8080/rest/items/iPhoneB_Online/state
        echo "B's iPhone is ${statusMessage}"
    fi
done


ARP needs special permissions and I can’t change that fact.

Flushing the arp table every time for a phone presence detection is not the solution, especially not if the script needs root to run.

And no we don’t hardcode eth0 into the code but test on every interface instead actually. What does the properties say?

just a clarification needed.

do i suppose to see the below log entry for a device which is actually not even connected to the network (physically not there) ?

Some device on the network is requesting the address. It is only logged if a DHCP packed has been received.

Thanks for that tutorial. Easy to follow. Just one question. I installed HPING3 yesterday following this tutorial. IphoneD.sh is the exact same as provided (excepted for the Device ID).

The return string in OH2 is not “ON” or "OFF, but the echo string from the scrip: “Phone xxx.xxx.xxx.xxx is detected!” or “Phone xxx.xxx.xxx.xxx is not present”.

I replaced “OFF” by “Phone xxx.xxx.xxx.xxx is not present”. It’s working now, but I don’t understand why the statusMessage from the script is not returned.

1 Like

Hi I was wondering how this approach differs from using the HTTP binding?

I don’t know how one would even approach iPhone detection using the http binding.

This uses a special command line too called hping3 to send a special type of pocket to wake up the phone then looks for the phone’s response to see is it is still on the network despite being asleep and not responding to pings.

The newest Network binding does this with arping so a separate script like this is no longer needed. There is also an iCloud binding that can get the phone’s location through Apple’s servers.

1 Like

Thanks for the good explanation - even for beginners
I implemented as you described and the script runs quite nice when executed from the command line.
But after setting up in openhab2 i always get the following error
2017-11-16 15:35:13.142 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule Find My iPhone: The argument ‘state’ must not be null or empty.

Hopefully any ideas?

Post your rule. Please use code fences:

```
code goes here
```

Also, be aware that the newest version of the Network Binding supports this through arping and if you have an iPhone there is a iCloud Binding, also available in the SNAPSHOT that works as well for iPhone presence detection.

Maybe i should give it a try. Can you point me in the right direction how to use network binding or icloud binding instead?
Thanks

OK - I tried the example from the network binding page.
i manually added a thing, item and a sitemap. installed arping. everything fine - no errors.
But the outcome for all channels (online/lastseen/latency) is always “-”.
What am i doing wrong?

things

network:pingdevice:iphoneS [ hostname="192.68.1.171" ]

items

Switch PhoneS { channel="network:pingdevice:iphoneS:online" }
Number PhoneSResponseTime { channel="network:pingdevice:iphoneS:latency" }
DateTime PhoneSlastseen { channel="network:pingdevice:iphoneS:lastseen" }

sitemap

sitemap iphone label="Main Menu"
{
        Frame {
                Text item=PhoneS label="iPhone Stefan [%s]"
                Text item=PhoneSlastseen label="lastSeen [%s]"
                Text item=PhoneSResponseTime label="Response Time [%s]"
        }
}

What version of OH are you running? I think you need to be on a SNAPSHOT past around 1050 or so to pick up those changes that support arping.

See the following for iCloud integration:

I have been working for hours now on my presence detection rule following the tutorial by @Maurits28 in the first post.
Ive got the script working and i even verified that it updates the correct item.
This is my modified version of the rule:

var Number SmartphoneCounter = 0
val Number MaxCounter = 18

rule "Execute script SmartphonePresence"
when
    Time cron "0 * * * * ?"
then
    var String Polling = executeCommandLine("sudo@@bash@@/opt/RunarMobil.sh", 5000)
    RunarMobil.postUpdate(Polling)
	logInfo("Test", Polling)
end

rule "Determine presence of Runarmobil"
when 
	Item RunarMobil received update
then {
	if(RunarMobil.state == "OFF") {
		SmartphoneCounter = SmartphoneCounter + 1
		if(SmartphoneCounter > MaxCounter) {
			RunarMobil_Sw.sendCommand(OFF)
			SmartphoneCounter = 0	
			logInfo("RULE","Smartphone Counter reached threshold and sent command OFF. Counter set to 0")
		}
		else {
			logInfo("RULE","Counter Smartphone threshold not yet reached. Counter set to " + SmartphoneCounter)
		}
	}
	else {
		RunarMobil_Sw.sendCommand(ON)	
		SmartphoneCounter = 0
		logInfo("RULE","RunarMobil_Sw received command ON. Counter reset to 0")
	}
	}
end

And here is what the log shows:

2018-01-06 14:56:01.359 [INFO ] [.eclipse.smarthome.model.script.RULE] - RunarMobil_Sw received command ON. Counter reset to 0

2018-01-06 14:57:01.397 [INFO ] [.eclipse.smarthome.model.script.Test] - Smartphone 10.0.0.110 is not present

==> /var/log/openhab2/events.log <==

2018-01-06 14:57:01.416 [ome.event.ItemCommandEvent] - Item 'RunarMobil_Sw' received command ON

==> /var/log/openhab2/openhab.log <==

2018-01-06 14:57:01.416 [INFO ] [.eclipse.smarthome.model.script.RULE] - RunarMobil_Sw received command ON. Counter reset to 0

2018-01-06 14:58:01.334 [INFO ] [.eclipse.smarthome.model.script.Test] - Smartphone 10.0.0.110 is not present

==> /var/log/openhab2/events.log <==

2018-01-06 14:58:01.351 [ome.event.ItemCommandEvent] - Item 'RunarMobil_Sw' received command ON

==> /var/log/openhab2/openhab.log <==

2018-01-06 14:58:01.363 [INFO ] [.eclipse.smarthome.model.script.RULE] - RunarMobil_Sw received command ON. Counter reset to 0

2018-01-06 14:59:01.341 [INFO ] [.eclipse.smarthome.model.script.Test] - Smartphone 10.0.0.110 is not present

==> /var/log/openhab2/events.log <==

2018-01-06 14:59:01.364 [ome.event.ItemCommandEvent] - Item 'RunarMobil_Sw' received command ON

==> /var/log/openhab2/openhab.log <==

2018-01-06 14:59:01.377 [INFO ] [.eclipse.smarthome.model.script.RULE] - RunarMobil_Sw received command ON. Counter reset to 0

2018-01-06 15:00:01.342 [INFO ] [.eclipse.smarthome.model.script.Test] - Smartphone 10.0.0.110 is not present

==> /var/log/openhab2/events.log <==

2018-01-06 15:00:01.373 [ome.event.ItemCommandEvent] - Item 'RunarMobil_Sw' received command ON

==> /var/log/openhab2/openhab.log <==

2018-01-06 15:00:01.372 [INFO ] [.eclipse.smarthome.model.script.RULE] - RunarMobil_Sw received command ON. Counter reset to 0

2018-01-06 15:01:01.362 [INFO ] [.eclipse.smarthome.model.script.Test] - Smartphone 10.0.0.110 is not present

==> /var/log/openhab2/events.log <==

2018-01-06 15:01:01.385 [ome.event.ItemCommandEvent] - Item 'RunarMobil_Sw' received command ON

==> /var/log/openhab2/openhab.log <==

2018-01-06 15:01:01.386 [INFO ] [.eclipse.smarthome.model.script.RULE] - RunarMobil_Sw received command ON. Counter reset to 0

2018-01-06 15:02:01.387 [INFO ] [.eclipse.smarthome.model.script.Test] - Smartphone 10.0.0.110 is not present

2018-01-06 15:02:01.407 [INFO ] [.eclipse.smarthome.model.script.RULE] - RunarMobil_Sw received command ON. Counter reset to 0

==> /var/log/openhab2/events.log <==

2018-01-06 15:02:01.407 [ome.event.ItemCommandEvent] - Item 'RunarMobil_Sw' received command ON

To me it looks like the rule “Execute script SmartphonePresence” in combination with the script does what its supposed to do. Im wondering if someone with the right expertise can spot the problem. I’ve spent hours upon hours now looking for the problem but cannot figure this out. My prime suspect is those curly brackets hanging around inside the rule and make no sense to me.

1 Like

Looked up the item receiving the status update from the script using Openhab Rest API - http://openhabianpi:8080/doc/index.html#!/items/getPlainItemState
Seems like we have found a clue on what’s going on we my failing presence detection. :slight_smile:
This is the response:

Response Body
Smartphone 10.0.0.110 is not present
Response Code
200
Response Headers
{
  "content-length": "36",
  "server": "Jetty(9.3.22.v20171030)",
  "content-type": "text/plain"
}

wtf? I was expecting a ON or OFF value here… If this is the string output i clearly cannot use this item in the if-statement in my rule:

if(RunarMobil.state == "OFF")

I’m wondering if im supposed to use a second item (the device item) in this part of the rule?