BEGINNER Guide to iPhone Presence?

Tags: #<Tag:0x00007f5c9ece9648>

(Karen Mitchell) #26

No, I use Designer to edit any OpenHAB files since it will show line errors in the file, but I did have to create this one using Notepad. I do have Notepad++, so I’ll use it in the future. I recreated the script in Nano and can view it in Designer with no errors. When I run


I get:

-bash: /etc/openhab/configurations/scripts/ Permission denied

I tried running it as “sudo /etc/openhab/configurations/scripts/”

and I get the error “command not found”. I went to the exact directory to verify the script is there and still get the same error:

pi@raspberrypi:/etc/openhab/configurations/scripts $ ls
demo.script  iPhoneDetect.original  README
pi@raspberrypi:/etc/openhab/configurations/scripts $
-bash: command not found

Glad it works for you! I would try it, but as I said in my original post, I couldn’t get any items to show up in IFTTT and searching through the forums didn’t have a single post with the solution.

(Stefan) #27

Please try a

ls -l

in your script directory and post the result here. We have so see if your script is executable (that was maybe the reason for the “permission denied”). Furthermore you made a second try starting the script with “”, but the correct name is “” (with a capital P). Maybe that’s the reason why you became a “command not found”.

You can try to execute the script (while you are in the correct directory) with: ./

I have a working iPhone detection using the exec-Binding and a Bash script, just like you want to do it. It was mainly a matter of permissions and user rights to get this working. But as Rich already mentioned, this is clearly no real beginner stuff. You should at first get a little more comfortable with Linux as an OS.

(Karen Mitchell) #28

results of ls -l:

pi@raspberrypi:/etc/openhab/configurations/scripts $ ls -l
total 16
-rw-r--r-- 1 openhab openhab  36 May 22 17:24 demo.script
-rw-r--r-- 1 openhab openhab 245 Sep 17 11:57 iPhoneDetect.original
-rw-r--r-- 1 openhab openhab 231 Sep 19 08:27
-rw-r--r-- 1 openhab openhab 230 May 22 15:44 README

When I try to run ./

pi@raspberrypi:/etc/openhab/configurations/scripts $ ./
-bash: ./ Permission denied

(Stefan) #29

Ok. We’ll getting there. :wink:

Your scripts aren’t executable. You have to have an “x” before the “rw” and ideally also before the second and third “r”.

chmod 777

It isn’t the perfect solution, but with this command we can rule out some other problems.
Then try again to execute the script. Maybe do another ls - l before to verify that the “x” have been set.

(Karen Mitchell) #30

I had to use sudo chmod 777, it looks like it worked?

-rwxrwxrwx 1 openhab openhab 231 Sep 19 08:27

When I ran the script it returned “Absent” - which is progress!!

How do I permanently set the permissions on the script?

(Stefan) #31

They are permanently set. The difference is: When you manually start the script (like you just did), you execute it as User “pi”. When Openhab executes the script with the exec binding, it executes the script with the user “openhab”. We have to make sure that this is allowed (which we did with chmod 777).

Next step: Please quote the current content of your script. I want to compare it with my script (but I can’t do this until this evening when I am at home). Try to get this with using nano on your ssh shell (type: nano

Furthermore we have to take care that the included commands in the script (like hping3) also are allowed to be executed by the user openhab. We have to edit the /etc/sudoers file for this. But one step at a time…

You see: No rocket science, but quite a bunch of stuff to consider…

(Karen Mitchell) #32
declare -a DEVICES
hping3 -2 -c 10 -p 5353 -i u1 -q >/dev/null 2>&1
DEVICES=`arp -an | awk '{print $4}'`
if [[ $DEVIES[*]} =~ $CHECK ]];then
	echo "Present"
	echo "Absent"
fi is my iPhone’s IP address connected to my home wifi
my:mac:addr is my iPhone’s mac address when connected to my home wifi

(Stefan) #33

Next step: Have you already installed hping3 like Max said:

Try it with simply executing the command hping3 on your ssh shell. It makes no sense but you can see if the program is installed. Otherwise install it with sudo apt-get install hping3.

And check if your mac address in the script is written in lower cases (aa:bb:cc instead of AA:BB:CC). For me it didn’t work with upper cases.

(Stefan) #34

Next step (as mentioned before): Edit the /etc/sudoers file. Execute the following command on your shell:

sudo visudo

Now you have started an editor and opened the sudoers file. Scroll down to the very end of the file and add the following lines:

openhab ALL = NOPASSWD: /usr/sbin/hping3
openhab ALL = NOPASSWD: /usr/sbin/arp

This allows the programs of the script to be executed by the user openhab. Save your changes and exit the editor.
CAUTION: This is a sensible file. Don’t mess up the file or modify/delete any of the other content.

(Stefan) #35

Checking your script I found one (maybe two) problems:

  1. There is a typo. You wrote “DEVIES” in line 6. It has to be “DEVICES” (like in line 2 or 4). Don’t know where this typo comes from as Rich has written it correctly. Anyway…

  2. This could be unnecessary, but I have an additional “sudo” before hping3 (sudo hping3 -2 -c …).

Other than that, the script seems to be identical to mine. With the above mentioned extra steps this should lead to a correct output now.

(Karen Mitchell) #36

OK, done.


Modified to lower case.



I rebooted my Pi after this and ran the script again and I got Permissions denied again. I ran the ls -l and saw the x’s were missing from the script so I knew the permissions were incorrect again. I ran the sudo chmod and then ran the script manually and got “present”!

I also checked the app and “To House” now states “Present” as well.

(Stefan) #37

Sorry, my fault. As Rich mentioned, the file permissions in the folder you used for the script are resetted with every boot. This folder is not intended to be used for other files.

So move over the file to another directory (Max suggested one, mine are in /scripts). So make a directory with:

mkdir /scripts

Copy your sh script to this folder (I won’t describe how to, just google and learn some linux commands :wink: ) and check the permissions one more time. Adapt your item configuration to the new path and test again.

And then disable wifi on your phone and check if the state changes to absent. The script only executes once a minute, so the change is not visible immediately.

(Karen Mitchell) #38

Awesome, thanks a million for everyone’s help!!

(Maurits) #39

Edit: final tutorial can be found here: Tutorial

Just an overview of what the outcome is of the above posts and some other posts on the forum.

To enable Iphone detection in openHAB 1.8.3 follow these steps (on RPI2):

  1. Make a directory to store scripts (if you don’t have this yet)
sudo mkdir /home/pi/scripts
  1. Create a file in this directory
sudo nano

with the following code:

declare -a DEVICES
sudo ip neigh flush all
sudo hping3 -2 -c 10 -p 5353 -i u1 -q >/dev/null 2>&1
sleep 1
DEVICES=`arp -an | awk '{print $4}'`
if [[ $DEVICES[*]} =~ $CHECK ]];then
        echo "Present"
        echo "Away"

Make sure you fill in your own local IP address of your Iphone as well as your MAC address (replace xx.xx.xx… in row 6). Make sure to use all lowercase for your MAC address.
Save and close the file.

  1. Now you need to change the permissions:
sudo chown 777

Now check with:

ls -all

if the script is executable.

  1. Install hping3:
sudo apt-get install hping3
  1. Make a rule to check from time to time presence (every minute in this example):

As an example I made an item that shows the result of my rule:

String Iphone "John Doe @ Home? [%s]" 

This item is updated by this rule, trigger by a cron job (link for details: Cron Trigger tutorial

rule "IphonePresence"
    Time cron "0 0/1 * * * ?" 
    var String IphoneExec = executeCommandLine("/home/pi/scripts/", 10000)
    if(Iphone.state != IphoneExec) {
  1. Edit sudoers file with
sudo visudo

Make sure the last line looks like this (if your user for openHAB is openhab)


(Rich Koshak) #40

Nice and concise tutorial on how to make hping3 work. Thank you for posting.

I might make one suggestion to add a sleep between the hping3 and the arp call in I’ve found on my machine at least that the script runs faster than arp packets fly across the network causing the script to only intermittently correctly report Present. Adding a sleep 1 in my case (much less would probably be adequate) makes it return the correct state every time.

( ) #41

Nice @Maurits28!
Now nothing is stopping you to just copy&paste the tutorial to an article at - That would be amazing :wink:

Be sure to check out the About Tutorials & Examples article.

(Maurits) #42

I just made a tutorial here: Iphone presence.

Some things occurred which I could not explain, maybe you can explain to me:

  1. I inserted a hyperlink, first one works, second one only shows the text.
  2. I inserted link to another forum user, first one works, second one only shows the text
  3. I could not give ‘hard enters / line breaks’ at some point in the post, which makes it more difficult to read (e.g. step 3)

iPhone Presence Detection with hping3 and ARP
(Maurits) #43

Thank you for the suggestion, I added it to the tutorial, and changed it in the post above.

( ) #44

Amazing. Okay it seems the original question was answered and the step-by-step tutorial is now available:

With that I will go ahead and close this thread. If there’s a reason against that, please do not hesitate to write me a personal message! :wink:

( ) #45