CaFc_Versace

Pinging a host by PHP

Dec 22nd, 2015
2,184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.20 KB | None | 0 0
  1. <?php
  2. error_reporting(0);
  3.  
  4.   # usage; this.php domain/ip
  5.  
  6. function ping($host){
  7.     $starttime = microtime(true);
  8.     $exec = fsockopen($host, 80, $errno, $errstr, 5); // 80 = port; 5 = max. execution time
  9.     $stoptime = microtime(true);
  10.     $status = 0;
  11.  
  12.     if (!$exec) {
  13.         $status = "Request timed out.";
  14.     } else {
  15.         $status = ($stoptime - $starttime) * 1000;
  16.         $status = "Reply from " . $host . ": bytes=32 time=" . floor($status) . "ms TTL=53";
  17.     }
  18.     fclose($exec);
  19.     return $status;
  20. }
  21.  
  22. if (preg_match('/^[a-z]{2,6}+\.[-a-z0-9]+\.[a-z]{2,6}$|^[-a-z0-9]+\.[a-z]{2,6}$/', strtolower($argv[1]))) {
  23.     echo "\nPinging " . gethostbyaddr(gethostbyname($argv[1])) . " [" . gethostbyname($argv[1]) . "] with 32 bytes of data:\n";
  24.     while (true) {
  25.         echo ping(gethostbyname($argv[1])) . "\n";
  26.         sleep(1);
  27.     }
  28. } elseif (preg_match('/^[0-9]{1,3}+\.[0-9]{1,3}+\.[0-9]{1,3}+\.[0-9]{1,3}$/', strtolower($argv[1]))) {
  29.     echo "\nPinging " . $argv[1] . " with 32 bytes of data:\n";
  30.     while (true) {
  31.         echo ping($argv[1]) . "\n";
  32.         sleep(1);
  33.     }
  34. } else {
  35.     echo "Ping request could not find host " . $argv[1] . ". Please check the name and try again.";
  36. }
  37. ?>
Advertisement
Add Comment
Please, Sign In to add comment