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 tried running it as “sudo /etc/openhab/configurations/scripts/iPhoneDetect.sh”
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 iPhoneDetect.sh README
pi@raspberrypi:/etc/openhab/configurations/scripts $ iphoneDetect.sh
-bash: iphoneDetect.sh: 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.
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 “iphoneDetect.sh”, but the correct name is “iPhoneDetect.sh” (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: ./iPhoneDetect.sh
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.
Your scripts aren’t executable. You have to have an “x” before the “rw” and ideally also before the second and third “r”.
Try
chmod 777 iPhoneDetect.sh
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.
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 iPhoneDetect.sh 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 iPhoneDetect.sh).
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…
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.
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:
pi ALL=(ALL) NOPASSWD: ALL
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.
Checking your script I found one (maybe two) problems:
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…
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.
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.
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 ) 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.
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):
Make a directory to store scripts (if you don’t have this yet)
sudo mkdir /home/pi/scripts
Create a file IphoneD.sh in this directory
sudo nano IphoneD.sh
with the following code:
#!/bin/bash
declare -a DEVICES
sudo ip neigh flush all
sudo hping3 -2 -c 10 -p 5353 -i u1 192.168.xxx.xxx -q >/dev/null 2>&1
sleep 1
DEVICES=`arp -an | awk '{print $4}'`
CHECK="xx:xx:xx:xx:xx:xx"
if [[ $DEVICES[*]} =~ $CHECK ]];then
echo "Present"
else
echo "Away"
fi
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.
Now you need to change the permissions:
sudo chown 777 IphoneD.sh
Now check with:
ls -all
if the script is executable.
Install hping3:
sudo apt-get install hping3
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"
when
Time cron "0 0/1 * * * ?"
then
var String IphoneExec = executeCommandLine("/home/pi/scripts/IphoneD.sh", 10000)
if(Iphone.state != IphoneExec) {
Iphone.postUpdate(IphoneExec)
}
end
Edit sudoers file with
sudo visudo
Make sure the last line looks like this (if your user for openHAB is openhab)
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 IphoneD.sh. 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.