1
0

wait-for-connection 827 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/php
  2. <?php
  3. $timeout = 60;
  4. switch ($argc) {
  5. case 4:
  6. $timeout = (float)$argv[3];
  7. case 3:
  8. $host = $argv[1];
  9. $port = (int)$argv[2];
  10. break;
  11. default:
  12. fwrite(STDERR, 'Usage: '.$argv[0].' host port [timeout]'."\n");
  13. exit(2);
  14. }
  15. if ($timeout < 0) {
  16. fwrite(STDERR, 'Timeout must be greater than zero'."\n");
  17. exit(2);
  18. }
  19. if ($port < 1) {
  20. fwrite(STDERR, 'Port must be an integer greater than zero'."\n");
  21. exit(2);
  22. }
  23. $socketTimeout = (float)ini_get('default_socket_timeout');
  24. if ($socketTimeout > $timeout) {
  25. $socketTimeout = $timeout;
  26. }
  27. $stopTime = time() + $timeout;
  28. do {
  29. $sock = @fsockopen($host, $port, $errno, $errstr, $socketTimeout);
  30. if ($sock !== false) {
  31. fclose($sock);
  32. fwrite(STDOUT, "\n");
  33. exit(0);
  34. }
  35. sleep(1);
  36. fwrite(STDOUT, '.');
  37. } while (time() < $stopTime);
  38. fwrite(STDOUT, "\n");
  39. exit(1);