Skip to content

Instantly share code, notes, and snippets.

@oerd
Forked from cschaba/gist:4740380
Last active December 17, 2015 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oerd/5560423 to your computer and use it in GitHub Desktop.
Save oerd/5560423 to your computer and use it in GitHub Desktop.
<?php
// from http://www.highonphp.com/regex-pattern-parsing-ifconfig
$data = <<<EOF
eth0 Link encap:Ethernet HWaddr 00:50:56:33:B6:D2
inet addr:192.168.12.103 Bcast:192.168.12.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1337 errors:0 dropped:0 overruns:0 frame:0
TX packets:985 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1657887 (1.5 MiB) TX bytes:104418 (101.9 KiB)
ra0 Link encap:Ethernet HWaddr 00:50:56:33:B6:D3
inet addr:192.168.12.103 Bcast:192.168.12.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1337 errors:0 dropped:0 overruns:0 frame:0
TX packets:985 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1657887 (1.5 MiB) TX bytes:104418 (101.9 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:291 errors:0 dropped:0 overruns:0 frame:0
TX packets:291 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1243639 (1.1 MiB) TX bytes:1243639 (1.1 MiB)
EOF;
$interfaces = array();
foreach (preg_split("/\n\n/", $data) as $int) {
preg_match("/^([A-z]*\d.?\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['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'] = (int) $regex[9];
$interface['rx']['errors'] = (int) $regex[10];
$interface['rx']['dropped'] = (int) $regex[11];
$interface['rx']['overruns'] = (int) $regex[12];
$interface['rx']['frame'] = (int) $regex[13];
$interface['rx']['bytes'] = (int) $regex[19];
$interface['rx']['hbytes'] = (int) $regex[20];
$interface['tx']['packets'] = (int) $regex[14];
$interface['tx']['errors'] = (int) $regex[15];
$interface['tx']['dropped'] = (int) $regex[16];
$interface['tx']['overruns'] = (int) $regex[17];
$interface['tx']['carrier'] = (int) $regex[18];
$interface['tx']['bytes'] = (int) $regex[21];
$interface['tx']['hbytes'] = (int) $regex[22];
$interfaces[] = $interface;
}
}
print_r($interfaces);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment