detecing a ping

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

detecing a ping

Post by pinehead18 »

i'm looking on how to get started writing a script that bsaically says if there no ping then this.

Now my question is how do i detect if there is actually a ping respose or not. I assuming using ping().

but isn't isset(ping) still going to give information wheather their is a response or not?

Thanks
Anthony
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Well, there isn't a function called ping().

http://uk.php.net/manual-lookup.php?pattern=ping

What are you intending to ping and what data do you expect to receive back? There is probably another function which will do what you want.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

fsockopen() .... if it returns true you're all good... else it failed ;)

There's other functions too which do name lookups that would have the same effect ;)
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

ping should return a response letting you know it was successful. Like 1 or 0 or something like that. Go check out the manual for system functions like exec(), system(), etc.... I've done this before on various occasions. One was an PHP CLI network discovery / port scanner.

Cheers,
BDKR
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

d11wtq wrote:fsockopen() .... if it returns true you're all good... else it failed ;)

There's other functions too which do name lookups that would have the same effect ;)
That's true but it's not a true ping.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Well is ping time in ms important or is just knowing if it'll respond more important?
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

I normally don't post examples but instead normally just try to point someone in the right direction. That way you're getting help and flexing the head muscle at the same time. In this case, i think perhaps an example is a better idea as we're talking about system level programming here more then the web dev stuff most of normally do.

Anyway....

Code: Select all

<?php
# Some Vars
$address='gateway';
$ret_val=0;

# Check that the network is up
// @system('ping -t 2 '.$address.' > /dev/null ', $ret_val);		      /* <-- Try this if you're on Winthroes */
@system('ping -c 2 '.$address.' > /dev/null ', $ret_val);		     /* Unix variant */

# Display result
if($ret_val===1) 
	{ echo "The network is up but may have problems.\n"; }  
elseif($ret_val===0)
	{ echo "The network is up. Woo hoo!\n"; }
elseif($ret_val===2)
	{ 
	$str="The network appears to be down. Please read the man page for ping to understand \n";
	$str.="what errors you may be experiencing.\n The exit code is 2!\n"; 
	echo $str;
	}
?>
Assuming you know Linux and/or that Windows ping will respond in the same way, ping will output a code based on the result of your command. 0 means it was all good. 1 means it's running but other conditions may exist that could be slowing the network down. 2 means that you're bummin' and it's time to fix something.

Now the system() function has an output parameter. We use that parameter to catch the output code from ping. We can tell by reading that output code what time it is (is the network up or down).

Make a point of it to read the ping man page or at least type "ping /h" if you're on Windows.

Hope that helps.

Cheers,
BDKR
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

http://www.planet-source-code.com/vb/sc ... 6&lngWId=8

I was just going to copy and paste to code from there, but credit where it's due etc. etc.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Jenk wrote:http://www.planet-source-code.com/vb/sc ... 6&lngWId=8

I was just going to copy and paste to code from there, but credit where it's due etc. etc.
Wow! That guy wrote a great article but I think he' s kinda reinventing the wheel there.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

I dunno.. the "essential" bit is:

Code: Select all

<?php
$socket = socket_create(AF_INET, SOCK_RAW, 1);
socket_connect($socket, "www.google.com", null);
$startTime = microtime(true);
socket_send($socket, $package, strLen($package), 0);
if (socket_read($socket, 255)) {
echo round(microtime(true) - $startTime, 4);
}
socket_close($socket);
?>
Not exactly a big wheel to reinvent..

:)
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Wow, that was a great article.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Jenk wrote:I dunno.. the "essential" bit is:

Code: Select all

<?php
$socket = socket_create(AF_INET, SOCK_RAW, 1);
socket_connect($socket, "www.google.com", null);
$startTime = microtime(true);
socket_send($socket, $package, strLen($package), 0);
if (socket_read($socket, 255)) {
echo round(microtime(true) - $startTime, 4);
}
socket_close($socket);
?>
Not exactly a big wheel to reinvent..

:)
True that. I just don't see a need. Most admins worth their salt won't setup a system without ping installed unless we're talking about a shared hosting type of thing. If that's the case, the sockets module for PHP prolly wouldn't be enabled either.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

bokehman wrote:
d11wtq wrote:fsockopen() .... if it returns true you're all good... else it failed ;)

There's other functions too which do name lookups that would have the same effect ;)
That's true but it's not a true ping.
Yeah that's true... You need to give a port number and it's simply trying open a connection on that port.
ody
Forum Contributor
Posts: 147
Joined: Sat Mar 27, 2004 4:42 am
Location: ManchesterUK

Post by ody »

Here you go, google "php ping" and amasing things will happen.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

ody wrote:Here you go, google "php ping" and amasing things will happen.
I think you're a little late to the party. :wink:
Post Reply