[PHP] ICMP-Ping

Dieses Thema im Forum "Webentwicklung" wurde erstellt von Smokers, 17. November 2007 .

  1. 17. November 2007
    ICMP-Ping

    Hallo,

    ich suche eine Möglichkeit einen ICMP Ping durchzuführen.
    Ich habe zwar normale Ping-Methoden in Erwägung gezogen, jedoch sind die IPs bzw die Server dazu so unterschiedlich das ich nicht sagen kann welcher Port bei jedem offen wäre.

    Somit würde ich gern einen ICMP Ping nutzen. Hab auch schon google genutzt jedoch nur eine Klasse gefunden mit der ich nicht wirklich was anfangen kann.. leider.
    ( Weil ich mit Klassen noch sogut wie nie gearbeitet habe, war einfach bisher nicht nötig)


    Hier mal die Klasse die ich gefunden hatte abe rnicht weis wie ich sie nutzen soll:



    PHP:
    <?
    // class_ICMP.php - ICMP Ping class
    // Version 0.1



    function  hex2dec (& $item , $key ) {
        
    $item  hexdec ( $item );
    }


    function  cbk ( $type , $data ) {
        switch(
    $type ) {
          case 
    "error" :
            
    $msg  "Error: " . $data [ "during" ]. ": " . $data [ "error" ];
            break;
          case 
    "send" :
            
    $msg  "Sent " . $data [ "bytes" ]. " bytes (" . $data [ "packet" ]. ")" ;
              break;
          case 
    "begin" :
                
    $msg  "PING " . $data [ "hostname" ]. " (" . $data [ "address" ]. "): " . $data [ "bytes" ]. " data bytes" ;
              break;
          case 
    "receive" :
            
    $msg  $data [ "bytes" ]. " bytes from " . $data [ "address" ]. ": icmp_seq=" . $data [ "packet" ]. " ttl=" . $data [ "ttl" ]. " time=" . $data [ "time" ]. " ms" ;
              break;
          case 
    "timeout" :
            
    $msg  "Request timed out" ;
              break;
          case 
    "statistics" :
            
    $msg  "\n --- " . $data [ "hostname" ]. " ping statistics ---\n" ;
          
    $msg  .=  $data [ "transmitted" ]. " packets transmitted, " . $data [ "received" ]. " packets received, " . $data [ "loss" ]. "% packet loss\n" ;
          
    $msg  .=  "round-trip min/avg/max = " . $data [ "mintime" ]. "/" . $data [ "avgtime" ]. "/" . $data [ "maxtime" ]. " ms" ;
      }
        if (
    $data [ "display" ]) echo  " $msg \n" ; else return  $msg ;
    }


    class  ICMP  {

        

        
    function  ICMP () {
          
    $this -> display  false ;
          
    $this -> verbose  false ;
        
    $this -> callbacks  = array(
            
    "error" => "cbk" ,
          
    "send" => "cbk" ,
          
    "begin" => "cbk" ,
          
    "receive" => "cbk" ,
          
    "timeout" => "cbk" ,
          
    "statistics" => "cbk"
        
    );
      }

        function 
    set_callback ( $type , $functionname ) {
          
    $this -> callbacks [ $type ] =  $functionname ;
      }

        

      
    function  getmicrotime () {
        list(
    $usec $sec ) =  explode ( " " , microtime ());
        return ((float)
    $usec  + (float) $sec );
      }

        

        
    function  repack  ( $var ) {
        
    $var  pack ( "n" , $var );
        
    $temp  unpack ( "n" , $var );
        
    $aux  $temp [ "" ];
        return 
    $aux ;
        }

        

        
    function  checksum ( $buffer ) {
        
    $cksum  0 ;
        
    $counter  0 ;
        
    //var_dump($buffer);
        
    $i  1 ;
        
        foreach (
    $buffer  as  $value ) {
                if (
    $i == 0 ) {
                
    $value1  .= $value ;
                
    $buff1 [] =  $value1 ;
                
    $i  1 ;
                
    $value1 = 0 ;
                } else {
                
    $value1 = $value ;
                
    $i  0 ;
                }
        }    
        
    $buff1 [] =  $value1 ;
        
        foreach (
    $buff1  as  $value ) {
                
    $aux  substr ( $value , 2 , 4 ). substr ( $value , 0 , 2 );
                
    $value1  hexdec ( $aux );
                
    $cksum  +=  $value1 ;
        }

        
    $aux  $this -> repack (( $cksum  0xffff ));

        
    $cksum1  $this -> repack ((( $cksum  >>  16 ) +  $aux ));
        
    $cksum2  $this -> repack (( $cksum1  + ( $cksum1  >>  16 )));
        
    $ans  = ~ $cksum2 ;
        
    $ans  $this -> repack ( $ans );

        
    $csum1  dechex ( $ans );
        
    $buffer [ 2 ] =  substr ( $csum1 , 2 , 4 );
        
    $buffer [ 3 ] =  substr ( $csum1 , 0 , 2 );

        return 
    $buffer ;
        }

        

        
    function  view_packet ( $data ){
        
    $datas  explode ( " " , $data );
        
    $i  1 ;
        foreach (
    $datas  as  $aux ) {

                
    $val  =   hexdec ( $aux );
                
    $val2  chr ( $val );
                
    $val3  decbin ( $val );
                
    $val4  str_pad ( $val3 , 8 , "0" , STR_PAD_LEFT );  //bin

                
    $show1 .=  $aux //hex
                
    $show4 .=  $val4 //bin
        
                
    if ((( $i % 2 )== 0 ))   $show1 .=  " " ;
                if (((
    $i % 2 )== 0 ))   $show4 .=  " " ;

                if (
    $i  ==  8 ) {
                
    $show1 .=  "\n" ;
                
    $show4 .=  "\n" ;
                
    $i = 1 ;
                } else {
                
    $i ++;
                }
        }
        echo 
    "\n------------------------------------------\n" ;
        echo 
    " $show1 \n\n $show4 " ;
        
    //echo "$show1";
        
    echo  "\n------------------------------------------\n" ;

        
    flush ();

        }

        

        
    function  callback ( $type , $data ) {
          if (
    $this -> callbacks [ "all" ]) {
            
    $cbk  $this -> callbacks [ "all" ];
        } elseif (
    $this -> callbacks [ $type ]) {
          
    $cbk  $this -> callbacks [ $type ];
          if (
    $cbk == "cbk" $data [ "display" ] =  $this -> display ;
        } else {
              return 
    false ;
          }
          
    $cbk ( $type , $data );
      }

        

        
    function  error ( $during , $msg ) {
          
    $this -> callback (
            
    "error" ,
          array(
              
    "during" => $during ,
            
    "error" => $msg
          
    )
        );
      }

        

        
    function  send_ping () {

              
    $this -> pktno ++;
                
    $conn  socket_connect ( $this -> sock , $this -> ipaddr , 0 );
          if (!
    $conn ) {
                    
    $this -> error ( "connecting to socket" , socket_strerror ( socket_last_error ()));
            return 
    false ;
          }
        
                
    $this -> datas [ 6 ]=  "05" // type (internal);
                
    $this -> datas [ 7 ]=  str_pad ( dechex ( $this -> pktno ), 2 , "0" , STR_PAD_LEFT );  //seqnum
        
            
    $datas_aux  $this -> checksum ( $this -> datas );

                
    //view_packet(join(" ",$datas_aux));

                
    unset( $val1 );
          foreach (
    $datas_aux  as  $aux ) {
                    
    $val  =   hexdec ( $aux );
                    
    $val2  chr ( $val );
                    
    $val1  .=  $val2 ;
            }

          
    $this -> times [ $this -> pktno ] =  $this -> getmicrotime ();
            
    $aux  socket_write ( $this -> sock , $val1 , strlen ( $val1 ));
          if (
    $aux  ===  false ) {
              
    $this -> error ( "writing to socket" , socket_strerror ( socket_last_error ()));
          } elseif (
    $this -> verbose ) {
                  
    $this -> callback (
                  
    "send" ,
                        array(
                    
    "bytes" => $aux ,
                  
    "packet" => $this -> pktno
              
    )
            );
            }
          return 
    $aux ;

        }

        

        
    function  ping ( $hostname , $maxpackets = 3 ) {
          
    $this -> hostname  $hostname ;
          
    $this -> maxpackets  $maxpackets ;
        
    $this -> pktno  0 ;

        
    //       ty cd cksum ident seqnu data ---- 
        
    $data  "08 00 00 00 08 97 00 00 " .
                        
    "65 00 00 39 8e d8 3c a1 "
            
    "e1 05 " .
              
    "00 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 " //data
            
    "15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 "
            
    "25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34" ;
            
    //"35 36 37";

            
    unset( $this -> sock );
            
    $this -> sock  socket_create ( AF_INET , SOCK_RAW , getprotobyname ( "ICMP" ));
        if (
    $this -> sock  ===  false ) {
          
    $this -> error ( "creating socket" , socket_strerror ( socket_last_error ()));
          return 
    false ;
        }

            
    $this -> datas  explode ( " " , $data );
        
    $this -> ipaddr  gethostbyname ( $this -> hostname );

        
    $this -> callback (
            
    "begin" ,
          array(
                
    "hostname"  =>  $this -> hostname ,
            
    "address"  =>  $this -> ipaddr ,
            
    "bytes"  =>  count ( $this -> datas )
          )
        );

            for (
    $i  0 $i  $this -> maxpackets $i ++) {
            
    $aux  $this -> send_ping ();
            if (
    $aux  1 ) {
                    
    $num  0 ;
                    
    $timeout  0 ;

                    while ((
    $num  <=  0 ) and ( $timeout  100 ))  {
              
    $set  = array( $this -> sock );
                    
    $num  socket_select ( $set $s_write  NULL $s_accept  NULL 0 1000 );
              if (
    $num  ===  false ) {
                
    $this -> callback ( "waiting on socket" , socket_strerror ( socket_last_error ()));
              }
                        
    $timeout ++;
                    }

                    if (
    $num > 0 ) {
                    
    $aux  socket_read ( $this -> sock , 100 );

              
    $rxtime  $this -> getmicrotime ();

                    
    $len  strlen ( $aux )- 20 ;
                    unset(
    $val3 );
                   for (
    $o  1 $o  strlen ( $aux ) ; $o ++) {
                            
    $val  $aux [ $o ];
                            
    $val1  ord ( $val );
                            
    $val2  str_pad ( dechex ( $val1 ), 2 , "0" , STR_PAD_LEFT );
                            
    $val3  .= " $val2  " ;
                }

                    
    $data_recv  explode ( " " , $val3 );
                    
    //view_packet($val3);

                    
    $src  =   array_slice ( $data_recv , 11 , 4 );  array_walk ( $src , "hex2dec" );
                    
    $dest  array_slice ( $data_recv , 15 , 4 );  array_walk ( $dest , "hex2dec" );
                    
    $type  $data_recv [ 19 ];
                    
    $seq1  array_slice ( $data_recv , 25 , 2 );  array_walk ( $seq1 , "hex2dec" );
                    
    $source  join ( "." , $src );
                    
    $destination  join ( "." , $dest );
                    
    $seq  join ( ":" , $seq1 );
                    
    $ttl  hexdec ( $data_recv [ 7 ]);

                    
    $txtime  $this -> times [ $seq1 [ 1 ]];
                        
    $rtt_msec  = ( $rxtime  $txtime ) *  1000 ;
                    
    $total_rtt []= $rtt_msec ;

              
    $this -> callback (
                  
    "receive" ,
                array(
                    
    "bytes" => $len ,
                  
    "address" => $source ,
                  
    "packet" => $seq1 [ 1 ],
                  
    "ttl" => $ttl ,
                  
    "time" => sprintf ( "%.2f" , $rtt_msec )
                )
              );
                    } else {
                        
    $this -> callback (
                  
    "timeout" ,
                array()
              );
              }
            }
            }

          
    $this -> packetsrecv  count ( $total_rtt );
            
    socket_close ( $this -> sock );

        
    $rtt_min  = - 1 ;
        
    $rtt_max  0 ;
        if (
    is_array ( $total_rtt )) {
                foreach (
    $total_rtt  as  $rtt ) {
                     
    $rtt_total += $rtt ;
            if (
    $rtt > $rtt_max $rtt_max  $rtt ;
            if (
    $rtt < $rtt_min  ||  $rtt_min < 0 $rtt_min  $rtt ;
            }
                
    $rtt_avg  round ( $rtt_total / $this -> packetsrecv , 3 );
        } else {
                
    $rtt_total  0 ;
          
    $rtt_avg  0 ;
          
    $pct  100 ;
            }

            
    $this -> callback (
            
    "statistics" ,
          array(
                
    "hostname" => $this -> hostname ,
            
    "transmitted" => $this -> maxpackets ,
            
    "received" => $this -> packetsrecv ,
                    
    "loss" =>( 100 -(( $this -> packetsrecv / $this -> maxpackets )* 100 )),
            
    "avgtime" => sprintf ( "%.2f" , $rtt_avg ),
            
    "mintime" => sprintf ( "%.2f" , $rtt_min ),
            
    "maxtime" => sprintf ( "%.2f" , $rtt_max )
          )
        );

            return 
    $this -> packetsrecv ;
        }

    }

    ?>
    und als Beispiel gab es das hier:
    PHP:
    <? php
    require_once( "class_ICMP.php" );

    // example callback function; see class_ICMP.php for valid $data values
    function  timeout_callback ( $callback_type , $data ) {
        echo 
    "Agh!  Request timed out!\n" ;
    }

    $icmp  = new  ICMP ();

    // example of using callbacks to trap specific events; see class_ICMP.php
    // for a list of valid callbacks
    $icmp -> set_callback ( "timeout" , "timeout_callback" );

    $icmp -> display  true ;

    if (
    $_SERVER [ "argc" ]< 2 ) {
        echo 
    "Usage: " . $_SERVER [ "argv" ][ 0 ]. " hostname pingcount\n" ;
      die;
    }
    if (
    $_SERVER [ "argc" ]> 1 $hostname  $_SERVER [ "argv" ][ 1 ];
    if (
    $_SERVER [ "argc" ]> 2 $pingcount  $_SERVER [ "argv" ][ 2 ]; else  $pingcount  3 ;

    $pingreceived  $icmp -> ping ( $hostname , $pingcount );

    echo 
    "\n" ;
    if (
    $pingcount == $pingreceived ) {
      echo 
    "The server responded to all pings." ;
    } elseif (
    $pingreceived > 0 ) {
        echo 
    "The server responded to some pings, but there was intermittent packet loss." ;
    } else {
        echo 
    "The server did not respond." ;
    }
    echo 
    "\n" ;
    ?>
    Ich will aber nur ein oder 2 Pings und wissen ob sie ankommen.
    Mehr nicht ^^°
    Aber ka wie ich das mit der Klasse anstellen soll
     
  2. 26. November 2007
    AW: ICMP-Ping

    Ähm is nich euer ernst oder? ^^°

    Nichtmal MakenX oder Murdoc ? xD
     
  3. 26. November 2007
    AW: ICMP-Ping

    Ich versteh nicht, wo das Problem ist, eine Klasse anzuwenden...

    PHP:
    $icmp  = new  ICMP ();
    if(
    $icmp -> ping ( 'google.de' 2 ))
      echo 
    'server antwortet' ;
    else
      echo 
    'server antwortet nicht' ;
     
  4. 26. November 2007
    AW: ICMP-Ping

    Genau da ist der Fehler.

    Bei jener Anwendung kommt nämlich folgender Fehler:

    Code:
     Fatal error: Unsupported operand types in
    
    C:\****\include\class_ICMP.php on line 129
    
    Und bevor ich den Fehler auf die Klasse schiebe habe ich gedacht schau ich
    mir lieber mal an ob ich sie auch richtig nutze. ;-)
     
  5. 30. November 2007
    AW: ICMP-Ping

    mal in zeile 129 geguckt?!

    $ans = ~$cksum2;

    muss heißen:
    $ans = $cksum2;
     
  6. Video Script

    Videos zum Themenbereich

    * gefundene Videos auf YouTube, anhand der Überschrift.