Jump to content

Recommended Posts

Okay, this is how I would do it

<?php
$URLs = array(
"Freaks"	=>"www.phpfreaks.com", 
"Google"	=>"www.google.com", 
"Dont exist" => "256.0.0.1",
"Me" 		=> "127.0.0.1",
);

foreach($URLs as $Name => $URL){
echo $Name;
$X = Ping($URL,300);
//$X = ext_ping($URL); //untested
if($X == 0){
   echo ' offline';
}else{
   echo ' online';
}
echo "<br />\n";
}


function ext_ping($URL){
$ping_ex = exec("ping -c4 $URL", $ping_result, $pr);

$valid = false;
foreach($ping_result as $R){
   if(preg_match('/(\d+) packets transmitted, \1/i', $R)) $valid = true;
}
return $valid;
}


function Ping($WebServer,$timeout=10,$Port = 80){
   Set_Time_Limit(0);  //Time for script to run .. not sure how it works with 0 but you need it
   Ignore_User_Abort(True); //this will force the script running at the end

   $handle = @fsockopen($WebServer, $Port,$errno,$errstr,$timeout);
   if (!$handle){
      //echo "Failed to open ProxyServer $WebServer errno=$errno,errstr=$errstr<br>";
      return 0;
   }
   else {
      $status = socket_get_status($handle);

      //Time the responce
      $start= microtime(true);

      stream_set_timeout($handle,$timeout);
      //send somthing
      ini_set('display_errors','0');
      $write=fwrite($handle,"echo this\n");
      if(!$write){
         return 0;
      }

      stream_set_blocking($handle,0);
      //Try to read. the server will most likely respond with a "ICMP Destination Unreachable" and end the read. But that is a responce!
      fread($handle,1024);
      fclose($handle);
      ini_set('display_errors','1');

      //Work out if we got a responce and time it
      $laptime=microtime(true)-$start;
      if($laptime>$timeout) return 0;
      return $laptime;
   }
}

?>

 

My results

Freaks online<br />

Google online<br />

Dont exist offline<br />

Me online<br />

Oh i should say, that if you want to check for the actual icmp then sockets would work better

ie

replace the ping function with

function Ping($host, $timeout){
$port = 0;
$datasize = 64;
global $g_icmp_error;
$g_icmp_error = "No Error";
$ident = array(ord('J'), ord('C'));
$seq   = array(rand(0, 255), rand(0, 255));

$packet = '';
$packet .= chr(; // type = 8 : request
$packet .= chr(0); // code = 0

$packet .= chr(0); // checksum init
$packet .= chr(0); // checksum init

$packet .= chr($ident[0]); // identifier
$packet .= chr($ident[1]); // identifier

$packet .= chr($seq[0]); // seq
$packet .= chr($seq[1]); // seq

for ($i = 0; $i < $datasize; $i++)
$packet .= chr(0);

$chk = icmpChecksum($packet);

$packet[2] = $chk[0]; // checksum init
$packet[3] = $chk[1]; // checksum init

$sock = socket_create(AF_INET, SOCK_RAW,  getprotobyname('icmp'));

$time_start = microtime();
if(!@socket_sendto($sock, $packet, strlen($packet), 0, $host, $port)) return -1;

$read   = array($sock);
$write  = NULL;
$except = NULL;

$select = socket_select($read, $write, $except, 0, $timeout * 1000);
if ($select === NULL)
{
	$g_icmp_error = "Select Error";
	socket_close($sock);
	return -1;
}
elseif ($select === 0)
{
	$g_icmp_error = "Timeout";
	socket_close($sock);
	return -1;
}

$recv = '';
$time_stop = microtime();
socket_recvfrom($sock, $recv, 65535, 0, $host, $port);
$recv = unpack('C*', $recv);

if ($recv[10] !== 1) // ICMP proto = 1
{
	$g_icmp_error = "Not ICMP packet";
	socket_close($sock);
	return -1;
}

if ($recv[21] !== 0) // ICMP response = 0
{
	$g_icmp_error = "Not ICMP response";
	socket_close($sock);
	return -1;
}

if ($ident[0] !== $recv[25] || $ident[1] !== $recv[26])
{
	$g_icmp_error = "Bad identification number";
	socket_close($sock);
	return -1;
}

if ($seq[0] !== $recv[27] || $seq[1] !== $recv[28])
{
	$g_icmp_error = "Bad sequence number";
	socket_close($sock);
	return -1;
}

$ms = ($time_stop - $time_start) * 1000;

if ($ms < 0)
{
	$g_icmp_error = "Response too long";
	$ms = -1;
}

socket_close($sock);

return $ms;
}

function icmpChecksum($data)
{
$bit = unpack('n*', $data);
$sum = array_sum($bit);

if (strlen($data) % 2) {
	$temp = unpack('C*', $data[strlen($data) - 1]);
	$sum += $temp[1];
}

$sum = ($sum >> 16) + ($sum & 0xffff);
$sum += ($sum >> 16);

return pack('n*', ~$sum);
}

function getLastIcmpError()
{
global $g_icmp_error;
return $g_icmp_error;
}

 

and the if with

if($X == -1){

 

EDIT: check php.ini

;extension=php_sockets.dll

should be

extension=php_sockets.dll

My results

Freaks online<br />

Google online<br />

Dont exist offline<br />

Me online<br />

i got the same as you when untuched.

 

i see that it pings on port 80.  the sever that this code is on is the only one  on part 80 and the rest of the computers in the house are "clients" (not running a web server)  so when i change some of the urls to computer ip's not only does it take forever to load but everyone of them that except the web server was offline.  even opened port 80 in windows firewall and still offline.  think of it as one server and many clients on my lan.  i am more less looking for what a router does when it lists all assigned computers on the net work and if they are on or not.  Great example for the linux users is arp-scan, more or less want that output.

 

 

i change code and got error

 

Warning: socket_create() [function.socket-create]: Unable to create socket [1]: Operation not permitted in /var/www/birdman/html/ping.html on line 61

Sounds like a permissions problem!

I remember something about ICMP being a root only command, but I am not 100% sure about that!

 

can you run this,

 

<?php
$ping_ex = exec("ping -c4 127.0.0.1", $ping_result, $pr);
echo "<BR/>\nOK: $ping_ex - ".implode("<br>\n",$ping_result);

$ping_ex = exec("ping -c4 256.0.0.1", $ping_result, $pr);
echo "<BR/>\nBAD: $ping_ex - ".implode("<br>\n",$ping_result);

$ping_ex = exec("ping -c4 google.com", $ping_result, $pr);
echo "<BR/>\nOK: $ping_ex - ".implode("<br>\n",$ping_result);
?>

 

and I'll create a simple script to check the what's valid, and what's not, (as it seams the external route will have less problems)

OK: rtt min/avg/max/mdev = 0.022/0.023/0.029/0.006 ms - PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.

64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.029 ms

64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.022 ms

64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.022 ms

64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.022 ms

 

--- 127.0.0.1 ping statistics ---

4 packets transmitted, 4 received, 0% packet loss, time 2997ms

rtt min/avg/max/mdev = 0.022/0.023/0.029/0.006 ms

BAD: - PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.

64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.029 ms

64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.022 ms

64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.022 ms

64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.022 ms

 

--- 127.0.0.1 ping statistics ---

4 packets transmitted, 4 received, 0% packet loss, time 2997ms

rtt min/avg/max/mdev = 0.022/0.023/0.029/0.006 ms

OK: rtt min/avg/max/mdev = 98.950/99.588/101.108/0.940 ms - PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.

64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.029 ms

64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.022 ms

64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.022 ms

64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.022 ms

 

--- 127.0.0.1 ping statistics ---

4 packets transmitted, 4 received, 0% packet loss, time 2997ms

rtt min/avg/max/mdev = 0.022/0.023/0.029/0.006 ms

PING google.com (74.125.53.100) 56(84) bytes of data.

64 bytes from pw-in-f100.1e100.net (74.125.53.100): icmp_seq=1 ttl=48 time=98.9 ms

64 bytes from pw-in-f100.1e100.net (74.125.53.100): icmp_seq=2 ttl=48 time=101 ms

64 bytes from pw-in-f100.1e100.net (74.125.53.100): icmp_seq=3 ttl=48 time=99.2 ms

64 bytes from pw-in-f100.1e100.net (74.125.53.100): icmp_seq=4 ttl=48 time=99.0 ms

 

--- google.com ping statistics ---

4 packets transmitted, 4 received, 0% packet loss, time 3003ms

rtt min/avg/max/mdev = 98.950/99.588/101.108/0.940 ms

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.