#!/bin/sh if [ -z "$BASH" ]; then printf "\nERROR: You need to be running bash." exit fi LOG=~/log/$0.log DEBUG=1 arch=`uname -s` println() { if [ $COLUMNS ]; then export TW=$COLUMNS else export TW=75 fi s="$*" test "x$#" != "x0" && s="[ $s ]" echo "$s" | perl -ne 'chomp;print "----$_"."-"x($ENV{TW}-5-length($_))."\n"'; unset s } # Log a message to a file log() { # Usage: log if [ $DEBUG ]; then printf "$*\n" else printf "$*\n" | tee -a $LOG fi } # Generate a debugging message in the log debug() { # Usage: debug if [ $DEBUG -gt 0 ]; then log "DEBUG: $*" fi } # Generate an error message and abort program executiong error() { # Usage: error log "ERROR: $*" return 1 } confirm() { # Usage: Confirm printf "$* (y/N) " read answer if [ $answer == "y" ]; then return 0 else return 1 fi } pinghost() { # Usage: pinghost case $arch in SunOS) ping $* 1 1 > /dev/null 2>&1 if [ $? -ne 0 ]; then error "$* is not responding." return 1 fi ;; Linux) ping -c 1 $* > /dev/null 2>&1 if [ $? -ne 0 ]; then error "$* is not responding." return 1 fi ;; *) error "Unknown architecture." ;; esac } } addhost() { # addhost pinghost $1 if [ $? -eq 0 ]; then targethosts="$targethosts $1" return 0 else return 1 fi } delhost() { # delhost if [ -z "$targethosts" ]; then error "No target hosts defined yet" return 1 fi targethosts=`echo $targethosts | sed "s/\<$1\>//g" | sed 's/ ?*/ /g'` } lshosts() { # lshosts if [ -z "$targethosts" ]; then error "No target hosts defined yet" return 1 fi println "Target host list" echo $targethosts } # What colo and what prefix for hosts should we use? colo() { # Usage: colo case $* in nameone) colo="nameone" nprefix="0" ;; nametwo) colo="nametwo" nprefix="1" ;; *) error "Invalid or no colo specified." ;; esac } farm() { # Usage: farm resethosts dnsserver="servername" zone="domain.com" if [ "$colo" ]; then msg=" at $colo" fi println "Connectivity check for $* farm$msg" for hn in `dig @$dnsserver $zone axfr | grep -v CNAME | grep "^$*$nprefix[0-9]" | cut -d"." -f1` do # If the hostname doesn't end with the letter e, add it to the variable if [ ! `echo $hn | grep ".*e$"` ]; then addhost $hn fi done println "Target hosts" echo $targethosts confirm "\nAre these the correct target hosts?" if [ $? -ne 0 ]; then resethosts fi } sequence() { # seqeunce resethosts println "Connectivity check for $1$2-$3" for num in `seq -f%03g $2 $3`; do hn="$1$num" addhost $hn done println "Target hosts" echo $targethosts fi } execute() { # execute