Forum Moderators: coopster

Message Too Old, No Replies

Ping another computer in PHP

         

qasimali82

10:16 am on Jan 10, 2008 (gmt 0)

10+ Year Member



Hello Guys!

I want to create a application in which i want to know weather another computer is working or not.

I want to ping another computer through php scripting. Can anyone help how this can be achieved?

Thanks in Advance.

Regards,
Qasim Ali

Habtom

10:29 am on Jan 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Using the combination of exec [php.net] and ping command, you can accomplish this.

$ping_ex = exec("ping 72.3.232.139", $ping_result, $pr);

if (count($ping_result) > 1){
echo 'Ping online - response';
} else {
echo 'Ping offline - response';
}

Note that there might be actual delays before the results appear.

Habtom

PHP_Chimp

10:35 am on Jan 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or [pear.php.net...]

qasimali82

10:41 am on Jan 10, 2008 (gmt 0)

10+ Year Member



Thanks Hampton!

I tried the script but it never returned anything. I waiting for very long but still it returned nothing.

Regards,
Qasim Ali

qasimali82

10:42 am on Jan 10, 2008 (gmt 0)

10+ Year Member



Sorry Habtom, i misspelled your name.

Habtom

10:44 am on Jan 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually just tested again here, and it works. Are you getting any response, error, or no response at all?

qasimali82

10:45 am on Jan 10, 2008 (gmt 0)

10+ Year Member



No response, it just stucks in execution. Even i waited for minutes.

qasimali82

10:48 am on Jan 10, 2008 (gmt 0)

10+ Year Member



what are these two variables for $ping_result, $pr

Habtom

10:49 am on Jan 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From the link provided:

string exec ( string $command [, array &$output [, int &$return_var ]] )
exec() executes the given command .

command - The command that will be executed.

output - If the output argument is present, then the specified array will be filled with every line of output from the command.

return_var - If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable.

qasimali82

11:06 am on Jan 10, 2008 (gmt 0)

10+ Year Member



Thanks.

But for some reason it just gone on and on and executes. Not sure why.

PHP_Chimp

11:25 am on Jan 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ping -c4 google.com ... if you want to ping google.com

The -c(number) is the number of pings completed, otherwise ping will keep going for ever...

qasimali82

11:42 am on Jan 10, 2008 (gmt 0)

10+ Year Member



Hi PHP_Chimp

Thanks!

Yup you are right. This one works. But their is a problem. I tired a following script

<?
$ping_ex = exec("ping -c2 192.168.2.45", $ping_result, $pr);

if (count($ping_result) > 1){
echo 'Ping online - response';
} else {
echo 'Ping offline - response';
}
?>

It prints "Ping online - response" in both cases weather the system is online or not. Not sure if this condition has a problem
" if(count($ping_result) > 1) "

Regards,
Qasim

PHP_Chimp

11:53 am on Jan 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To be honest iv never tried ping the way you are doing it. So I would suggest that you look at the contents of the $ping_result array.

Example -


ping -c2 nothere_aklsjdhflsakjflkasjdf_example.com
ping: unknown host nothere_aklsjdhflsakjflkasjdf_example.com

So there is always a return from the ping statement. However if you are pinging twice then you should have something like -

ping -c2 example.com
PING example.com (208.77.188.166) 56(84) bytes of data.
64 bytes from www.example.com (208.77.188.166): icmp_seq=1 ttl=52 time=165 ms
64 bytes from www.example.com (208.77.188.166): icmp_seq=2 ttl=52 time=157 ms
--- example.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 157.295/161.213/165.132/3.939 ms

So you will need a way to look through the returned array and find out if your ping has actually worked.

qasimali82

6:32 am on Jan 11, 2008 (gmt 0)

10+ Year Member



Thanks PHP_Chimp,

This information is Great.

Regards,
Qasim