I have the following function that I doesn't work so far. I would like to ping an IP address and then to echo whether the IP is alive or not.
function pingAddress($ip){ $pingresult = shell_exec("start /b ping $ip -n 1"); $dead = "Request timed out."; $deadoralive = strpos($dead, $pingresult); if ($deadoralive == false){ echo "The IP address, $ip, is dead"; } else { echo "The IP address, $ip, is alive"; } }
When I call this function using the example:
pingAddress("127.0.0.1")
The echo result is always 'dead' - no matter what.
Could someone please help me where I'm going wrong? And/OR is there a better method of doing this with the same result?
Many thanks.
Update: Have amended code to include the double quotes but still getting the same (incorrect) results.
Making a Ping request To make a ping request, simply write the word "ping" followed by a space then the IP address or domain name whose connection you want to test in command prompt or terminal. Then hit enter.
A ping network test transmits data packets to a specific IP address and either confirms or denies there is connectivity between IP-networked devices. In the case of confirmation, you will discover the “latency” (i.e., the length of the response time) by performing a ping test.
When a ping command is issued, an echo request packet is sent to the address specified. When the remote host receives the echo request, it responds with an echo reply packet. By default, the ping command sends several echo requests, typically four or five.
NOTE: Solution below does not work on Windows. On linux exec a "which ping" command from the console, and set command path (of the suggested exec call) accordingly
I think you want to check the exit status of the command, whereas shell_exec gives you full output (might be dangerous shall command output change from command version to version. for some reason). Moreover your variable $ip is not interpreted within single quotes. You'd have to use double ones "". That might be the only thing you need to fix in order to make it work.
But I think following code can be more "portable". IMHO it is in fact better to catch the exit status, rather than trying to parse result string. IMHO it's also better to specify full path to ping command.
<?php function pingAddress($ip) { $pingresult = exec("/bin/ping -n 3 $ip", $outcome, $status); if (0 == $status) { $status = "alive"; } else { $status = "dead"; } echo "The IP address, $ip, is ".$status; } pingAddress("127.0.0.1");
This also did not work for me in Wordpress. I also tried -t and -n and other ways, but did not work. I used,
function pingAddress($ip) { $pingresult = exec("/bin/ping -c2 -w2 $ip", $outcome, $status); if ($status==0) { $status = "alive"; } else { $status = "dead"; } $message .= '<div id="dialog-block-left">'; $message .= '<div id="ip-status">The IP address, '.$ip.', is '.$status.'</div><div style="clear:both"></div>'; return $message; } // Some IP Address pingAddress("192.168.1.1");
This worked perfectly for me, finally. I referred this from http://www.phpscriptsdaily.com/php/php-ping/ Hope this will help
Well I want to modify this as it is working fine on my localhost but not on my live server For live server, I got another thing which now works for both local as well as live.
$fp = fSockOpen($ip,80,$errno,$errstr,1); if($fp) { $status=0; fclose($fp); } else { $status=1; }
Then I show the Server is up for 0 and down for 1.
This works perfectly for me. I got this from Ping site and return result in PHP Thanks @karim79
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With