0

PHP knows the own IP in $_SERVER["SERVER_ADDR"] but how can I read the rest of the network-configuration netmask, broadcast, etc?

One solution would be to get the information with exec on my Linux server, but that would be quite a complicated regular expression to cut that information apart

rubo77
  • 19,527
  • 31
  • 134
  • 226

2 Answers2

2

Ubuntu:

exec("/sbin/ifconfig", $data);
$data = implode($data, "\n");

foreach (preg_split("/\n\n/", $data) as $int) {
  preg_match("/^([A-z]*\d)\s+Link\s+encap:([A-z]*)\s+HWaddr\s+([A-z0-9:]*).*" .
    "inet addr:([0-9.]+).*Bcast:([0-9.]+).*Mask:([0-9.]+).*" .
     "MTU:([0-9.]+).*Metric:([0-9.]+).*" .
     "RX packets:([0-9.]+).*errors:([0-9.]+).*dropped:([0-9.]+).*overruns:([0-9.]+).*frame:([0-9.]+).*" .
     "TX packets:([0-9.]+).*errors:([0-9.]+).*dropped:([0-9.]+).*overruns:([0-9.]+).*carrier:([0-9.]+).*" .
     "RX bytes:([0-9.]+).*\((.*)\).*TX bytes:([0-9.]+).*\((.*)\)" .
     "/ims", $int, $regex);

    if (!empty($regex)) {
      $interface = array();

      $interface = array(); 
      $interface['name'] = $regex[1]; 
      $interface['type'] = $regex[2]; 
      $interface['mac'] = $regex[3]; 
      $interface['ip'] = $regex[4]; 
      $interface['broadcast'] = $regex[5]; 
      $interface['netmask'] = $regex[6]; 
      $interface['mtu'] = $regex[7]; 
      $interface['metric'] = $regex[8]; 

      $interface['rx']['packets'] = $regex\[9]; 
      $interface['rx']['errors'] = $regex\[10]; 
      $interface['rx']['dropped'] = $regex\[11]; 
      $interface['rx']['overruns'] = $regex\[12]; 
      $interface['rx']['frame'] = $regex\[13]; 
      $interface['rx']['bytes'] = $regex\[19]; 
      $interface['rx']['hbytes'] = $regex\[20]; 

      $interface['tx']['packets'] = $regex\[14]; 
      $interface['tx']['errors'] = $regex\[15]; 
      $interface['tx']['dropped'] = $regex\[16]; 
      $interface['tx']['overruns'] = $regex[17]; 
      $interface['tx']['carrier'] = $regex[18]; 
      $interface['tx']['bytes'] = $regex[21]; 
      $interface['tx']['hbytes'] = $regex[22];

      $interfaces[] = $interface;
    }
}

Source: Regular Expression Pattern Parsing ifconfig

http://www.highonphp.com/regex-pattern-parsing-ifconfig

sn-user
  • 36
  • 2
0

You can use the exec() function to run ipconfig or ifconfig depending on your server and if you have permission or not. You will then need to scrape the results from the returned array of strings.

exec('ifconfig',$output); //use ipconfig for windows
print_r($output);

If you don't want to us ifconfig you could create a custom script that you can still use exec() with but that has cleaner output for easier manipulation.

Pitchinnate
  • 7,517
  • 1
  • 20
  • 37
  • You might want to point out the dangers of using `exec()` and the fact that if the server is a shared host they most likely won't have access to it – RMcLeod Jul 02 '13 at 14:07
  • @RMcLeod Yeah I completely agree, that is why I said "If you have permission". – Pitchinnate Jul 02 '13 at 14:09
  • http://stackoverflow.com/questions/6197845/how-should-i-or-should-i-use-php-functions-considered-dangerous – Pitchinnate Jul 02 '13 at 14:19