MajorSophos update script

Mark Nienberg mark at TIPPINGMAR.COM
Thu Feb 27 17:53:51 GMT 2003


>From time to time someone writes in to the list and inquires about
automating the major (monthly, or at most, tri-monthly) updates of Sophos.
The attached shell script will do so for you.

It downloads the file from the Sophos website using your sophos
username and password and installs it using the script provided with
MailScanner.  It works either interactively or from cron.  It has an option to
download the file but not install it.  If run from cron, it can report its results
by e-mail and/or write to a log.

Even if you don't set it up to run from cron, it is a timesaver to use it
interactively.  It does a fair amount of error checking, so I don't see how it
could ever mess up a working installation.  It reports the version of
installed sophos before and after running.  It does this by actually running
sweep with the "-v" switch, so you can be pretty confident that the install
was a success.

If you want to give it a try, I advise the following procedure to build up
some confidence in it:

1. Edit the variables at the top of the script to suit your configuration.
2. Run with no arguments to see a usage statement.
3. Run with "-download" argument to see if it can download the file.
4. rm the temp directory created in 3. (/tmp/MajorSophos.sh.xxxxxx)
5. Run with "-install" argument to see if it can download and install.
6. Repeat all of the above from cron, if you like.

I wrote in on a RedHat 7.3 system.  It requires "which" and "mktemp", but
if you don't have those it would be simple to modify.
--
Mark W. Nienberg, SE
Tipping Mar + associates
1906 Shattuck Ave, Berkeley, CA  94704
visit our website at http://www.tippingmar.com


-------------- next part --------------
#!/bin/sh

# Shell script to download latest version of Sophos and
# (optionally) install it using the MailScanner installer.
# Developed on Red Hat Linux 7.3. rev 2-25-2003
# Mark Nienberg <imark at tippingmar.com>
# Use as you please at your own risk.

# --------Usage:--------------------------------------------------
# run the program with no options for a Usage printout

# --------Set Environment Variables-------------------------------
# Note that the values below are valid for standard installations
# of MailScanner 3.x.  They may need tweaking for 4.x.

# Set SWEEP to the location of the MailScanner wrapper for sweep.
# This is only used to report the Sophos version.  If it is wrong,
# it won't prevent the successful download or installation.
SWEEP=/usr/local/Sophos/bin/sophoswrapper

# Program to run to install Sophos after downloading it
INSTALL=/usr/local/MailScanner/bin/Sophos.install

# Username provide by Sophos
WEBUSER="myusername"

# Password provided by Sophos
WEBPASS="mypassword"

# Website to download from. It must end with "/"
WEBSITE="http://www.us.sophos.com/sophos/products/full/"

# Name of the file to download
DOWNFILE="linux.intel.libc6.tar.Z"

# The rest of the variables (6 total) are only used if run from cron
APPENDLOG="yes"                        # Append report to a log? yes or no
LOGFILE="/var/log/MajorSophos.log"     # Append report to this log file
EMAIL="yes"                            # Send report by email? yes or no
MAILPROG="/bin/mail"                   # Program used to send e-mail
MAILNAMES="mark"                       # Persons to receive e-mail

# The "install.sh" provided by Sophos, which is called by Mailscanner's
# "Sophos.install" script, uses ldconfig on Linux and FreeBSD.  But
# ldconfig may not be in cron's path.  If you list it here, we can fix
# cron's path so the installation will work. RedHat definitely needs this.
# Alternatively, you could specify it in your crontab file.
# It will be ignored for other platforms.
LDCONFIGPATH="/sbin"

# --------End Environment Variables-------------------------------


# --------Function to handle output-------------------------------
printout() {
        # we'll write to stdout if interactive, otherwise write to a file
        if [ "$LOG" = "no" ] ; then
           echo $@
        else
           echo $@ >>$TMPLOG
        fi
}

# --------Subroutine to find the current version of Sophos--------
sweepVer() {
        # find version of sweep currently installed by running it with -v
        if [ -f $SWEEP ]; then
           printout "Current Sophos version information follows:"
           printout `$SWEEP -v | egrep 'Product|Released'`
        else
           printout "Sophos is not currently installed or the SWEEP variable is wrong"
        fi
}

# --------Subroutine to download the file-------------------------
download() {
        TMPDIR=`mktemp -d -q /tmp/$BASE.XXXXXX`
        if [ $? -ne 0 ]; then
           printout "$BASE: Can't create temp directory, exiting"
           cleanUp
        fi

        cd $TMPDIR
        if [ "$LOG" = "yes" ] ; then
           wget -a$TMPLOG -t10 --progress=dot:mega --http-user=$WEBUSER --http-passwd=$WEBPASS $WEBSITE$DOWNFILE
        else
           wget -t10 --progress=dot:mega --http-user=$WEBUSER --http-passwd=$WEBPASS $WEBSITE$DOWNFILE
        fi

        if [ $? -eq 0 ]; then
           DOWNLOAD="success"
           printout "Download of $DOWNFILE succeeded"
        else
           DOWNLOAD="failure"
           printout "Download of $DOWNFILE failed"
        fi
}

# --------Subroutine to test for ldconfig-------------------------
ldconfigTest() {
     if [ "`uname -s`" = "Linux" ] || [ "`uname -s`" = "FreeBSD" ] ; then
        printout "Platform is `uname -s`"
        which ldconfig > /dev/null 2>&1
        if [ $? -ne 0 ] ; then
           printout "Cannot find ldconfig"
           printout "Current path is $PATH"
           if [ -d $LDCONFIGPATH ] ; then
              printout "adding $LDCONFIGPATH to the path"
              PATH=$PATH:$LDCONFIGPATH ; export PATH
              printout "New path is $PATH"

              # try once more
              which ldconfig > /dev/null 2>&1
              if [ $? -ne 0 ] ; then
                 printout "Still cannot find ldconfig, exiting."
                 cleanUp
              else
                 printout "Found ldconfig"
              fi
           else
              printout "LDCONFIGPATH variable is not valid, exiting."
              cleanUp
           fi
        fi
     fi
}

# --------Subroutine to end & handle log, if one was made.--------
cleanUp() {
if [ "$LOG" = "yes" ] ; then
   if [ "$EMAIL" = "yes" ] ; then
      $MAILPROG -s 'MajorSophos Update Log' $MAILNAMES<$TMPLOG
   fi
   if [ "$APPENDLOG" = "yes" ] ; then
      cat $TMPLOG>>$LOGFILE
   fi
   rm $TMPLOG
fi
exit
}

######### Execution starts here ##################################

BASE=`basename $0`

# --------See if we are running interactively or from cron--------
if tty -s ; then
   LOG="no"
else
   LOG="yes"
   TMPLOG=`mktemp -q /tmp/$BASE.XXXXXX`
   if [ $? -ne 0 ]; then
      echo "$BASE: Can't create temp log file, exiting"
      exit
   fi
   printout
   printout "$BASE $1 started `date`"
fi

# --------See how we were called----------------------------------
case "$1" in
   -download)   # download only
        sweepVer
        download
        cleanUp
        ;;

   -install)    # download and install
        sweepVer
        if [ "$LOG" = "yes" ] ; then ldconfigTest ; fi ;
        download

        if [ "$DOWNLOAD" = "success" ] ; then
           printout "Starting the MailScanner Sophos installation script"
           if [ "$LOG" = "yes" ] ; then
              $INSTALL >> $TMPLOG
           else
              $INSTALL
           fi

           sweepVer
           printout "Cleaning up installation files"
           cd /tmp
           rm -rf $TMPDIR
           printout "Done."
        else
           printout "Installation was not attempted."
        fi
        cleanUp
        ;;

   *)           #no option or an invalid option
        if [ "$LOG" = "yes" ] ; then
           printout "$BASE was not started with a valid option"
           printout "Run from command line with no options for a Usage Statement"
        else
           echo
           echo "Usage: $BASE [-install | -download]"
           echo "       -install   Download and install the latest Sophos program."
           echo "       -download  Download only, do not install."
        fi
        cleanUp
        ;;
esac


More information about the MailScanner mailing list