Home  >  Article  >  Backend Development  >  Example code for php to obtain linux command results

Example code for php to obtain linux command results

黄舟
黄舟Original
2017-03-14 09:32:041425browse

下面小编就为大家带来一篇php获取linux命令结果的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

如果使用php命令行里想获取etho网卡的IP怎么处理呢 ?


public function get_server_ip() {
 	if (PHP_SAPI === 'cli'){
	 	$buffer = "";
		 $handle = popen("ifconfig eth0|grep 'inet addr'|awk -F'[ :]' '{print $13}'", 'r');
			while(!feof($handle)) {
			 $buffer.=fgets($handle);
			}
			pclose($handle);
		 $server_ip = rtrim($buffer);
 	}else{
	 	if (isset($_SERVER)) { 
		  if($_SERVER['SERVER_ADDR']) {
		   $server_ip = $_SERVER['SERVER_ADDR']; 
		  } else { 
		   $server_ip = $_SERVER['LOCAL_ADDR']; 
		  } 
		 } else { 
		  $server_ip = getenv('SERVER_ADDR');
		 }
 	}
	 return $server_ip; 
	}

The above is the detailed content of Example code for php to obtain linux command results. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn