+#!/bin/bash
+#
+# ============================================================================
+#
+# Copyright (C) THALES. All rights reserved
+# Author: Laurent Mazet
+#
+# Gestion attente switches au boot (ping,telnet)
+# ============================================================================
+
+# TODO
+# *
+CMD=""
+MODE="iter"
+PROGNAME=$(basename $0)
+VER=1.0
+EXCLUDE="___"
+
+function usage () {
+ echo "usage: $PROGNAME [-c command] [-h] [-i] [-o] [-t seconds] [-v] [-w] [-x] host1 host2..."
+ echo " -c: set command (default '$CMD')"
+ echo " -h: help message"
+ echo " -i: infinity loop mode [default]"
+ echo " -o: only try ones"
+ echo " -t: timeout"
+ echo " -v: version message"
+ echo " -w: wait mode"
+ exit $@
+}
+
+while [ $# -gt 0 ]; do
+ case "$1" in
+ -c) shift; CMD+=" -c '${1// /@@}'";;
+ -h) usage 0;;
+ -i) MODE="iter";;
+ -o) MODE="ones";;
+ -t) shift; [[ "$1" =~ [^0-9] ]] && { echo "incorrect timeout ($1)"; exit 1;}; TIMEOUT=$1;;
+ -v) echo "$PROGNAME: version $VER"; exit;;
+ -w) MODE="wait";;
+ *) list+=" "$1;;
+ esac
+ shift
+done
+
+# list
+[ "$list" ] || list=$(awk '{sub(/#.*/, "")} /sw[gm]/ {print $2}' /etc/hosts)
+list=$(echo $list" ")
+
+# timeout
+if [ "$TIMEOUT" ]; then
+ trap "echo 'timeout expired (${TIMEOUT}s)'; exit 0;" SIGVTALRM
+ ( sleep $TIMEOUT; kill -SIGVTALRM $$ 2>/dev/null; [ $? -eq 0 ] && rc=1 || rc=0; exit $rc) &
+fi
+trap "exit 0;" SIGINT
+
+# main loop
+while true; do
+ for host in $list; do
+ stat=0
+ echo -n ${host}:" "
+ ping -c 1 $host >&/dev/null && { log=$(eval /opt/script/connect_switch $host ${CMD//@@/ } -c exit 2>/dev/null) && stat=1; echo ${log:=soon..} | sed 's/\r/\n'$host:' /g'; } || echo
+ [ "$MODE" = "wait" -a $stat -eq 1 ] && list=${list/$host }
+ done
+ [ "$MODE" = "ones" ] && break;
+ [[ "$list" =~ [a-zA-Z0-9_-] ]] && sleep 1 || break;
+done