Merge branch 'master' of srvgitgnv:/gitstore/g_tr65/debian6500
[debian6500.git] / master / opt / script / check_network
CommitLineData
b3f3562f
OL
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
23CMD="uptime"
24MODE="iter"
25PROGNAME=$(basename $0)
26VER=1.3
27EXCLUDE="___"
28
29function 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
42while [ $# -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";;
1e5491e9 51 -x) EXCLUDE="trc00[^ ]*";;
b3f3562f
OL
52 *) list+=" "$1;;
53 esac
54 shift
55done
56
57# list
58[ "$list" ] || list=$(awk '{sub(/#.*/, "")} /trc[0-9][0-9]/ && ! /-rf/ {print $2}' /etc/hosts)
1e5491e9 59list=$(echo $list" "|sed "s/$EXCLUDE//")
b3f3562f
OL
60
61# timeout
62if [ "$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) &
65fi
66trap "exit 0;" SIGINT
67
68# main loop
69while 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;
78done