home‎ > ‎

php_fonction

Parse la sortie de la commande "iwinfo" et stock le tout dans un array :

<?php
function scan_wifi($wlan_if){
   $in = shell_exec( "iwinfo $wlan_if scan 2> /dev/null" );
   if(empty($in)) return false;

   $out = array();
   $in = explode( "\n", $in );

   foreach($in as $line){
      $line = trim($line);
      if(!empty($line)) {
         /* Create new array with cell number, and save address (same line) */
         if(substr($line, 0, 5) == 'Cell '){
            $cell = (int)substr($line, 5, 2);
            $out[$cell] = array();
            $doppelp_pos = strpos($line, ':');
            $out[$cell]['Address'] = trim(substr($line, $doppelp_pos + 1));
         }
         /* Signal and Quality on same line */
         elseif(substr($line, 0, 7) == 'Signal:'){
            $first_eq_pos = strpos($line, ':');
            $last_eq_pos = strrpos($line, ':');
            $dbm_pos = strpos($line, 'm') - $first_eq_pos;
            $out[$cell]['Signal'] = trim(substr($line, $first_eq_pos + 1, $dbm_pos));
            $out[$cell]['Quality'] = trim(substr($line, $last_eq_pos + 1));
         }
         else{
            $doppelp_pos = strpos($line, ':');
            $field = trim( substr( $line, 0, $doppelp_pos ) );
            $out[$cell][$field] = "";
            $out[$cell][$field] .= trim(str_replace('"', '', substr($line, $doppelp_pos + 1)));
         }
      }
   }
   return $out;
}
?>

Array
(
    [1] => Array
        (
            [Address] => 00:19:70:4C:XX:XX
            [ESSID] => Home
            [Mode] => Master  Channel: 6
            [Signal] => -36 dBm
            [Quality] => 70/70
            [Encryption] => none
        )

    [2] => Array
        (
            [Address] => 0A:19:70:4C:XX:XX
            [ESSID] => orange
            [Mode] => Master  Channel: 6
            [Signal] => -36 dBm
            [Quality] => 70/70
            [Encryption] => none
        )

    [3] => Array
        (
            [Address] => 5C:33:8E:E5:XX:XX
            [ESSID] => Livebox-93xx
            [Mode] => Master  Channel: 6
            [Signal] => -36 dBm
            [Quality] => 70/70
            [Encryption] => mixed WPA/WPA2 PSK (TKIP, CCMP)
        )

    [4] => Array
        (
            [Address] => 56:33:8E:E5:XX:XX
            [ESSID] => orange
            [Mode] => Master  Channel: 6
            [Signal] => -36 dBm
            [Quality] => 70/70
            [Encryption] => none
        )

)






Comments