Skip to main content

do-cmd.sh

uralt... Heutzutage nimmt dafür lieber ansible oder ähnliches :)

#!/bin/bash
#
# 2006 Oliver Voelker <info(at)ovtec.it>
#
 
SERVERLIST=serverlist.txt
COMMAND=$*
REPORT=/tmp/report.$$
NOTIFY="scripts@magenbrot.net"
DATE=`date +%d.%m.%y-%H%M`
 
PROGDIR=`dirname $0`
cd $PROGDIR
 
if [ ! -f $SERVERLIST ]; then
  echo "$SERVERLIST nicht gefunden."
  exit 1
fi
 
if [ -z "$1" ]; then
  echo "Usage '$0 <remotecmd>'"
  echo "Executes <remotecmd> on all servers in $SERVERLIST"
  echo
  echo "-check-update           checks for updates on the servers"
  echo "-update                 installs available updates"
  echo
  echo "notifies are mailed to $NOTIFY"
  exit 0
fi
 
echo "Scriptlauf am `date +%c`" > $REPORT
echo "Scriptlauf am `date +%c` geloggt nach $REPORT"
echo "-------------------------------------------------------------------------------" >> $REPORT
 
if [ -e $SERVERLIST ]; then
  echo "-------------------------------------------------------------------------------"
  exec 5<&0
  cat $SERVERLIST | while read LINE; do
    trap 'exit 0' 2
    if [ "`echo $LINE | head -c1`" != "#" ] && [ "`echo $LINE | head -c1`" != "" ]; then
      #echo $LINE | awk -F" - " '{printf "Server: %s / Distri: %s / Updater: %s / Comment: %s\n", $1, $2, $3, $4}'
      SERVER=`echo $LINE | awk -F" - " '{print $1}'`
      DISTRI=`echo $LINE | awk -F" - " '{print $2}'`
      UPDATER=`echo $LINE | awk -F" - " '{print $3}'`
      COMMENT=`echo $LINE | awk -F" - " '{print $4}'`
      if [ -z "$SERVER" ] || [ -z "$DISTRI" ] || [ -z "$UPDATER" ] || [ -z "$COMMENT" ]; then
        echo "$SERVERLIST hat das falsche Format in Zeile $LINE"
        echo "$SERVERLIST hat das falsche Format in Zeile $LINE" >> $REPORT
        continue
      fi
      echo "Executing '$COMMAND' on $SERVER ($DISTRI / $COMMENT)..."
      echo "Executing '$COMMAND' on $SERVER ($DISTRI / $COMMENT)..." >> $REPORT
      case "$COMMAND" in
        "-check-update")
          case "$UPDATER" in
            "yum")
              ssh $SERVER "sudo su root -c 'yum check-update'" >> $REPORT 2>&1 <&5
              ;;
            "apt")
              ssh $SERVER "sudo su root -c 'apt-get -qq update'" >> $REPORT 2>&1 <&5
              ssh $SERVER "sudo su root -c 'apt-get -q -u -s upgrade'" >> $REPORT 2>&1 <&5
              ;;
            "up2date")
              ssh $SERVER "sudo su root -c 'up2date --dry-run'" >> $REPORT 2>&1 <&5
              ;;
            *)
              echo "kein gueltiger updater erkannt"
              ;;
          esac
          ;;
        "-update")
          case "$UPDATER" in
            "yum")
              ssh $SERVER "sudo su root -c 'yum -y update'" >> $REPORT 2>&1 <&5
              ;;
            "apt")
              ssh $SERVER "sudo su root -c 'apt-get -y upgrade'" >> $REPORT 2>&1 <&5
              ;;
            "up2date")
              # wenn auch Kernel-Updates mit installiert werden sollen, muss
              # up2date um den Parameter -f ergaenzt werden
              ssh $SERVER "sudo su root -c 'up2date -u'" >> $REPORT 2>&1 <&5
              ;;
            *)
              echo "kein gueltiger updater erkannt"
              ;;
          esac
          ;;
        *)
          ssh $SERVER "sudo su root -c '$COMMAND'" >> $REPORT 2>&1 <&5
          ;;
      esac
      echo "-------------------------------------------------------------------------------" >> $REPORT
    fi
  done
  exec 5<&-
fi
 
echo "-------------------------------------------------------------------------------"
echo "done... notifying $NOTIFY"
mail -s "updater-report" $NOTIFY < $REPORT
cp $REPORT `dirname $0`/report-$DATE.log
echo "Bericht gespeichert unter: `dirname $0`/report-$DATE.log"
echo "-------------------------------------------------------------------------------"
 
rm -f $REPORT