detecing a ping
Moderator: General Moderators
-
pinehead18
- Forum Contributor
- Posts: 329
- Joined: Thu Jul 31, 2003 9:20 pm
detecing a ping
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
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
- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England
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.
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.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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....
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
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;
}
?>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
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.
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.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.
I dunno.. the "essential" bit is:
Not exactly a big wheel to reinvent..

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);
?>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.Jenk wrote:I dunno.. the "essential" bit is:
Not exactly a big wheel to reinvent..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); ?>
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Yeah that's true... You need to give a port number and it's simply trying open a connection on that port.bokehman wrote:That's true but it's not a true ping.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
Here you go, google "php ping" and amasing things will happen.
I think you're a little late to the party.ody wrote:Here you go, google "php ping" and amasing things will happen.