ping ip address and run applescrip​t on response

I would like to set my mac to ping the static ip address of my phone every two minutes. Then when my mac recieves a response from my phone I want my mac to run a script, is this possible, and how would I do this?

Thanks in advance for your help,

Is this an script that stays open? Are you able to ping your phone? Not every provider supports icmp packets and therefore not able to ping phones, like mine. The easiest way would be

on run argv
--do your ping command here
with timeout of 300 seconds
delay 120 second
end timeout
end repeat
end run

Keep in mind that after an day or more your script will take your mac’s performance down. The best solution would be that the script will be executed every 20 seconds by launchd.

Yes, this script will always be open. I only need to ping my phone when it connects to my home wi-fi network. When my mac recieves a response from my phone, I want my mac to run an applescript.