Checking if a device is on local network

Is it possible for an applescript to check if a device (such as an iPhone) is on the local network? I want to be able to run a script when that occurs. Then, maybe have another script run when it disconnects.

If so, can anyone post code???