#!/bin/bash # if [ $# -lt 2 ]; then echo "Usage: $0 [sleep time]" echo "Host file contains the list of hosts to open screen session for" echo "Command is the command to execute on each screen session" echo "First time use init as Command to construct the screens" exit 1 fi # # Get a list (say server names) HOSTFILE=$1 # Create a counter for the screen windows HOSTCMD=$2 # Number of seconds to sleep if [ -z "$3" ]; then SLEEPTIME="0.2" else SLEEPTIME=$3 fi #if ! [[ "$SLEEPTIME" =~ ^[0-9]+$ ]]; then # SLEEPTIME=0 #fi #echo $SLEEPTIME #exit # Define a name for the screen session. SCRNAME='thenag' NOW=$(date +"%Y-%m-%d-%H-%M-%S") TMPFILE="/tmp/${SCRNAME}-${NOW}.txt" # if [ ! -f "$HOSTFILE" ]; then echo "File: $HOSTFILE does not exist" exit 1 fi # if [ "$HOSTCMD" == "init" ]; then echo "Initializing Screens ..." fi # if [ "$HOSTCMD" == "quit" ]; then echo "Quitting Screens ..." screen -S $SCRNAME -X quit exit fi declare sn integer sn=0 cat $HOSTFILE | while read ServerName do let sn++ echo "Processing $ServerName, Sequence $sn" case $HOSTCMD in init) # This creates a blank screen window for each instance screen -S $SCRNAME -X screen # Sleep may be required as sometimes commands get missed! sleep $SLEEPTIME screen -S $SCRNAME -p $sn -X stuff "echo $ServerName \n" ;; ssh) screen -S $SCRNAME -p $sn -X stuff "ssh ${ServerName}\n" sleep $SLEEPTIME ;; tail) screen -S $SCRNAME -p $sn -X hardcopy $TMPFILE sleep $SLEEPTIME sed -e :a -e '/^\n*$/{$d;N;};/\n$/ba' $TMPFILE tail -3 $TMPFILE rm $TMPFILE echo "--------------------------------" ;; *) screen -S $SCRNAME -p $sn -X stuff "${HOSTCMD} \n" sleep $SLEEPTIME esac done