Page 1 of 2

HOWTO:Zoneminder recording if your 'iPhone' is out of home

Posted: Sun Mar 15, 2015 6:55 pm
by asker
(Note: this howto needs a lot of cleaning up. It's currently geek-folk friendly, not user friendly - you need to change stuff etc and has dependencies on other changes I made to ZM... I'll clean it all up, I promise)

This weekend, I spent some time thinking about how cool it would be if I could automatically start ZM recording if I could somehow detect if my iPhone was out of the house. That would be a great addition to cron schedules, which I already have, to account for those time when my day does not follow 'schedule'

I first thought I could write a script to 'ping' the iPhone. If you can ping it in your LAN, you are at home. The problem is you can't ping your iPhone if its in screen off state (goes to low power a few seconds after screen off, and does not respond to ICMP). Arping also randomly stops working.

So instead, I looked around to see if there was a way I could use "find my iPhone" (assuming you have it enabled) to get current location, and if that location is not home, then boom - use zmpkg.pl to switch run states :-)

(I did not want to load any special app on my iPhone, as it would contribute to battery drain compared to an iCloud find my iPhone query because its very likely the phone already has an open channel with iCloud and it would be more optimized)

I came across a great package called pyicloud here https://github.com/picklepete/pyicloud
I was hoping there is a Perl equivalent, because I know a bit of Perl, but the Perl library WebService::FindMyiPhone never worked. Well, time to learn a bit of python then.

So I wrote this script to write the word 'in' or 'out' to /usr/share/zoneminder/zm_phone_state.txt --> Now you can check that file to switch modes.


Enjoy!!

This is my first python program, so don't curse it. The only bad part is pyicloud needs the iCloud master password - I'm surprised why application specific password's don't work (and I have 2 factor auth enabled).

I have a Cron schedule to run this script every hour - given that it logs into iCloud, I don't want to hit any API limit of Apple. I think 1 hr is a reasonable time to check (so short in/out trips won't be caught). You can reduce this timeframe in cron.

Code here: https://github.com/pliablepixels/zmhack ... _iphone.py

And here is my cron script that runs every 5 minutes to check the state of iPhone and ZM state. If it finds out ZM thinks I am in, but the phone says I am out, then it forces ZM to switch to "out" run state

https://github.com/pliablepixels/zmhack ... kiPhone.sh

My crontab:

Basically, I run the iPhone location checker once an hour (so there is granularity loss - I just don't want to hit any API limits that iCloud may have. You can reduce this time if you want more frequent pings.

Then, I check for the file it creates every 5 minutes. Yes, I know, why check 5 minutes if it changes only once in an hour. I'll sync them later....

"zm_change_state.sh" is nothing but a wrapper around zmpkg.pl --> it makes sure it does not change state if I am in vacation mode (see my other thread linked above for that) - you can use zmpkg instead

https://github.com/pliablepixels/zmhack ... r/cronfile

Re: HOWTO:Zoneminder recording if your 'iPhone' is out of ho

Posted: Fri Mar 20, 2015 3:42 pm
by Christo
Hello Asker,

Nice work, although much easier to perform arping on your local network.

Just grep your Phone's mac address and find it's state!

A few line in bash will do the trick.

Regard, Christo

Re: HOWTO:Zoneminder recording if your 'iPhone' is out of ho

Posted: Fri Mar 20, 2015 10:24 pm
by asker
Hello Christo, thank you!
Can you tell me exactly how? I've gotten very random results - the iPhone sometimes responds sometimes not (even with count 5).
I am doing arping IP-of-iphone (its static in my LAN). I've tried it several times - at times, there is 0 response irrespective of whether the phone is in low power state or not and other times, I get a response with its mac. If it really is this random, I can't use it as a predictable source. I have a 5S.
Christo wrote:Hello Asker,

Nice work, although much easier to perform arping on your local network.

Just grep your Phone's mac address and find it's state!

A few line in bash will do the trick.

Regard, Christo

Re: HOWTO:Zoneminder recording if your 'iPhone' is out of ho

Posted: Sat Mar 21, 2015 11:32 am
by sime
I ping my blackberry passport and my gf's on a 5 minute cron and it respond flawlesslly.... I can ping it all night without a single drop

But i use ping not arping...

Re: HOWTO:Zoneminder recording if your 'iPhone' is out of ho

Posted: Sat Mar 21, 2015 11:44 am
by asker
Sime, I first used ping - that would predictably fail every time the iPhone went to sleep.
Then I tried arping - that would randomly fail too. Maybe an iPhone thing?
sime wrote:I ping my blackberry passport and my gf's on a 5 minute cron and it respond flawlesslly.... I can ping it all night without a single drop

But i use ping not arping...

Re: HOWTO:Zoneminder recording if your 'iPhone' is out of ho

Posted: Sat Mar 21, 2015 12:25 pm
by sime
Is your device jailbreaked?

If yes, this could do it..

http://modmyi.com/cydia/package.php?id=2268

But i guess thw way you do it is more secure,,, lets say some bad poeple connects to my wifi a set teir ip to the right one... They turn down camera lol...

In your way, they have to steal your apple id/phone

Re: HOWTO:Zoneminder recording if your 'iPhone' is out of ho

Posted: Sat Mar 21, 2015 12:59 pm
by asker
Hi Sime, no my device is not jailbroken. Considering the amount of modifications I keep doing, I am surprised its not :mrgreen:

Yes, I quite like the iCloud method, but the only problem is "pyIcloud" is a web scraper. It's not really using iCloud APIs (Apple doesn't expose it to web developers as far as I know). So who knows when it will be disabled, or when changes to the interface will break pyicloud :-p
sime wrote:Is your device jailbreaked?

If yes, this could do it..

http://modmyi.com/cydia/package.php?id=2268

But i guess thw way you do it is more secure,,, lets say some bad poeple connects to my wifi a set teir ip to the right one... They turn down camera lol...

In your way, they have to steal your apple id/phone

Re: HOWTO:Zoneminder recording if your 'iPhone' is out of ho

Posted: Mon Mar 23, 2015 10:02 am
by Christo
Hello Asker...

About arping... try this:

# MAC ADDRESS MOBILE HOST(S) TO DETECT IT'S IP
MAC="XX:XX:XX:XX:XX:XX"

FNDMAC=`sudo arp-scan --interface=wlan0 --localnet | grep $MAC | awk '{print $2}'`

# IF MAC ADDRESS IS FOUND / MOBILE DEVICE PRESENT:
if [ -n $FNDMAC ]; then
# DO YOUR THING
exit 0
fi

# IF MAC ADDRESS IS NOT FOUND / MOBILE DEVICE ABSENT:
if [ -z $FNDMAC ]; then
# DO YOUR THING
fi


Regards and success, Christo

Re: HOWTO:Zoneminder recording if your 'iPhone' is out of ho

Posted: Mon Mar 23, 2015 10:07 am
by Christo
Hello SIME,

Most smartphones do not responde on a ping request, so youll have to use an other way to detect if it's present / connect to your network.

In case of BB, I know that most models responde on Ping, but arping is not depending on a specific IP in case your network is reset, your phone will connect on an other IP. Arping will always detect your phone if present / connected.

see: https://www.youtube.com/watch?v=oou6qKXMMG0

Good luck and success, Christo

Re: HOWTO:Zoneminder recording if your 'iPhone' is out of ho

Posted: Mon Mar 23, 2015 12:39 pm
by sime
Hi Christo,
my router's DCHP make sure that my two device get always the same IP. It is working flawlessly since 12th of march. But i think i'll change my script to use arping since it's arder to spoof a mac adress than an ip adress

Re: HOWTO:Zoneminder recording if your 'iPhone' is out of ho

Posted: Mon Mar 23, 2015 2:55 pm
by asker
Thank you, Christo.

I tried arp-scan. Unfortunately, its result is as unpredictable as arp-ping. I located the device 4 times in 10 tries. I tried different retry counts etc.
The odd part is, its not just that it fails when the iPhone is in standby. It fails randomly even when the iPhone is alive and active.

It's nice that BB responds to pings. I'd love for the iPhone to be more predictable!
Christo wrote:Hello Asker...

About arping... try this:

# MAC ADDRESS MOBILE HOST(S) TO DETECT IT'S IP
MAC="XX:XX:XX:XX:XX:XX"

FNDMAC=`sudo arp-scan --interface=wlan0 --localnet | grep $MAC | awk '{print $2}'`

# IF MAC ADDRESS IS FOUND / MOBILE DEVICE PRESENT:
if [ -n $FNDMAC ]; then
# DO YOUR THING
exit 0
fi

# IF MAC ADDRESS IS NOT FOUND / MOBILE DEVICE ABSENT:
if [ -z $FNDMAC ]; then
# DO YOUR THING
fi


Regards and success, Christo

Re: HOWTO:Zoneminder recording if your 'iPhone' is out of ho

Posted: Fri Mar 27, 2015 11:48 am
by Christo
Hello Asker,

There are always other options open that can solve your problems..

You could try to connect with Bluetooth?

# SET COUNTER FOR HOST(S) DETECTED
CNT=0

# BLUETOOTH MAC-ADDRESSES TO DETECT (AD MAC-ADDRESS, SEPARATED WITH A SINGLE SP$
for MAC in xx:xx:xx:xx:xx:xx yy:yy:yy:yy:yy:yy

do
if sudo l2ping -c 1 $MAC >/dev/null
then
echo $MAC RESPONDING >/dev/null 2>&1
CNT=$(( $CNT + 1))
else
echo $MAC DID NOT RESPOND >/dev/null 2>&1
fi
done

Ill use a LogiLink BT0015 Bluetooth-stick 4.0 on a automated smart thermostat (Raspberry)

Regards, Christo

Re: HOWTO:Zoneminder recording if your 'iPhone' is out of ho

Posted: Fri Mar 27, 2015 4:00 pm
by asker
Thanks again Christo. BT is not an option for me - I have a large area and my phone will easily go out of range with BT, including when I am in the lawn/back yard/basement.

At this stage, I think the only solution that works reliably for my with an iPhone is location tracking. The only other viable solution was the WiFi arp ping, but it doesn't work reliably with the iPhone.

Re: HOWTO:Zoneminder recording if your 'iPhone' is out of ho

Posted: Tue Mar 31, 2015 2:37 pm
by Christo
Hello asker,

About your problems detecting your Iphone, I remembered a similar case that I solved by using nmap.

try: nmap -sP 192.168.1.1/24

for details, see: http://www.cyberciti.biz/networking/nma ... tutorials/


I hope this helps:)

Regards, Christo

Re: HOWTO:Zoneminder recording if your 'iPhone' is out of ho

Posted: Tue Mar 31, 2015 4:27 pm
by asker
Ahh, the venerable nmap!
I must say this is the most promising probe so far, but the mighty iPhone stealth techniques seem to have overpowered this too.

I tried -sP, -sn, -Pn, and without args too (all modes). Stopped responding after succeeding for a while.
Christo wrote:Hello asker,

About your problems detecting your Iphone, I remembered a similar case that I solved by using nmap.

try: nmap -sP 192.168.1.1/24

for details, see: http://www.cyberciti.biz/networking/nma ... tutorials/


I hope this helps:)

Regards, Christo