Reputation: 878
I have one issue executing 'ping' on PHP, I received a blank result but if I execute other command like a 'whoami' I received a right result, Could you help me please? any Idea?
<?php
exec('ping google.com', $output);
echo $output;
//Result:
?>
<?php
exec('whoami', $output);
echo $output;
//Result: apache
?>
Thanks
Note: Maybe can be some from apache config? or php config? or linux permission?
Upvotes: 1
Views: 832
Reputation: 7221
You can also try this one
<?php
$output = shell_exec('ping google.com');
echo "<pre>$output</pre>";
?>
Upvotes: 0
Reputation: 3275
I'm guessing it's cos the default behavoir of ping is to never stop. It keeps going until you kill it.
man ping says " -c count Stop after sending count ECHO_REQUEST packets. With deadline option, ping waits for count ECHO_REPLY packets, until the timeout expires."
Try adding options to limit pings run-time and see if you get a result.
Upvotes: 1
Reputation: 7221
Try this code.
<?php
function GetPing($ip=NULL) {
if(empty($ip)) {$ip = $_SERVER['REMOTE_ADDR'];}
if(getenv("OS")=="Windows_NT") {
$exec = exec("ping -n 3 -l 64 ".$ip);
return end(explode(" ", $exec ));
}
else {
$exec = exec("ping -c 3 -s 64 -t 64 ".$ip);
$array = explode("/", end(explode("=", $exec )) );
return ceil($array[1]) . 'ms';
}
}
echo GetPing();
?>
Upvotes: 0