add gateway in clovis-lite dhcp
[debian6500.git] / master / opt / script / check_network
1 #!/bin/bash
2 #
3 # ============================================================================
4 #
5 # Copyright (C) THALES. All rights reserved
6 # Author: Alexandre Cazalis
7 #
8 # Gestion attente lames esclaves au boot (ping,ssh)
9 #
10 # 2013-11-20 : V1.2 LMa
11 # - Ajout de la personalisation de la commande a executer
12 # par defaut (uptime). Ajout attente montage nfs avant demarrage
13 # 2014-05-28 : V1.3 LMa
14 # - Ajout d'un mode foreground pour ne tenter qu'une fois chaque commande
15 # - Ajout d'une commande pour exclude la lame maitre
16 #
17 # 2014-10-28 : Change owner
18 # ============================================================================
19
20 # TODO
21 # *
22
23 CMD="uptime"
24 MODE="iter"
25 PROGNAME=$(basename $0)
26 VER=1.3
27 EXCLUDE="___"
28
29 function usage () {
30 echo "usage: $PROGNAME [-c command] [-h] [-i] [-o] [-t seconds] [-v] [-w] [-x] host1 host2..."
31 echo " -c: set command (default '$CMD')"
32 echo " -h: help message"
33 echo " -i: infinity loop mode [default]"
34 echo " -o: only try ones"
35 echo " -t: timeout"
36 echo " -v: version message"
37 echo " -w: wait mode"
38 echo " -x: avoid master blade"
39 exit $@
40 }
41
42 while [ $# -gt 0 ]; do
43 case "$1" in
44 -c) shift; CMD=$1;;
45 -h) usage 0;;
46 -i) MODE="iter";;
47 -o) MODE="ones";;
48 -t) shift; [[ "$1" =~ [^0-9] ]] && { echo "incorrect timeout ($1)"; exit 1;}; TIMEOUT=$1;;
49 -v) echo "$PROGNAME: version $VER"; exit;;
50 -w) MODE="wait";;
51 -x) EXCLUDE="trc00";;
52 *) list+=" "$1;;
53 esac
54 shift
55 done
56
57 # list
58 [ "$list" ] || list=$(awk '{sub(/#.*/, "")} /trc[0-9][0-9]/ && ! /-rf/ {print $2}' /etc/hosts)
59 list=$(echo $list" "|sed s/$EXCLUDE//)
60
61 # timeout
62 if [ "$TIMEOUT" ]; then
63 trap "echo 'timeout expired (${TIMEOUT}s)'; exit 0;" SIGVTALRM
64 ( sleep $TIMEOUT; kill -SIGVTALRM $$ 2>/dev/null; [ $? -eq 0 ] && rc=1 || rc=0; exit $rc) &
65 fi
66 trap "exit 0;" SIGINT
67
68 # main loop
69 while true; do
70 for host in $list; do
71 stat=0
72 echo -n ${host}:" "
73 ping -c 1 $host >&/dev/null && { ssh -t -o ConnectTimeout=1 root@$host "$CMD" 2>/dev/null && stat=1 || echo "soon..."; } || echo
74 [ "$MODE" = "wait" -a $stat -eq 1 ] && list=${list/$host }
75 done
76 [ "$MODE" = "ones" ] && break;
77 [[ "$list" =~ [a-zA-Z0-9_-] ]] && sleep 1 || break;
78 done