The Apache Haus Forum

Advanced search  

News:

Welcome to Apache Haus Distribution Forum

Pages: [1]   Go Down

Author Topic: PHP Ping  (Read 8620 times)

mario

  • Administrator
  • Member Elite
  • *****
  • Offline Offline
  • Posts: 606
PHP Ping
« on: December 26, 2008, 11:59:41 PM »

Code: [Select]
<?php

class Net_Ping
{
  var 
$icmp_socket;
  var 
$request;
  var 
$request_len;
  var 
$reply;
  var 
$errstr;
  var 
$time;
  var 
$timer_start_time;

  function 
Net_Ping()
  {
   
$this->icmp_socket socket_create(AF_INETSOCK_RAW1);
   
socket_set_block($this->icmp_socket);
  }
 
  function 
ip_checksum($data)
  {
     for(
$i=0;$i<strlen($data);$i += 2)
     {
         if(
$data[$i+1]) $bits unpack('n*',$data[$i].$data[$i+1]);
         else 
$bits unpack('C*',$data[$i]);
         
$sum += $bits[1];
     }
    
     while (
$sum>>16$sum = ($sum 0xffff) + ($sum >> 16);
     
$checksum pack('n1',~$sum);
     return 
$checksum;
  }

  function 
start_time()
  {
   
$this->timer_start_time microtime();
  }
 
  function 
get_time($acc=2)
  {
   
// format start time
   
$start_time explode (" "$this->timer_start_time);
   
$start_time $start_time[1] + $start_time[0];
   
// get and format end time
   
$end_time explode (" "microtime());
   
$end_time $end_time[1] + $end_time[0];
   return 
number_format ($end_time $start_time$acc);
  }

  function 
Build_Packet()
  {
   
$data "abcdefghijklmnopqrstuvwabcdefghi"// the actual test data
   
$type "\x08"// 8 echo message; 0 echo reply message
   
$code "\x00"// always 0 for this program
   
$chksm "\x00\x00"// generate checksum for icmp request
   
$id "\x00\x00"// we will have to work with this later
   
$sqn "\x00\x00"// we will have to work with this later

   // now we need to change the checksum to the real checksum
   
$chksm $this->ip_checksum($type.$code.$chksm.$id.$sqn.$data);

   
// now lets build the actual icmp packet
   
$this->request $type.$code.$chksm.$id.$sqn.$data;
   
$this->request_len strlen($this->request);
  }
 
  function 
Ping($dst_addr,$timeout=5,$percision=3)
  {
   
// lets catch dumb people
   
if ((int)$timeout <= 0$timeout=5;
   if ((int)
$percision <= 0$percision=3;
  
   
// set the timeout
   
socket_set_option($this->icmp_socket,
     
SOL_SOCKET,  // socket level
     
SO_RCVTIMEO// timeout option
     
array(
       
"sec"=>$timeout// Timeout in seconds
       
"usec"=>0  // I assume timeout in microseconds
       
)
     );

   if (
$dst_addr)
   {
     if (@
socket_connect($this->icmp_socket$dst_addrNULL))
     {
    
     } else {
       
$this->errstr "Cannot connect to $dst_addr";
       return 
FALSE;
     }
     
$this->Build_Packet();
     
$this->start_time();
     
socket_write($this->icmp_socket$this->request$this->request_len);
     if (@
socket_recv($this->icmp_socket$this->reply2560))
     {
       
$this->time $this->get_time($percision);
       return 
$this->time;
     } else {
       
$this->errstr "Timed out";
       return 
FALSE;
     }
   } else {
     
$this->errstr "Destination address not specified";
     return 
FALSE;
   }
  }
}

$ping = new Net_Ping;
$ping->ping("127.0.0.1");

if (
$ping->time){
  echo 
"Time: ".$ping->time;
}
else
{
  echo 
$ping->errstr;
}
?>

Logged

amntiw601

  • Newbie
  • *
  • Offline Offline
  • Posts: 1
monclerdownjackets
« Reply #1 on: June 24, 2011, 04:49:36 AM »

Well written, support it
Logged

mikecarmelo

  • Newbie
  • *
  • Offline Offline
  • Posts: 1
Re: PHP Ping
« Reply #2 on: August 22, 2016, 06:03:14 PM »

Code: [Select]
<?php

class Net_Ping
{
  var 
$icmp_socket;
  var 
$request;
  var 
$request_len;
  var 
$reply;
  var 
$errstr;
  var 
$time;
  var 
$timer_start_time;

  function 
Net_Ping()
  {
   
$this->icmp_socket socket_create(AF_INETSOCK_RAW1);
   
socket_set_block($this->icmp_socket);
  }
 
  function 
ip_checksum($data)
  {
     for(
$i=0;$i<strlen($data);$i += 2)
     {
         if(
$data[$i+1]) $bits unpack('n*',$data[$i].$data[$i+1]);
         else 
$bits unpack('C*',$data[$i]);
         
$sum += $bits[1];
     }
    
     while (
$sum>>16$sum = ($sum 0xffff) + ($sum >> 16);
     
$checksum pack('n1',~$sum);
     return 
$checksum;
  }

  function 
start_time()
  {
   
$this->timer_start_time microtime();
  }
 
  function 
get_time($acc=2)
  {
   
// format start time
   
$start_time explode (" "$this->timer_start_time);
   
$start_time $start_time[1] + $start_time[0];
   
// get and format end time
   
$end_time explode (" "microtime());
   
$end_time $end_time[1] + $end_time[0];
   return 
number_format ($end_time $start_time$acc);
  }

  function 
Build_Packet()
  {
   
$data "abcdefghijklmnopqrstuvwabcdefghi"// the actual test data
   
$type "\x08"// 8 echo message; 0 echo reply message
   
$code "\x00"// always 0 for this program
   
$chksm "\x00\x00"// generate checksum for icmp request
   
$id "\x00\x00"// we will have to work with this later
   
$sqn "\x00\x00"// we will have to work with this later

   // now we need to change the checksum to the real checksum
   
$chksm $this->ip_checksum($type.$code.$chksm.$id.$sqn.$data);

   
// now lets build the actual icmp packet
   
$this->request $type.$code.$chksm.$id.$sqn.$data;
   
$this->request_len strlen($this->request);
  }
 
  function 
Ping($dst_addr,$timeout=5,$percision=3)
  {
   
// lets catch dumb people
   
if ((int)$timeout <= 0$timeout=5;
   if ((int)
$percision <= 0$percision=3;
  
   
// set the timeout
   
socket_set_option($this->icmp_socket,
     
SOL_SOCKET,  // socket level
     
SO_RCVTIMEO// timeout option
     
array(
       
"sec"=>$timeout// Timeout in seconds
       
"usec"=>0  // I assume timeout in microseconds
       
)
     );

   if (
$dst_addr)
   {
     if (@
socket_connect($this->icmp_socket$dst_addrNULL))
     {
    
     } else {
       
$this->errstr "Cannot connect to $dst_addr";
       return 
FALSE;
     }
     
$this->Build_Packet();
     
$this->start_time();
     
socket_write($this->icmp_socket$this->request$this->request_len);
     if (@
socket_recv($this->icmp_socket$this->reply2560))
     {
       
$this->time $this->get_time($percision);
       return 
$this->time;
     } else {
       
$this->errstr "Timed out";
       return 
FALSE;
     }
   } else {
     
$this->errstr "Destination address not specified";
     return 
FALSE;
   }
  }
}

$ping = new Net_Ping;
$ping->ping("127.0.0.1");

if (
$ping->time){
  echo 
"Time: ".$ping->time;
}
else
{
  echo 
$ping->errstr;
}
?>


Marvelous! A well written code....have you done any implementation to this code or this is the final one?
Once again great job!
keep up the good work!
thx!
Logged

mario

  • Administrator
  • Member Elite
  • *****
  • Offline Offline
  • Posts: 606
Re: PHP Ping
« Reply #3 on: August 23, 2016, 10:02:16 AM »

That is the last version
Logged

JimmyMallory

  • Newbie
  • *
  • Offline Offline
  • Posts: 4
Re: PHP Ping
« Reply #4 on: October 24, 2020, 10:21:30 PM »

Code: [Select]
<?php

class Net_Ping
{
  var 
$icmp_socket;
  var 
$request;
  var 
$request_len;
  var 
$reply;
  var 
$errstr;
  var 
$time;
  var 
$timer_start_time;

  function 
Net_Ping()
  {
   
$this->icmp_socket socket_create(AF_INETSOCK_RAW1);
   
socket_set_block($this->icmp_socket);
  }
 
  function 
ip_checksum($data)
  {
     for(
$i=0;$i<strlen($data);$i += 2)
     {
         if(
$data[$i+1]) $bits unpack('n*',$data[$i].$data[$i+1]);
         else 
$bits unpack('C*',$data[$i]);
         
$sum += $bits[1];
     }
    
     while (
$sum>>16$sum = ($sum 0xffff) + ($sum >> 16);
     
$checksum pack('n1',~$sum);
     return 
$checksum;
  }

  function 
start_time()
  {
   
$this->timer_start_time microtime();
  }
 
  function 
get_time($acc=2)
  {
   
// format start time
   
$start_time explode (" "$this->timer_start_time);
   
$start_time $start_time[1] + $start_time[0];
   
// get and format end time
   
$end_time explode (" "microtime());
   
$end_time $end_time[1] + $end_time[0];
   return 
number_format ($end_time $start_time$acc);
  }

  function 
Build_Packet()
  {
   
$data "abcdefghijklmnopqrstuvwabcdefghi"// the actual test data
   
$type "\x08"// 8 echo message; 0 echo reply message
   
$code "\x00"// always 0 for this program
   
$chksm "\x00\x00"// generate checksum for icmp request
   
$id "\x00\x00"// we will have to work with this later
   
$sqn "\x00\x00"// we will have to work with this later

   // now we need to change the checksum to the real checksum
   
$chksm $this->ip_checksum($type.$code.$chksm.$id.$sqn.$data);

   
// now lets build the actual icmp packet
   
$this->request $type.$code.$chksm.$id.$sqn.$data;
   
$this->request_len strlen($this->request);
  }
 
  function 
Ping($dst_addr,$timeout=5,$percision=3)
  {
   
// lets catch dumb people
   
if ((int)$timeout <= 0$timeout=5;
   if ((int)
$percision <= 0$percision=3;
  
   
// set the timeout
   
socket_set_option($this->icmp_socket,
     
SOL_SOCKET,  // socket level
     
SO_RCVTIMEO// timeout option
     
array(
       
"sec"=>$timeout// Timeout in seconds
       
"usec"=>0  // I assume timeout in microseconds
       
)
     );

   if (
$dst_addr)
   {
     if (@
socket_connect($this->icmp_socket$dst_addrNULL))
     {
    
     } else {
       
$this->errstr "Cannot connect to $dst_addr";
       return 
FALSE;
     }
     
$this->Build_Packet();
     
$this->start_time();
     
socket_write($this->icmp_socket$this->request$this->request_len);
     if (@
socket_recv($this->icmp_socket$this->reply2560))
     {
       
$this->time $this->get_time($percision);
       return 
$this->time;
     } else {
       
$this->errstr "Timed out";
       return 
FALSE;
     }
   } else {
     
$this->errstr "Destination address not specified";
     return 
FALSE;
   }
  }
}

$ping = new Net_Ping;
$ping->ping("127.0.0.1");

if (
$ping->time){
  echo 
"Time: ".$ping->time;
}
else
{
  echo 
$ping->errstr;
}
?>


<?php

class Net_Ping
{
  var $icmp_socket;
  var $request;
  var $request_len;
  var $reply;
  var $errstr;
  var $time;
  var $timer_start_time;

  function Net_Ping()
  {
   $this->icmp_socket = socket_create(AF_INET, SOCK_RAW, 1);
   socket_set_block($this->icmp_socket);
  }
 
  function ip_checksum($data)
  {
     for($i=0;$i<strlen($data);$i += 2)
     {
         if($data[$i+1]) $bits = unpack('n*',$data[$i].$data[$i+1]);
         else $bits = unpack('C*',$data[$i]);
         $sum += $bits[1];
     }
   
     while ($sum>>16) $sum = ($sum & 0xffff) + ($sum >> 16);
     $checksum = pack('n1',~$sum);
     return $checksum;
  }

  function start_time()
  {
   $this->timer_start_time = microtime();
  }
 
  function get_time($acc=2)
  {
   // format start time
   $start_time = explode (" ", $this->timer_start_time);
   $start_time = $start_time[1] + $start_time[0];
   // get and format end time
   $end_time = explode (" ", microtime());
   $end_time = $end_time[1] + $end_time[0];
   return number_format ($end_time - $start_time, $acc);
  }

  function Build_Packet()
  {
   $data = "abcdefghijklmnopqrstuvwabcdefghi"; // the actual test data
   $type = "\x08"; // 8 echo message; 0 echo reply message
   $code = "\x00"; // always 0 for this program
   $chksm = "\x00\x00"; // generate checksum for icmp request
   $id = "\x00\x00"; // we will have to work with this later
   $sqn = "\x00\x00"; // we will have to work with this later

   // now we need to change the checksum to the real checksum
   $chksm = $this->ip_checksum($type.$code.$chksm.$id.$sqn.$data);

   // now lets build the actual icmp packet
   $this->request = $type.$code.$chksm.$id.$sqn.$data;
   $this->request_len = strlen($this->request);
  }
 
  function Ping($dst_addr,$timeout=5,$percision=3)
  {
   // lets catch dumb people
   if ((int)$timeout <= 0) $timeout=5;
   if ((int)$percision <= 0) $percision=3;
 
   // set the timeout
   socket_set_option($this->icmp_socket,
     SOL_SOCKET,  // socket level
     SO_RCVTIMEO, // timeout option
     array(
       "sec"=>$timeout, // Timeout in seconds
       "usec"=>0  // I assume timeout in microseconds
       )
     );

   if ($dst_addr)
   {
     if (@socket_connect($this->icmp_socket, $dst_addr, NULL))
     {
   
     } else {
       $this->errstr = "Cannot connect to $dst_addr";
       return FALSE;
     }
     $this->Build_Packet();
     $this->start_time();
     socket_write($this->icmp_socket, $this->request, $this->request_len);
     if (@socket_recv($this->icmp_socket, $this->reply, 256, 0))
     {
       $this->time = $this->get_time($percision);
       return $this->time;
     } else {
       $this->errstr = "Timed out";
       return FALSE;
     }
   } else {
     $this->errstr = "Destination address not specified";
     return FALSE;
   }
  }
}

$ping = new Net_Ping;
$ping->ping("127.0.0.1");

if ($ping->time){
  echo "Time: ".$ping->time;
}
else
{
  echo $ping->errstr;
}
?>
Logged
Pages: [1]   Go Up
 

Sitemap 1 2 3 4 5 6 7 8 9 10 11 12 13