PHP Classes

File: index_ping.php

Recommend this page to a friend!
  Classes of Schroetter Christian   PHP Ping Traceroute and DNS   index_ping.php   Download  
File: index_ping.php
Role: Example script
Content type: text/plain
Description: Ping example
Class: PHP Ping Traceroute and DNS
Send ping and traceroute ICMP request messages
Author: By
Last change:
Date: 9 years ago
Size: 1,382 bytes
 

Contents

Class file image Download
<?php
   
include_once('./ping.class.php');

    function
PrintUsage($string = '')
    {
        if(
$string != '') {
            echo
$string . "\n\n";
        }

        echo
'php index_ping.php [addr] [-c:X] [-t:Y]' . "\n";
        echo
"\t" . '[addr] mandatory: The host (IP address or host name) to ping...' . "\n";
        echo
"\t" . '[-c:X] optionnal: The number of ping rtequest to send' . "\n";
        echo
"\t" . '[-t:X] optionnal: The timeout value' . "\n";
        die(
"\n");
    }

    if(
$argc <= 1) {
       
PrintUsage('Paramètre(s) invalide(s)...');
        die(
"\n");
    }

   
$try = 4;
   
$timeout = 2500;
    for(
$i = 0 ; $i < $argc ; $i++)
    {
        if(
strpos($argv[$i], '-c:') !== false)
           
$try = (int)substr($argv[$i], 3);
        else if(
strpos($argv[$i], '-t:') !== false)
           
$timeout = (int)substr($argv[$i], 3);
        else
            echo
$argv[$i] . "\n";
    }

   
$ping = new CPing();
   
$overall = $success = 0;

    echo
'#' . "\t" . 'IP' . "\t\t\t" . 'Time (ms)' . "\t" . 'TTL' . "\n";
    for(
$i = 0 ; $i < $try ; $i++) {

       
$res = $ping->Ping($argv[1], $timeout);
        if(
$res === false) {
            echo
$argv[1] . ' ne répond pas au ping...' . "\n";
            die();
        } else {

            echo (
$i + 1) . "\t" . $res['answer']->ipHeader->iaSrc . "\t\t" . sprintf('%.3f', $res['time']) . "\t\t" . $res['answer']->ipHeader->TTL . "\n";

           
$success++;
           
$overall += $res['time'];
        }
    }
    echo (
$success * 100.0 / $try) . "%\t" . $argv[1] . "\t" . sprintf('%.3f', $overall / $try) . "\n";

?>