Script hangs periodically

Hi,

I have some code that is checking the presence of some devices on my network - which I have lifted and modified from another thread.

#!/etc/php5
    <?php
      function readActiveWiFiMacs ($ip,$wifi) {
//        $check_openWRT="ssh root@".$ip." 'iw dev ".$wifi." station dump' | awk '/^Station/ {print substr($2,0);}'";
        $check_openWRT="sshpass -p password ssh -o StrictHostKeyChecking=no root@".$ip." arp > /tmp/a8ree ; sleep 2 | awk '{print substr($4,0);}' /tmp/a8ree";
        exec($check_openWRT,$addresses);
        return ($addresses);
      }
      function sendCommand($item, $data) {
        $url = "http://192.168.1.8:8080/rest/items/" . $item;
        $options = array(
            'http' => array(
            'header'  => "Content-type: text/plain\r\n",
            'method'  => 'POST',
            'content' => $data  //http_build_query($data),
            ),
        );
        $context  = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        return $result;
    }
      // An array of mac addresses to be checked, adjust to fit your setup
      $macAddresses = array(
       '00:ed:ed:e9:5a:c4' => 'A_Phone',
       '80:ea:e6:e2:e7:6c' => 'R_Phone',
       '5c:8e:9e:02:8f:65' => 'D_Phone',
       );
      $users =array ();  // An array to hold  WiFi active status indexed by user name
      foreach ($macAddresses as $mac => $user)
        $users[$user]="0"; // As the openWrt will return only active clients, all known should be initially marked as OFF
      $macs = readActiveWiFiMacs ("192.168.1.1","wlan0");   			// Let's check the first access point
      $macs = array_merge($macs,readActiveWiFiMacs ("192.168.1.1","wlan0")); // Let's see who is online at second AP
      $macs = array_merge($macs,readActiveWiFiMacs ("192.168.1.1","wlan1")); // And on 5MHz network at second AP
      foreach ($macs as $mac)  // Mark users related to returned active mac addresses as active
        $users[$macAddresses[$mac]]="1";
      foreach ($users as $user => $status)
         sendCommand('WiFi_'.$user,$status);
?>

The script works fine without issue for a period of time - sometimes days. Then I notice that this and all other scripts have stopped processing.

The only way in which I can get this to continue working without killing OpenHAB is to list the processes, identify the SSHPASS process and kill it.

Does anyone know why this would hang or a method of auto-killing it?

Thanks

I’m not sure why it fails but I think it would be easier to do this with the network health binding.
Set a static DHCP lease in open_wrt for the phones/devices you would like to check for presence and then add items like this (example):
Switch A_Phone “My phone A” nh=“192.168.1.100”

This way it’s less “hackish” and you don’t have to worry about script timeouts etc.

Will the devices respond to ICMP?

That of course is an assumption. If they don’t reply to ICMP/PING then my solution won’t work.
(I’m currently using this at home and it’s been working fine)

I will have a check and see… do you manage to detect Android and IOS?

I know for sure Samsung S4 and S6 works (Android)

There’s also another option to consider if you want to avoid the ssh approach, use snmp.
Enable snmpd in open_wrt and retrieve the arp table with the “ipNetToPhysicalPhysAddress” OID.

Then just use http://php.net/manual/en/snmp.walk.php to walk through the table.