Merge "Fix CSIT for PMSH"
[integration/csit.git] / scripts / policy / wait_for_port.sh
1 #!/bin/sh
2
3 tmout=120
4 cmd=
5 while getopts c:t: opt; do
6     case "$opt" in
7     c) cmd="$OPTARG" ;;
8     t) tmout="$OPTARG" ;;
9     esac
10 done
11 nargs=$(expr $OPTIND - 1)
12 shift $nargs
13
14 even_args=$(expr $# % 2)
15 if [ $# -lt 2 -o $even_args -ne 0 ]; then
16     echo "args: [-t timeout] [-c command] hostname1 port1 hostname2 port2 ..." >&2
17     exit 1
18 fi
19
20 while [ $# -ge 2 ]; do
21     export host=$1
22     export port=$2
23     shift
24     shift
25
26     echo "Waiting for $host port $port..."
27     timeout $tmout sh -c 'until nc -vz "$host" "$port"; do echo -n ".";
28         sleep 1; done'
29     rc=$?
30
31     if [ $rc != 0 ]; then
32         echo "$host port $port cannot be reached"
33         exit $rc
34     fi
35 done
36
37 $cmd
38
39 exit 0