Using PHP to gather all active network interfaces

Posted by | Posted in Code, Command-Line, Functions, OS X, PHP | Posted on 19-07-2009

I use this script to gather all Ethernet based network activity to determine which network port is active and then determine what the hardware address and IP address are.

# input NONE
# output String ("en0, en1")
 
function get_all_active_network_interfaces() {
  $inclusion = array(en0,en1);
  @exec('/sbin/ifconfig -lu', $retval);
  $interfaces = explode(" ",$retval[0]);
  $ret=NULL;
  foreach($interfaces as $interface){
     if (in_array($interface,$inclusion)){
       $ret.=$interface ." \n \r";
     } // end if
  }// end foreach
return $ret;
} // end get_all_active_network_interfaces
echo get_all_active_network_interfaces();

This script is similar to the above script, but doesn’t include the constraints of the inclusion list.

# input NONE
# output String ("en0, en1, etc") 
function get_all_active_network_interfaces() {
  @exec('/sbin/ifconfig -lu', $retval);
  $interfaces = explode(" ",$retval[0]);
  $ret=NULL;
    foreach($interfaces as $interface){
      $ret .= $interface ." \n \r";
    }// end foreach
return $ret;
} // end get_all_active_network_interfaces
 
echo get_all_active_network_interfaces();

Comments are closed.

Join the mailing list

Check your email and confirm the subscription