All pastes #236619 Raw Edit

Stuff

public shellscript v1 · immutable
·published 2006-11-03 22:42 UTC
rendered paste body
#!/bin/shif [ -z "$BASH" ]; then  printf "\nERROR: You need to be running bash."  exitfiLOG=~/log/$0.logDEBUG=1arch=`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 filelog() { # Usage: log <message>  if [ $DEBUG ]; then        printf "$*\n"  else        printf "$*\n" | tee -a $LOG  fi}# Generate a debugging message in the logdebug() { # Usage: debug <message>  if [ $DEBUG -gt 0 ]; then        log "DEBUG: $*"  fi}# Generate an error message and abort program executiongerror() { # Usage: error <message>  log "ERROR: $*"  return 1}confirm() { # Usage: Confirm <message>  printf "$* (y/N) "  read answer  if [ $answer == "y" ]; then        return 0  else        return 1  fi}        pinghost() { # Usage: pinghost <hostname>  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 <hostname>  pinghost $1  if [ $? -eq 0 ]; then        targethosts="$targethosts $1"        return 0  else        return 1  fi}delhost() { # delhost <hostname>  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 <colocation abbreviation>  case $* in        nameone)          colo="nameone"          nprefix="0"          ;;        nametwo)          colo="nametwo"          nprefix="1"          ;;        *)          error "Invalid or no colo specified."          ;;  esac}farm() { # Usage: farm <farm prefix>        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 <hostname prefix> <start integer> <end integer>  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 <script>  script=$1  if [ ! -e "$script" ]; then        error "$script does not exist."        return 1  fi  if [ -z "$targethosts" ]; then        error "No target hosts defined."        return 1  fi  println "Executing $script"  for target in $targethosts  do        log "Processing $target."        sed "s/<target>/$target/g" $script > /tmp/$0.$$        sh /tmp/$0.$$  done  rm /tmp/$0.$$}