在centos7和centos8测试通过:
function getsysipmac(&$ipaddr, &$mac)
{
$strinfo = trim(shell_exec("routel |grep host |grep -v 127.0.0.1|tail -1"));
$strarr = explode(' ', $strinfo);
$i = 0;
$inf = '';
foreach($strarr as $str) {
if(strlen($str)) {
if($i == 0) $ipaddr = $str;
if($i == 5) $inf = $str;
$i++;
}
}
if(strlen($inf) > 0) {
$cmd = "ifconfig ".$inf."|grep ether|tail -1";
$strinfo = trim(shell_exec($cmd));
$strarr = explode(' ', $strinfo);
if(count($strarr) > 2) {
$mac = $strarr[1];
}
}
if(strlen($ipaddr) > 0 && strlen($mac) > 0) {
return true;
}
return false;
}