#!/bin/bash #Copyright 2000 William Stearns #GPL'd, of course. #Version 0.1, 4/6/2000 #Usage: fanout "MACHINES" "commands and parameters to run on each machine" #Examples: # fanout "wstearns@localhost localhost anotherhost.someplace.net" \ # "echo My PID is \"\$PPID\" ; sleep 15" # # fanout "localhost" "uname -a ; rpm -qa | egrep -i '(openlinux|redhat-release)' \ # ; uptime ; df -P / ; netstat -a | grep '*:*'" | less # # fanout "localhost myaccount@localhost" "uptime" >uptime-sample # #Notes: #- The accounts you connect to should have your ssh key ready and you should be #running ssh-agent ready to serve that key. #- The command(s) you execute run concurrently on each remote machine. Output #does not show up until all are done. #- Sample run is at the end of the script. MYPID="$$" TARGETS=`echo "$1" | tr ' ' '\012' | sort | uniq` #Sort and remove dupes shift #Leave just the commands and params to be executed. STARTTIME=`date` for ONETARGET in $TARGETS ; do case $ONETARGET in *@*) #user@machine form ONEUSER="-l ${ONETARGET%%@*}" ONEMACH=${ONETARGET##*@} HEADER="==== As ${ONETARGET%%@*} on $ONEMACH ====" ;; *) #just machine form ONEUSER="" ONEMACH=$ONETARGET HEADER="==== On $ONEMACH ====" ;; esac if ping -c 4 $ONEMACH >/dev/null 2>/dev/null ; then echo Starting $ONETARGET >/dev/stderr #Machine is reachable #Show machine name header, show command output, indented two spaces, save all to a temp file. ( echo $HEADER ; ssh -n $ONEUSER $ONEMACH "$*" | sed -e 's/^/ /' ; echo ) >/tmp/output.$MYPID.$! & else echo $ONETARGET unavailable >/dev/stderr #Machine not responding ( echo $HEADER ; echo "==== Machine unreachable by ping" ; echo ) >/tmp/output.$MYPID.$! & fi BACKPIDS="$BACKPIDS $!" #Remember the PID for later retrieval done wait #Until everyone's done. echo Fanout executing \"$*\" echo Start time $STARTTIME , End time `date` for ONEPID in $BACKPIDS ; do cat /tmp/output.$MYPID.$ONEPID rm -f /tmp/output.$MYPID.$ONEPID done #Sample run #[wstearns@sparrow fanout]$ ./fanout "localhost wstearns@localhost aaa.bbb.ccc" "uptime" | less #aaa.bbb.ccc unavailable #Starting localhost #Starting wstearns@localhost #Fanout executing "uptime" #Start time Fri Apr 7 00:13:07 EDT 2000 , End time Fri Apr 7 00:13:20 EDT 2000 #==== On aaa.bbb.ccc ==== #==== Machine unreachable by ping # #==== On localhost ==== # 12:13am up 3 days, 10:44, 0 users, load average: 0.17, 0.17, 0.22 # #==== As wstearns on localhost ==== # 12:13am up 3 days, 10:44, 0 users, load average: 0.15, 0.16, 0.22