Home >Backend Development >PHP Tutorial >How to get linux command results in php

How to get linux command results in php

墨辰丷
墨辰丷Original
2018-05-24 17:33:412120browse

本篇文章主要介绍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; 
	}

以上就是本文的全部内容,希望对大家的学习有所帮助。


相关推荐:

PHP调用Linux命令权限不足问题解决方法,phplinux_PHP教程

PHP实现linux命令tail -f,phplinuxtail-f_PHP教程

PHP执行linux命令常用函数汇总,phplinux命令汇总_PHP教程

The above is the detailed content of How to get linux command results in php. 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