Page 1 of 1

detecing a ping

Posted: Fri Dec 16, 2005 5:40 pm
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

Posted: Fri Dec 16, 2005 6:22 pm
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.

Posted: Fri Dec 16, 2005 7:31 pm
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 ;)

Posted: Fri Dec 16, 2005 8:27 pm
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

Posted: Sat Dec 17, 2005 4:00 pm
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.

Posted: Sat Dec 17, 2005 4:23 pm
by m3mn0n
Well is ping time in ms important or is just knowing if it'll respond more important?

Posted: Sat Dec 17, 2005 8:03 pm
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

Posted: Sun Dec 18, 2005 1:03 am
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.

Posted: Sun Dec 18, 2005 1:12 am
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.

Posted: Sun Dec 18, 2005 1:14 am
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..

:)

Posted: Sun Dec 18, 2005 3:34 am
by m3mn0n
Wow, that was a great article.

Posted: Sun Dec 18, 2005 2:55 pm
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.

Posted: Sun Dec 18, 2005 3:04 pm
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.

Posted: Sun Dec 18, 2005 3:26 pm
by ody
Here you go, google "php ping" and amasing things will happen.

Posted: Sun Dec 18, 2005 8:04 pm
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: