home *** CD-ROM | disk | FTP | other *** search
- #!/bin/sh
- #----------------------------------------------------------------------
- # NOM
- # hloop = Execute une meme commande sur une liste de stations.
- #
- # SYNTAXE
- # hloop -h|H (aide)
- # hloop commande (execute la commande)
- # hloop [-q] [-s station] [-u user] [-f fichier] commande
- #
- #----------------------------------------------------------------------
- # VERSION
- # 1.1
- #
- # DESCRIPTION
- # * Cette commande permet d'executer une liste de stations une meme
- # commande passee en argument.
- # Si aucune liste n'est passe en argument la procedure prend le
- # fichier /etc/hosts ou si l'on se trouve dans un domaine NIS
- # la procedure utilise la commande ypcat.
- #
- # * Le fichier de stations doit contenir une station par ligne.
- #
- #
- # OPTION(S)
- # -h|H Aide courte ou longue.
- #
- # -f fichier Prend la liste des station inscrites dans
- # fichier.
- #
- # -u user Execute les remote commande a travers une entree
- # utilisateur.
- #
- # -s station Execute une commande uniquement sur une station.
- # Il est plus rapide faire directement un remsh
- # sur la station souhaite, mais une telle option
- # peut etre utile.
- #
- # -q Mode silence, se contente juste d'executer la
- # commande.
- #
- #
- # EXEMPLE(S)
- # * Lance la commande "users" sur la liste de station inscrites dans
- # le fichier liste :
- #
- # prompt_unix> hloop -f liste users
- #
- # * Lance la commande "bdf" sur la liste de station inscrites dans
- # le fichier file.txt avec comme entree l'utilisateur toto :
- #
- # prompt_unix> hloop -q -u toto -f file.txt bdf
- #
- # DEPENDANCE(S)
- # * remsh|rsf, domainname, awk, ping, sed, grep, ypcat.
- #
- # AUTEUR
- # Copyright (c) 1998 Bertrand LE QUELLEC, All Rigths Reserved.
- #
- # CONTACT
- # eMail : blq@mygale.org ou blq@wanadoo.fr
- # Url : http://www.mygale.org/09/blq/ ou http://perso.wanadoo.fr/blq/
- #
- # Cree : 18/11/93
- #----------------------------------------------------------------------
- # Permission to use, copy, modify, distribute, and sell this software
- # and its documentation for any purpose is hereby granted without fee,
- # provided that the above copyright notice appear in all copies and
- # that both that copyright notice and this permission notice appear in
- # supporting documentation, and that the name of Bertrand LE QUELLEC
- # not be used in advertising or publicity pertaining to distribution
- # of the software without specific, written prior permission. Bertrand
- # LE QUELLEC makes no representations about the suitability of
- # this software for any purpose. It is provided "as is" without
- # express or implied warranty.
- #
- # THIS SOFTWARE IS PROVIDED BY Bertrand LE QUELLEC `AS IS' AND ANY
- # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Bertrand LE QUELLEC BE
- # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
- # BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- #----------------------------------------------------------------------
-
- # ------------------------------------------
- # ZONE DE DEFINITION DES VARIABLES GLOBALES:
- # ------------------------------------------
- NOM_PROCEDURE=`basename $0`
- LA_PROCEDURE=$0
- CD=`pwd`
- AWK=`[ -f /usr/bin/nawk ] && echo "nawk" || echo "awk" `
-
- HOSTNAME=`hostname`
- UNAME=`uname`
- RSH=`[ "$UNAME" = "HP-UX" -o "$UNAME" = "AIX" ] && echo "remsh" || echo "rsh"`
- TEST_PING=`[ "$UNAME" = "HP-UX" -o "$UNAME" = "AIX" ] && echo "0% packet loss" || echo "alive"`
-
- ##
- ## Variables particulieres:
- ##
- DATE=`date "+%d/%m/%y"`
- MODE_COULEUR=0
-
-
- ##
- ## Les variables concernant l'utilisateur:
- ##
- DEFAUT_AUTEUR=`whoami`
- DEFAUT_EDITEUR=${EDITOR}
- DEFAUT_SHELL=${SHELL}
-
-
- #----------------------------------------------------------------------
- # -----------------------------------------
- # ZONE DE DEFINITION DES FONCTIONS LOCALES:
- # -----------------------------------------
- # La fonction doit etre precedee d'un commentaire avec en debut de ligne
- # le caractere # (diese) repete deux fois.
-
- ##
- ## En cas d'une interruption la fonction suivante est executee.
- ## Les interruptions sont des signaux envoyes au processus du script,
- ## par la commande Unix: kill(1). La "capture" de ces signaux est
- ## identifiee par la commande Shell: trap definie apres la zone des
- ## options.
- ##
- TrapInterrup()
- {
- # Vous pouvez executer des instructions avant une sortie
- # par signal (ou brutale!).
- exit 1
- }
-
-
- ##
- ## Renvoie le PATH et nom d'un ou plusieurs executables.
- ## Si le ou les executables sont introuvables la fonction ne renvoie rien.
- ## Dans le cas d'un alias la fonction ne renvoie rien, car le PATH n'est
- ## pas recuperable.
- ##
- Type()
- {
- [ $# -ne 0 ] && type $* | ${AWK} '$2=="is" && $4!="alias" {print $NF}'
- }
-
-
- ##
- ## Affiche l'aide indiquee en en-tete du script shell.
- ## Ne pas supprimer car cette fonction est utilisee a l'appel de la
- ## procedure avec les options -H ou -H
- ##
- AideProc()
- {
- fichier=$1
- niveau=$2
-
- [ -z "$fichier" ] && {
- Erreur "AideProc(): nombre d'argument incorect."
- return 0
- }
-
- [ ! -f "$fichier" ] && {
- Erreur "AideProc(): impossible d'atteindre le fichier $fichier."
- return 0
- }
-
- [ ${niveau:=2} = 2 ] && {
- ${AWK} 'BEGIN {i=2}
- /^#(#+|-+|÷+|\*+|\\+|\++|=+|\.+|\/+)+ *$/{
- if(--i<=0)exit
- next
- }
- /^#[ ]*[A-Z]*[ ]*$/{
- next
- }
- /^#[^!]/{
- print substr($0,1,80)
- }
- /^#[ \t]*$/{
- print ""
- }
- ' $fichier | sed -e 's/^#[ ]*//'
- } || {
- ${AWK} '\
- /^#(#+|-+|÷+|\*+|\\+|\++|=+|\.+|\/+)+ *$/{
- if(--i<=0) exit
- next
- }
- /^#[^!]/{
- print substr($0,2,80)
- }
- /^#[ \t]*$/{
- print ""
- }
- ' i=$niveau $fichier | pg -e
- }
- }
-
- ##
- ## Renvoie le type de la machine ou l'on se trouve et surtout le
- ## systeme d'exploitation en cours. La fonction renvoie un des numero
- ## suivant en reponse:
- ##
- ## inconue 0
- ##
- ## SunOs 1
- ## Sun Solaris 2
- ## HP s700 3
- ## HP s800 4
- ## HP s300 5
- ## Silicon Graphic 6
- ## IBM AIX 7
- ##
- ## Pour recuperer le code retour de la fonction, utilisez la variable $?
- ##
- Machine()
- {
- os=`uname`
-
- [ "$os" = "SunOS" ] && return 1
-
- [ "$os" = "HP-UX" ] && {
- if uname -a | grep "PA-RISC" 1> /dev/null
- then
- return 3
- elif uname -a | grep "HP-PA" 1> /dev/null
- return 4
- then
- return 5
- fi
- }
-
- [ "$os" = "IRIX" ] && return 6
-
- [ "$os" = "AIX" ] && return 7
-
- return 0
- }
-
-
- ##
- ## Affiche un message en rouge si l'on se trouve dans une fenetre hpterm
- ## et se met en attente de 2 secondes.
- ## Il est possible de recuperer le code retour de la fonction.
- ##
- Erreur()
- {
- [ $# -eq 0 ] && return -1
-
- echo "\n\t${ROUGE}$*"
- sleep 2
-
- return 0
- }
-
-
- #----------------------------------------------------------------------
- # ----------------------------------------------
- # DEFENITION DES VARIABLES LOCALES AU PROGRAMME:
- # ----------------------------------------------
-
- LISTE=
- USER=
- QUIET=0
-
-
- #----------------------------------------------------------------------
- # ----------------------------------------------
- # DEFENITION DES FONCTIONS LOCALES AU PROGRAMME:
- # ----------------------------------------------
-
- ## --------------------------------------------------
- ## verification que la station est Up ou Down.
- ##
- ## 0 Down
- ## 1 Up
- ##
- UpDown ()
- {
- Machine
- [ $? -eq 7 ] && {
- perc=`ping $1 -c 1 | grep -c "${TEST_PING}"'`
- } || {
- perc=`ping $1 64 -n 1 | grep -c "${TEST_PING}"'`
- }
-
- [ `echo $perc | grep -c unknown` -gt 0 ] && return 0
- [ "$perc" -eq 0 -o "$perc" = "1" ] && return 1 || return 0
- }
-
- ## --------------------------------------------------
- ## execution de la commande sur la station distante.
- ##
- hexec()
- {
- [ $QUIET -eq 1 ] || echo "Execution sur $host ..."
-
- if UpDown $host
- then
- [ `$RSH $host $USER echo 2>&1 | grep -c incorrect` -gt 0 ] && ( [ $QUIET -eq 1 ] || echo "\t>> Pas d'entree sur $host" ) || eval $RSH $host $USER $*
- else
- [ $QUIET -eq 1 ] || echo "\t>> $host est down"
- fi
-
- [ $QUIET -eq 1 ] || echo ""
- }
-
-
- #----------------------------------------------------------------------
- # -------------------------
- # RECUPERATION DES OPTIONS:
- # -------------------------
- set -- `getopt hHxf:u:s:q $*`
- for opt in $*
- do
- case $opt in
- -h) AideProc $LA_PROCEDURE ; exit 0 ;;
- -H) AideProc $LA_PROCEDURE 3 ; exit 0 ;;
-
- -x) set -x ; shift ;;
-
- -f) LISTE=$2 ; shift ; shift ;;
- -u) USER="-l $2" ; shift ; shift ;;
- -s) echo "$2" > /tmp/liste$$ ; LISTE=/tmp/liste$$
- shift ; shift ;;
- -q) QUIET=1 ; shift ;;
-
- --) shift ; break ;;
- esac
- done
-
-
- #----------------------------------------------------------------------
- # AUTRES DEFINITIONS:
- # -------------------
- trap TrapInterrup 3 2 15 # Recuperation des interruptions 3, 2 et 15
-
- #----------------------------------------------------------------------
- # DEBUT ZONE DU PROGRAMME:
- # ------------------------
-
- [ -z "$LISTE" ] && (
- if domainname > /dev/null
- then
- for host in `${AWK} '$1 !~ "#" {car=substr($1,1,1);if(car!="#")print $2}' /etc/hosts`
- do
- hexec $*
- done
- else
- for host in `ypcat hosts | ${AWK} '{print $2}'`
- do
- hexec $*
- done
- fi ) || (
- for host in `${AWK} '{print $1}' $LISTE`
- do
- hexec $*
- done
-
- [ "$LISTE" = /tmp/liste$$ ] && rm -f /tmp/liste$$ )
-