Monthly Archives: 10 月 2013

[PHP]PHP获取服务器IP输出为数组

Leave a comment

用PHP执行ifconfig获得Linux服务器IP并输出为数组,下面是代码:

function getServerIp(){ //用ifconfig读取服务器IP并输出为数组
		$ss = exec('/sbin/ifconfig | sed -n \'s/^ *.*addr:\\([0-9.]\\{7,\\}\\) .*$/\\1/p\'',$arr);
		return $arr; 
		}
$ips=getServerIp();
foreach($ips as $k=>$v){//过滤IP
	if(substr($v,0,3)=='127' || substr($v,0,3)=='10.' || substr($v,0,7)=='192.168' || substr($v,0,6)=='172.16'){
		unset($ips[$k]);
	}
}
shuffle($ips);//重新排序
print_r($ips);