Created
December 11, 2015 17:31
-
-
Save psi-4ward/c0cb0d43cb3f256037c8 to your computer and use it in GitHub Desktop.
waitForPort.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
MAXTIME=30 | |
PORTS=() | |
START_TSTAMP=$(date "+%s") | |
function help() { | |
>&2 echo "Wait until a port becomes open" | |
>&2 echo | |
>&2 echo "Usage: $0 [options] -h HOST -p Port" | |
>&2 echo "Usage: $0 [options] -c Container -p Port" | |
>&2 echo | |
>&2 echo "Options:" | |
>&2 echo " -p Port, multiple notations supported" | |
>&2 echo " -h Hostname or IP" | |
>&2 echo " -c Container ID or Name for IP lookup" | |
>&2 echo " -t Max wait time in seconds (default $MAXTIME)" | |
>&2 echo " -v Verbose verboseOut" | |
>&2 echo | |
} | |
# Check if nmap is installed | |
type nmap >/dev/null 2>&1 || { | |
echo >&2 "ERROR: nmap is not installed" | |
exit 1 | |
} | |
# Parse CLI args | |
while [[ $# > 0 ]]; do | |
key=$1 | |
case "$key" in | |
-p) | |
PORTS+=($2) | |
shift | |
;; | |
-h) | |
HOST=$2 | |
shift | |
;; | |
-c) | |
CONTAINER=$2 | |
shift | |
;; | |
-t) | |
MAXTIME=$2 | |
shift | |
;; | |
-v) | |
VERBOSE=true | |
;; | |
*) | |
>&2 echo WARN: Ignoring unknown argument $key | |
;; | |
esac | |
shift # past argument or value | |
done | |
# Validate -p args | |
if [ ${#PORTS[@]} == 0 ] ; then | |
>&2 echo ERROR: -p argument missing | |
>&2 echo | |
help | |
exit 1 | |
fi | |
# Validat if either -h or -c is present | |
if [ "$HOST" == "" ] && [ "$CONTAINER" == "" ] ; then | |
>&2 echo ERROR: -h or -c argument missing | |
>&2 echo | |
help | |
exit 1 | |
fi | |
# output helper | |
function verboseOut() { | |
[ "$VERBOSE" != true ] && return | |
echo $@ | |
} | |
function errorOut() { | |
>&2 echo $@ | |
} | |
# Helper to get Container IP | |
function getContainerIP() { | |
echo $(docker inspect --format '{{ .NetworkSettings.IPAddress }}' $CONTAINER 2>/dev/null) | |
} | |
# Resolve IP from Container name | |
if [ "$HOST" == "" ] && [ "$CONTAINER" != "" ] ; then | |
while [ "$(getContainerIP)" == "" ] ; do | |
if [ $(expr $(date "+%s") - $MAXTIME) -ge $START_TSTAMP ]; then | |
errorOut "Could not get IP from Container $CONTAINER within ${MAXTIME}s. Giving up." | |
exit 2 | |
fi | |
verboseOut "Waiting for Container to came up and aquire an IP" | |
sleep 1 | |
done | |
HOST=$(getContainerIP) | |
fi | |
# wait until all ports are up | |
LC_ALL=C | |
for PORT in "${PORTS[@]}" ; do | |
while ! nmap --host-timeout 2 -Pn -p$PORT $HOST -oG - | grep -qF "Ports: $PORT/open"; do | |
if [ $(expr $(date "+%s") - $MAXTIME) -ge $START_TSTAMP ]; then | |
errorOut "Port $PORT not opened in $MAXTIME seconds. Giving up." | |
exit 2 | |
fi | |
verboseOut "Waiting for ${HOST}:${PORT}..." | |
sleep 1 | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment