Current location: Hot Scripts Forums » Programming Languages » PHP » PHP Ping and Tracert


PHP Ping and Tracert

Reply
  #1 (permalink)  
Old 02-11-05, 07:47 PM
webzila webzila is offline
New Member
 
Join Date: Mar 2004
Location: TX
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Ping and Tracert

Does anyone know if there are any PHP scripts that will allow you to run Ping and Traceroute without utilizing the PHP system() function or use the network utilities on the server..

I understand that this wouldnt be a 'real' ping or traceroute but anything will do for me.

I used to have a script that utilized the system() command but the webhost disabled it.

Then I had a script which did something like this:

PHP Code:

function p($target){

message("<p><b>Ping Results:</b><blockquote>"); if (! $msg .= trim(nl2br(`ping -c5 '$target'`))) #bugfix
$msg .= "Ping failed. Host may not be active."$msg .= "</blockquote></p>"message($msg); }

function 
tr($target){
message("<p><b>Traceroute Results:</b><blockquote>"); if (! $msg .= trim(nl2br(`/usr/sbin/traceroute '$target'`))) #bugfix
$msg .= "Traceroute failed. Host may not be active."$msg .= "</blockquote></p>"message($msg); } 
That script doesnt seem to use the system() function but has also stopped working recently so I was looking for something that would not depend on the Ping and Traceroute utilities on the server but rather 'mimic' those functions and provide comparable output.

Thanks
Reply With Quote
  #2 (permalink)  
Old 02-13-05, 01:01 PM
bizzar528's Avatar
bizzar528 bizzar528 is offline
Community Liaison
 
Join Date: Sep 2004
Location: Pennsylvania, US
Posts: 1,550
Thanks: 2
Thanked 16 Times in 15 Posts
There's probably something for tracert, but I did remember seeing a php ping script that uses socket connections.

Link -- http://us3.php.net/sockets

Look for a post by noSanity. Here's his code from the site:
PHP Code:

<?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("www.google.ca");

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

?>
__________________
Yep, it's a signature...
Reply With Quote
  #3 (permalink)  
Old 11-13-05, 05:42 PM
Augustino Augustino is offline
Newbie Coder
 
Join Date: Nov 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
I used your script and say this warnings:

Warning: socket_create() Unable to create socket [1]: Operation not permitted in /home/august7/public_html/crearting/ping.php on line 14

Warning: socket_set_block() expects parameter 1 to be resource, boolean given in /home/august7/public_html/crearting/ping.php on line 15

Warning: socket_set_option() expects parameter 1 to be resource, boolean given in /home/august7/public_html/crearting/ping.php on line 79
Cannot connect to www.google.ca

Which is the problem?
Reply With Quote
  #4 (permalink)  
Old 11-13-05, 05:46 PM
Augustino Augustino is offline
Newbie Coder
 
Join Date: Nov 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Also found this http://pear.php.net/package/Net_Ping but have a require("pear.php");

Where can I find this file?

Last edited by Augustino; 11-13-05 at 06:45 PM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 05:29 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.