简体   繁体   中英

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.

when exec works it returns an array as $output and return variable as $return_var.

if ping's unsuccessful, so when IP can't be pinged, it returns:

array: with 9 elements

return_var: 1

if IP is pinged it returns:

array: with more than 9 elements

return_var: 0

on server it returns:

empty array

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.

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:

<?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 ?

also safe mode is OFF on server and it runs php version 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:

on windows it's:

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

and on unix, it's:

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. some of these ips gets pinged and some not. on localhost it works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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