简体   繁体   English

php exec无法在服务器上执行ping操作

[英]php exec doesn't work for ping on server

I'm trying to ping an IP with exec function to know if IP gets pinged or not, it works totally fine on localhost and returns the output array, but when I run it on the server it returns an empty output array. 我正在尝试使用exec函数对IP进行ping操作,以了解IP是否被ping通,它在localhost上可以正常工作并返回输出数组,但是当我在服务器上运行它时,它将返回一个空的输出数组。

when exec works it returns an array as $output and return variable as $return_var. 当exec工作时,它返回一个数组为$ output,返回变量为$ return_var。

if ping's unsuccessful, so when IP can't be pinged, it returns: 如果ping不成功,那么当IP无法ping时,它将返回:

array: with 9 elements 数组:具有9个元素

return_var: 1 return_var:1

if IP is pinged it returns: 如果IP ping通,它将返回:

array: with more than 9 elements 数组:具有9个以上的元素

return_var: 0 return_var:0

on server it returns: 在服务器上,它返回:

empty array 空数组

return_var: 2 return_var:2

as I searched and found out when return_var is 2, it means that exec doesn't work and there's an error. 当我搜索并发现return_var为2时,这意味着exec无法正常工作并且出现错误。

this is my code: 这是我的代码:

 <?php
    exec('ping -n 4 '.$ip, $output, $return_var);

    echo "<pre>";
    var_dump($output);
    ?>

exec() isn't disabled on server, I tried this: exec()未在服务器上禁用,我尝试了以下操作:

<?php
$disabled = explode(',', ini_get('disable_functions'));

echo "<pre>";
var_dump($disabled);
?>

and this is a disabled functions list I got: 这是我得到的禁用功能列表:

 array(8) {
      [0]=>
      string(7) "symlink"
      [1]=>
      string(10) "proc_close"
      [2]=>
      string(9) "proc_open"
      [3]=>
      string(5) "popen"
      [4]=>
      string(6) "system"
      [5]=>
      string(2) "dl"
      [6]=>
      string(8) "passthru"
      [7]=>
      string(14) "escapeshellcmd"
    }

is there any chance any of these blocked functions causing problems of exec() functionality ? 这些被阻塞的函数是否有可能导致exec()功能出现问题?

also safe mode is OFF on server and it runs php version 5.3.29 服务器上的安全模式也关闭,并且运行php版本5.3.29

I solved it, I thought all servers where running windows, but the one my site is up to is running unix, and exec needed a little change: 我解决了这个问题,我以为所有服务器都在运行Windows,但是我的站点可以运行的服务器正在运行unix,而exec需要做一些改动:

on windows it's: 在Windows上是:

exec('ping -n 4 '.$ip, $output, $return_var);

and on unix, it's: 在Unix上,它是:

exec('ping -c 4 '.$ip, $output, $return_var);

but new problem is that, it doesn't ping the ips that actually gets pinged but can't be found on ns_lookup in cmd. 但是新的问题是,它无法ping到实际上会被ping通但无法在cmd的ns_lookup中找到的ip。 some of these ips gets pinged and some not. 这些IP中的一些会被ping通,而有些则不会。 on localhost it works fine. 在本地主机上工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM