run script when device enters wifi network

Hi,

I have been playing around with the app Proximity which lets you run applescripts when a bluetooth device enters or exits bluetooth range of your computer.

I have set it up so that when I arrive home my itunes library starts playing but I plan on moving into using it to run home automation items like lights etc.

Proximity is a great idea but I would rather not have bluetooth turned on all the time as its a battery drainer and the iphone needs all the help it can get.

I then wondered if when my iphone auto joins my wifi network I could have script that detects it is on the network and runs the automation scripts. This would basically perform a similar function to proximity but using wifi.

any ideas?

Dave

Model: iMac, Airport Extreme, AppleTV, iPhone, Apple HiFi, Airport Express
Browser: Firefox 3.5
Operating System: Mac OS X (10.5)

I’m trying to do this same thing, using WiFi. Have you gotten anywhere with it yet? I feel like it should be fairly simple, but somehow isn’t lol.

One approach would be to start by seeing if your iPhone offers any Bonjour services.

You can check all your Bonjour services on your local network with a free App like this:
https://itunes.apple.com/us/app/discovery-bonjour-browser/id305441017?mt=8

If you can find one offered by your iPhone, then you can use a “do shell script” from Applescript in an always-on background app to access dns-sd and see if the phone’s service is available. When it becomes available, your phone just joined the network. dns-sd should return information under “Instance Name” to allow you to distinguish between different devices providing the same service so you know which one, if you’ve got multiple people with iPhones and your script needs to know who’s who.

dns-sd:
http://hints.macworld.com/article.php?story=20051026183044858

Good luck,

Tom.

This is an awesome idea. However, my phone doesn’t seem to offer any services…what would cause an iPhone to show up as a Bonjour service? The only thing I’m seeing is my printer lol.

Well crap, maybe not such an awesome idea then. I’d just assumed, with all the services and iCloud and everything running these days, that iOS must be broadcasting something on Bonjour. But I couldn’t Google up anything.

It looks like all of Siri’s proximity reminders are just based on GPS location and can’t be set based on connecting to a wifi network.

Does it have to be by the wifi connection, or is location a good enough proxy for what you’re trying to achieve?

IFTTT (If This Then That) has an iOS app that lets you set up triggers based on proximity, and there are hundreds of possible outputs, I feel like there must be one you can use to trigger your script. Toggle something that syncs over a web service that your computer can monitor.

https://ifttt.com/location

IFTTT has an “if connected to a specific wifi network” trigger, but only for Android.

https://ifttt.com/channels/android_device/triggers/1107434142-connects-to-a-specific-wifi-network

Sorry I don’t have a better answer… there are so many possibilities, I feel like there must be some very simple heuristic we’re just missing.

Of course, if your wireless router supports IP binding to a MAC address, you can just bind a static local IP to your phone, and ping it via “do shell script.” That’ll tell your script when your phone’s on the network.

If you can’t bind your phone to a static local IP, you could probably applescript a port scanner. Could install nmap.

https://nmap.org/book/inst-macosx.html

Although nmap requires sudo to return hostnames, and it’s probably not ideal from a security situation to have an always-on backgound applescript with your admin password sending it off to terminal every x seconds… maybe you can put keychain in the middle there? Or just assume Applescripts are too esoteric to be hacker targets on your home network. If the script’s compiled run-only, I don’t know if it’s possible to pull the password back out of it.

Anyway, NMAP run through a “do shell script” will return the MAC address of everything connected to your local network back to Applescript, so that should do it if you can’t put your phone on a static IP.

I checked, and my iPhone is responding to ping and it’s MAC address is showing up in under NMAP.

Good luck. If you get working code, please post.

  • Tom.

You could also try using snmp and install an trap

I love this idea! I was able to get real time phone status on my home network by using curl to read all the IP addresses from my home-router. It was able to tell me my wife had arrived in the driveway by noticing her IP address suddenly appearing in the router table. Next I’ll add a text or email alert for remote monitoring. Your IP numbers and your router’s html formatting will be different than mine so here’s a rough example of the basic components for anyone that wants it…

Here is a basic example:


#READ LOCAL ROUTER PAGE
set myRouterURL to "http://192.168.X.XXX/cgi-bin/whatever.html"
set deviceINFO to return
repeat with eachline in paragraphs of (do shell script "curl " & quoted form of myRouterURL & "| sed 's/<[^>]*>/ /g'")
	if eachline contains "192.168" then
		set deviceINFO to deviceINFO & eachline & return
	end if
end repeat
return deviceINFO

Here is a more involved example:


#
#  THIS CHECKS OUR ROUTER FOR CHANGES 
#   which indicates on/off useage of TV's & Laptops, OR phone arrived or left 
#

# READ LOCAL ROUTER PAGE
set myRouterURL to "http://192.168.XXX.XXX/whatever.html"
set currntROUTERhtml to (do shell script "curl " & quoted form of myRouterURL & "| sed 's/<[^>]*>/ /g'")
--tell application "Finder" to open location myRouterURL  ---->peek at it in yourbrowser

#PRE-POPULATE A DEVICE LIST TO USE AS REFERENCE  
set ourPHONES to {{"192.168.X.XX", "MyMobilePhoneName"}, {"192.168.X.XX", "HerMobilePhoneName"}}
set ourLAPTOPS to {{"192.168.X.XX", "MyLaptopName"}}
set ourDEVICES to ourPHONES & ourLAPTOPS

#CHECK LINE-BY_LINE FOR FAMILIAR DEVICE INFO
set deviceINFO to {}
repeat with eachline in paragraphs of currntROUTERhtml
	if eachline contains "192.168." then
		repeat with dvcs in ourDEVICES
			set {deviceIP, deviceNAME} to dvcs
			if eachline contains deviceIP then
				set onOff to word 1 of (text ((length of deviceNAME) + (offset of deviceNAME in eachline)) thru -1 of eachline)
				set end of deviceINFO to {deviceIP, deviceNAME, onOff}
			end if
		end repeat
	end if
end repeat

#SEPARATE INTO  ON/OFF  LISTS
set {onTEST, offTEST} to {{}, {}}
repeat with dvcs in deviceINFO
	set {dvIP, dvNAM, dvSTAT} to dvcs
	if dvSTAT is "on" then set end of onTEST to dvcs as list --your on/off flag may differ from mine.
	if dvSTAT is "off" then set end of offTEST to dvcs as list--your on/off flag may differ from mine.
end repeat
return {onTEST, offTEST} --->COMPARE THESE LISTS TO RESULTS OF PREVIOUS VALUES, stored as a property or a database file.

Model: Mac Pro, Yosemite
AppleScript: 2.7
Browser: Safari 601.2.7
Operating System: macOS 10.14