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.
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.
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 ?
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?
@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
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"
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.