Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pinging an IP address using PHP and echoing the result

Tags:

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.

like image 781
Bernard Avatar asked Nov 06 '11 22:11

Bernard


People also ask

How check IP ping in PHP?

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.

What happens if you ping an IP address?

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.

How many echo requests does the ping command send?

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.


2 Answers

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"); 
like image 138
maraspin Avatar answered Nov 04 '22 08:11

maraspin


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

like image 40
Nilamkumar Patel Avatar answered Nov 04 '22 06:11

Nilamkumar Patel