Ver Mensaje Individual
  #6 (permalink)  
Antiguo 14/08/2011, 14:39
Avatar de andresdzphp
andresdzphp
Colaborador
 
Fecha de Ingreso: julio-2011
Ubicación: $this->Colombia;
Mensajes: 2.749
Antigüedad: 12 años, 9 meses
Puntos: 793
Respuesta: script para hacer ping en php

Estas funciones me las encontré un día leyendo todo el manual PHP, tal vez te sirvan:

Código PHP:
Ver original
  1. <?php
  2. function GetPing($ip=NULL) {
  3.  if(empty($ip)) {$ip = $_SERVER['REMOTE_ADDR'];}
  4.  if(getenv("OS")=="Windows_NT") {
  5.   $exec = exec("ping -n 3 -l 64 ".$ip);
  6.   return end(explode(" ", $exec ));
  7.  }
  8.  else {
  9.   $exec = exec("ping -c 3 -s 64 -t 64 ".$ip);
  10.   $array = explode("/", end(explode("=", $exec )) );
  11.   return ceil($array[1]) . 'ms';
  12.  }
  13. }
  14.  
  15. $ip = '190.28.6.20';
  16.  
  17. if (GetPing($ip) == 'perdidos),') {
  18.     echo 'Tiempo agotado';
  19. } else if (GetPing($ip) == '0ms') {
  20.     echo 'servidor apagado';
  21. } else {
  22.     echo 'servidor con conectividad';
  23. }

Código PHP:
Ver original
  1. function ping($host, $timeout = 1) {
  2.     /* ICMP ping packet with a pre-calculated checksum */
  3.     $package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";
  4.     $socket = socket_create(AF_INET, SOCK_RAW, 1);
  5.     socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0));
  6.     socket_connect($socket, $host, null);
  7.  
  8.     $ts = microtime(true);
  9.     socket_send($socket, $package, strLen($package), 0);
  10.     if (socket_read($socket, 255))
  11.         $result = microtime(true) - $ts;
  12.     else
  13.         $result = false;
  14.     socket_close($socket);
  15.  
  16.     return $result;
  17. }

Código PHP:
Ver original
  1. function hacerPing($ip) {
  2.     $str = exec("ping -n 1 -w 1 $ip", $res, $ret);
  3.     if ($ret == 0) {
  4.         return true;
  5.     }
  6.     return false;
  7. }
  8.  
  9. if (hacerPing('154.0.0.45') === true) {
  10.     echo 'ON <br>' . $res;
  11. } else {
  12.     echo 'OFF <br>' . $res;
  13. }
__________________
Si sabemos como leer e interpretar el manual será mucho más fácil aprender PHP. En lugar de confiar en ejemplos o copiar y pegar - PHP