Skip to content

Instantly share code, notes, and snippets.

@AndresPineros
Created October 4, 2017 15:57
Show Gist options
  • Save AndresPineros/5859c14b7fef78a46011fe40e845b915 to your computer and use it in GitHub Desktop.
Save AndresPineros/5859c14b7fef78a46011fe40e845b915 to your computer and use it in GitHub Desktop.
usage: wait.sh 30 google.com:80 yahoo.com:80
#!/bin/bash
# Use this script to test if a given TCP host/port are available
cmdname=$(basename $0)
timeout=30
if [[ $1 =~ ^[0-9]+$ ]]; then
timeout=$1
shift 1
fi
while [[ $# -gt 0 ]]; do
case "$1" in
*:* )
address=$1
address=${address##http://}
address=${address##https://}
address=${address%%/*}
hostport=(${address//:/ })
HOST=${hostport[0]}
PORT=${hostport[1]}
echo "Waiting $timeout seconds on $HOST:$PORT..."
timeout $timeout nc -z $HOST $PORT
if [[ $? -ne 124 ]]; then
echo "Connected to ${HOST}"
else
echo "Connection refused ${HOST}"
exit 1
fi
shift 1
;;
*)
echo "Argument: \"$1\" has no HOST:PORT format."
exit 1
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment