The recent need to improve a log mechanism to monitor the script execution status of machines from different servers, especially for Windows and Linux and the Web and command line mode to write a function to be compatible.
The following function is written to achieve the above requirements:
Copy Code code as follows:
function Getserveraddr () {
Running the web App
if (Isset ($_server["SERVER_ADDR"])) {
return $_server["Server_addr"];
else {//Running CLI
if (Stristr (Php_os, ' WIN ')) {
A hacky way to perform against a Windows server
EXEC ("Ipconfig/all", $catch);
foreach ($catch as $line) {
$new _catch[] = iconv ("GBK", "Utf-8", $line). "\ n";
}
foreach ($new _catch as $new _line) {
if (Preg_match ('/ipv4 address/', $new _line)) {//Chinese system
List ($t, $ip) = Explode (': ', $new _line);
$ip = Trim ($IP);
Preg_match ('/(?:(? : 25[0-5]|2[0-4]\d| ((1\d{2}) | ([1-9]?\d))] \.) {3} (?: 25[0-5]|2[0-4]\d| ((1\d{2}) | ([1-9]?\d))] /', $IP, $match);
return $match [1];
}
}
} else {
$ifconfig = Shell_exec ('/sbin/ifconfig eth0′);
Preg_match ('/addr: [\d\.] +)/', $ifconfig, $match);
return $match [1];
}
}
}
$ip = Getserveraddr ();
Print $ip;