add blank dhcpd.conf for all configurations
[debian6500.git] / master / opt / script / check_switches
CommitLineData
cb212393
LM
1#!/bin/bash
2#
3# ============================================================================
4#
5# Copyright (C) THALES. All rights reserved
6# Author: Laurent Mazet
7#
8# Gestion attente switches au boot (ping,telnet)
9# ============================================================================
10
11# TODO
12# *
13CMD=""
14MODE="iter"
15PROGNAME=$(basename $0)
16VER=1.0
17EXCLUDE="___"
18
19function usage () {
20 echo "usage: $PROGNAME [-c command] [-h] [-i] [-o] [-t seconds] [-v] [-w] [-x] host1 host2..."
21 echo " -c: set command (default '$CMD')"
22 echo " -h: help message"
23 echo " -i: infinity loop mode [default]"
24 echo " -o: only try ones"
25 echo " -t: timeout"
26 echo " -v: version message"
27 echo " -w: wait mode"
28 exit $@
29}
30
31while [ $# -gt 0 ]; do
32 case "$1" in
33 -c) shift; CMD+=" -c '${1// /@@}'";;
34 -h) usage 0;;
35 -i) MODE="iter";;
36 -o) MODE="ones";;
37 -t) shift; [[ "$1" =~ [^0-9] ]] && { echo "incorrect timeout ($1)"; exit 1;}; TIMEOUT=$1;;
38 -v) echo "$PROGNAME: version $VER"; exit;;
39 -w) MODE="wait";;
40 *) list+=" "$1;;
41 esac
42 shift
43done
44
45# list
46[ "$list" ] || list=$(awk '{sub(/#.*/, "")} /sw[gm]/ {print $2}' /etc/hosts)
47list=$(echo $list" ")
48
49# timeout
50if [ "$TIMEOUT" ]; then
51 trap "echo 'timeout expired (${TIMEOUT}s)'; exit 0;" SIGVTALRM
52 ( sleep $TIMEOUT; kill -SIGVTALRM $$ 2>/dev/null; [ $? -eq 0 ] && rc=1 || rc=0; exit $rc) &
53fi
54trap "exit 0;" SIGINT
55
56# main loop
57while true; do
58 for host in $list; do
59 stat=0
60 echo -n ${host}:" "
61 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
62 [ "$MODE" = "wait" -a $stat -eq 1 ] && list=${list/$host }
63 done
64 [ "$MODE" = "ones" ] && break;
65 [[ "$list" =~ [a-zA-Z0-9_-] ]] && sleep 1 || break;
66done