I use a PHP "shell" script similar to this, launched from terminal to test if one or more devices are down/disconnected. I use it to monitor if a Wifi bridge has gone down (if it can't see MAC address of devices on the other side of the bridge, then assume it's gone down):
#!/usr/bin/php
<?php
$MULTICAST_ADDR='192.168.99.255';
$DEVICES_TO_TEST_FOR_BRIDGE=array( // list of MACs
'XX:XX:63:f2:XX:XX',
'XX:XX:d0:ad:XX:XX',
'XX:XX:b9:eb:XX:XX'
);
$SLEEP=10000000; // one sec = 1000000, sleep before reconnect
while(TRUE) {
usleep($SLEEP);
flush();
`ping -b -c 3 -t 3 $MULTICAST_ADDR 2> /dev/null`;
flush();
$res=`arp -an`;
$bridgeIsOn=FALSE;
echo("DEVICES:\n");
echo($res);
echo("\n");
foreach($DEVICES_TO_TEST_FOR_BRIDGE as $deviceToTestForBridge) {
if (strpos($res, $deviceToTestForBridge) !== false) {
$bridgeIsOn=TRUE;
$lastBridgeOn=time();
}
}
if(!$bridgeIsOn && (time()-$lastBridgeOn>5*60) ) { // If bridge is down for 5 minutes
echo(" BRIDGE HAS BEEN OFF FOR LONG (".(time()-$lastBridgeOn)."s) - REQUESTING A RESTART\n");
// Do something here
}
}
What it does is like, in a terminal, typing
ping -b -c 3 -t 3 192.168.99.255 2> /dev/null
My devices are in the range 192.168.99.xxx, .255 means "them all". So ping them all and ignore the replies.
And then reading the arp tables with command
arp -an
The rest of the code is to compare the list with the devices I want.
At the end of the loop, $bridgeIsOn is either TRUE or FALSE, and $lastBridgeOn has the time it last was seen on, so I can make choices and execute stuff. In your case you must revert the logic, $bridgeIsOn means your phone is on the network: if($bridgeIsOn) { /* do something */}
I save the script as "monitorbridge", make it executable and launch it in a terminal
./monitorbridge
The script stays on until I close the terminal or type ^C. You can of course have it start at login or boot.
If you have a computer always on it can detect the presence of another device scanning the network and testing for a MAC address. I have a PHP shell script that does exactly this. I use it in revers to scan when a device is not present to detect if the connection is down. Do you have a Mac or Linux computer on and available? – FrancescoMM – 2017-12-06T11:09:22.353