Guest User

Untitled

a guest
May 28th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. TIMEOUT=15
  4. QUIET=0
  5.  
  6. echoerr() {
  7. if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi
  8. }
  9.  
  10. usage() {
  11. exitcode="$1"
  12. cat << USAGE >&2
  13. Usage:
  14. $cmdname host:port [-t timeout] [-- command args]
  15. -q | --quiet Do not output any status messages
  16. -t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
  17. -- COMMAND ARGS Execute command with args after the test finishes
  18. USAGE
  19. exit "$exitcode"
  20. }
  21.  
  22. wait_for() {
  23. command="$*"
  24. for i in `seq $TIMEOUT` ; do
  25. nc -z "$HOST" "$PORT" > /dev/null 2>&1
  26.  
  27. result=$?
  28. if [ $result -eq 0 ] ; then
  29. if [ -n "$command" ] ; then
  30. exec $command
  31. fi
  32. exit 0
  33. fi
  34. sleep 1
  35. done
  36. echo "Operation timed out" >&2
  37. exit 1
  38. }
  39.  
  40. while [ $# -gt 0 ]
  41. do
  42. case "$1" in
  43. *:* )
  44. HOST=$(printf "%s\n" "$1"| cut -d : -f 1)
  45. PORT=$(printf "%s\n" "$1"| cut -d : -f 2)
  46. shift 1
  47. ;;
  48. -q | --quiet)
  49. QUIET=1
  50. shift 1
  51. ;;
  52. -t)
  53. TIMEOUT="$2"
  54. if [ "$TIMEOUT" = "" ]; then break; fi
  55. shift 2
  56. ;;
  57. --timeout=*)
  58. TIMEOUT="${1#*=}"
  59. shift 1
  60. ;;
  61. --)
  62. shift
  63. break
  64. ;;
  65. --help)
  66. usage 0
  67. ;;
  68. *)
  69. echoerr "Unknown argument: $1"
  70. usage 1
  71. ;;
  72. esac
  73. done
  74.  
  75. if [ "$HOST" = "" -o "$PORT" = "" ]; then
  76. echoerr "Error: you need to provide a host and port to test."
  77. usage 2
  78. fi
  79.  
  80. wait_for "$@"
Add Comment
Please, Sign In to add comment