Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2005
    Posts
    694

    Get ping time - php

    Hi all,

    I want to based on ping results, not if it came back results or not. Basically i want to echo out the ip if pings higher or lower than 100ms.

    The below is what I have, please help me:

    Code:
    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    $on = '2097216';
    $off = '2097220';
          
    
    		
        $str=exec("ping -c 1 -w 1 $justip",$a,$a1);
      
      if (strlen($str)<1) {
    	//update gateway that this **** is down first
    	
    		echo "ping is too high";
    
    	     
    		} else {
    		
    			if ($ip['reason']<2) {
    			
    			echo "ping is good";
    		}
    
      
      
     ?>
    Thank you in advance...

  2. #2
    Join Date
    Mar 2007
    Location
    UK
    Posts
    89
    Code:
    <?php
    echo "<pre>";
    $host = 'www.google.com';
    $pingTime = shell_exec('ping -q -c1 ' . $host . ' | grep "packets transmitted" | sed s/"^[[:print:]]* time \([0-9]*\)ms$"/\\\\1/g');
    
    echo "ping time:" . $pingTime . "ms\n";
    
    echo "Full ping output:" . shell_exec('ping -q -c1 ' . $host) . "\n</pre>";
    ?>

  3. #3
    Join Date
    Apr 2005
    Posts
    694
    Thank you so much, here is what I got:

    Code:
    ping time:0
    ms
    Full ping output:PING www.l.google.com (74.125.65.106) 56(84) bytes of data.
    
    --- www.l.google.com ping statistics ---
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 18.889/18.889/18.889/0.000 ms

  4. #4
    Join Date
    Mar 2007
    Location
    UK
    Posts
    89
    So I take it that you successfully adapted my code snippet to suit your purposes?

  5. #5
    Join Date
    Apr 2005
    Posts
    694
    Yes, indeed. Thank you for your help.

  6. #6
    Join Date
    Jul 2008
    Posts
    45
    Always escape arguments passed to shell_exec.

    Replace $host with
    PHP Code:
    escapeshellarg($host
    Even if this script won't be used by anyone else other than you, it's good practice to escape everything properly.

    Also based on your reply that code snippet doesn't look to be parsing the ping output correctly (0 ms?). It is also calling ping twice.

    Here is my function:
    PHP Code:
    function Ping($host$timeout 10)
    {
        
    $output = array();
        
    $com 'ping -n -w ' $timeout ' -c 1 ' escapeshellarg($host);
        
        
    $exitcode 0;
        
    exec($com$output$exitcode);
        
        if (
    $exitcode == || $exitcode == 1)
        { 
            foreach(
    $output as $cline)
            {
                if (
    strpos($cline' bytes from ') !== FALSE)
                {
                    
    $out = (int)ceil(floatval(substr($clinestrpos($cline'time=') + 5)));
                    return 
    $out;
                }
            }
        }
        
        return 
    FALSE;


    Returns the ping or FALSE if theres an error (like a timeout, unreachable host or unresolvable name). You can also tweak the timeout threshold.

    PHP Code:
    $ping Ping('www.google.com');

    if (
    $ping === FALSE)
        echo 
    'host is down';
    else if (
    $ping 500)
        echo 
    'high ping';
    else
        echo 
    'ping is good'
    Always check for false with the type equality operator (http://php.net/manual/en/language.op...comparison.php)
    Last edited by Unquantifiable; 05-04-2012 at 11:29 AM.
    Cost effective and reliable website & server monitoring
    OnUptime - www.onuptime.com

  7. #7
    Join Date
    Jul 2009
    Location
    Atlanta, GA
    Posts
    620
    You can do this without using shell_exec at all. shell_exec() is sometimes disabled in PHP for security reasons.

    I found this code a while back and adopted it for a function I was working on.

    PHP Code:
    <?php
    function icmp_checksum($data) {
      if (
    strlen($data) % 2) {
        
    $data .= "\x00";
      }
      
    $bit unpack('n*'$data);
      
    $sum array_sum($bit);
      while  (
    $sum  >> 16) {
        
    $sum = ($sum >> 16) + ($sum 0xffff);
      }
      return 
    pack('n*', ~$sum);
    }

    function 
    ping($host) {
      
    $tmp "\x08\x00\x00\x00\x00\x00\x00\x00PingTest";
      
    $checksum icmp_checksum($tmp);
      
    $package "\x08\x00".$checksum."\x00\x00\x00\x00PingTest";
      
    $socket socket_create(AF_INETSOCK_RAW1);
      
    socket_connect($socket$hostnull);
      
    $timer microtime(1);
      
    socket_send($socket$packagestrlen($package), 0);
      if (
    socket_read($socket255)) {
        return 
    round((microtime(1) - $timer) * 10002);
      }
    }

    $pingtime ping('www.google.com');
    echo 
    $pingtime."ms";

    ?>
    Frank Laszlo - Developer
    Franksworld Solutions, LLC

  8. #8

    Thanks :)

    Thanks - Great Help

  9. #9
    Join Date
    Apr 2005
    Posts
    694
    Hello,

    Here's what I got but it's returning unknown host:

    Code:
    function Ping($host, $timeout = 10)
    {
        $output = array();
        $com = 'ping -n -w ' . $timeout . ' -c 1 ' . escapeshellarg($host);
    
        $exitcode = 0;
        exec($com, $output, $exitcode);
    
        if ($exitcode == 0 || $exitcode == 1)
        {
            foreach($output as $cline)
            {
                if (strpos($cline, ' bytes from ') !== FALSE)
                {
                    $out = (int)ceil(floatval(substr($cline, strpos($cline, 'time=') + 5)));
                    return $out;
                }
            }
        }
    
        return FALSE;
    }
    
                    $result = mysql_query("SELECT * FROM `gateways`",$xb) or die(mysql_error());
                    $num_rows = mysql_num_rows($result);
                    if($num_rows>0)
                            {
                            while($row = mysql_fetch_array($result)){
    
                            $ips = substr($row['ip'],0,-5);
                            $ping = Ping('$ips');
                            //echo $row['ip'];
                            if($ping === FALSE) {
                            mysql_query("UPDATE `gateways` SET `active`=0 WHERE `id`='".$row['id']."' limit 1", $xb);
                            } else {
                            mysql_query("UPDATE `gateways` SET `active`=1 WHERE `id`='".$row['id']."' limit 1", $xb);
                            }


    Quote Originally Posted by Unquantifiable View Post
    Always escape arguments passed to shell_exec.

    Replace $host with
    PHP Code:
    escapeshellarg($host
    Even if this script won't be used by anyone else other than you, it's good practice to escape everything properly.

    Also based on your reply that code snippet doesn't look to be parsing the ping output correctly (0 ms?). It is also calling ping twice.

    Here is my function:
    PHP Code:
    function Ping($host$timeout 10)
    {
        
    $output = array();
        
    $com 'ping -n -w ' $timeout ' -c 1 ' escapeshellarg($host);
        
        
    $exitcode 0;
        
    exec($com$output$exitcode);
        
        if (
    $exitcode == || $exitcode == 1)
        { 
            foreach(
    $output as $cline)
            {
                if (
    strpos($cline' bytes from ') !== FALSE)
                {
                    
    $out = (int)ceil(floatval(substr($clinestrpos($cline'time=') + 5)));
                    return 
    $out;
                }
            }
        }
        
        return 
    FALSE;


    Returns the ping or FALSE if theres an error (like a timeout, unreachable host or unresolvable name). You can also tweak the timeout threshold.

    PHP Code:
    $ping Ping('www.google.com');

    if (
    $ping === FALSE)
        echo 
    'host is down';
    else if (
    $ping 500)
        echo 
    'high ping';
    else
        echo 
    'ping is good'
    Always check for false with the type equality operator (http://php.net/manual/en/language.op...comparison.php)

Similar Threads

  1. Ping time significant?
    By diesel12 in forum Web Hosting
    Replies: 9
    Last Post: 09-18-2008, 03:52 PM
  2. What does ping time mean?
    By ivytony in forum VPS Hosting
    Replies: 6
    Last Post: 03-23-2007, 02:29 AM
  3. good ping time?
    By dotdoms in forum Dedicated Server
    Replies: 20
    Last Post: 01-13-2005, 08:30 PM
  4. ping time request
    By netdude in forum Dedicated Server
    Replies: 27
    Last Post: 10-24-2002, 04:48 AM
  5. Ping-time
    By marco in forum Hosting Security and Technology
    Replies: 1
    Last Post: 06-28-2001, 03:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •