I wrote this script some time ago to automatically update the installation of Sophos AntiVirus on my Linux server. But as I’m replacing the server, I decided to switch to ClamAV and so the script is no longer any use to me. Mainly I’m posting it here in case I should ever need it again, but also I hope somebody might find it useful. No doubt it could benefit from some serious cleaning-up, so consider it public domain and feel free to fix it.
The script is designed to be run as root from cron. I used to run it daily, but you could run it hourly if you want more frequent updates. It will handle IDE updates as well as full application updates. It logs it’s actions to /var/log/sophos.log and sends mail to the e-mail address you specify if it fails to update for some reason.
#
# Script to automate the download and installation of Sophos AntiVirus
#
#
# Init some variables
#
INSTSRCDIR=”http://downloads.sophos.com/dp/full”;
IDESRCDIR=”http://www.sophos.com/downloads/ide”;
INSTFILENAME=”linux.intel.libc6.tar.Z”;
EMAIL=”admin@mydomain.co.uk”;
IDEFILENAME=”web_ides.zip”;
LOGFILE=”/var/log/sophos.log”;
NICEDATE=`date +%d/%m/%Y`;
MAILTMP=”/tmp/.mail”;
http_proxy=”";
export http_proxy
#
# Setup the log file descriptor
#
exec 4>>$LOGFILE
#
# This is a function to log the end of the update and send
# an error e-mail to the administrator.
# This is used when an error occurs.
#
# Called By: scriptdie
#
scriptdie() {
echo “`date +%a\ %b\ %e\ %H:%M:%S\ %Y` *** Update Finished ***” >&4
echo “This is an automatically generated message from $HOSTNAME.” >$MAILTMP
echo “The antivirus update on $NICEDATE has generated errors. The relevant part of the log is attached below.” >> $MAILTMP
echo ” ” >> $MAILTMP
echo “—” >> $MAILTMP
tail $LOGFILE >> $MAILTMP
echo “—” >> $MAILTMP
mail $EMAIL -s “Sophos update has generated errors!” < $MAILTMP
rm -f $MAILTMP
exit 1
}
#
# A function to cleanup the installation directory
# after the install – or when the install fails.
#
# Called By: cleanup
#
cleanup() {
cd /tmp
rm -rf sav-install
rm -f web_ides.zip
}
#
# Check if we’re running as root.
#
if [ "`whoami`" != "root" ]; then
echo “This script must be run as root. Exiting…”;
scriptdie
fi
#
# Log the fact that the update has started.
#
echo “`date +%a\ %b\ %e\ %H:%M:%S\ %Y` *** Update Started ***” >&4
#
# Change to the temporary directory to which everything
# will be downloaded.
#
cd /tmp
#
# Record the installed version of Sophos
#
if [ -e /tmp/$INSTFILENAME ]; then
FILESTAMP=`ls -al /tmp/$INSTFILENAME | awk ‘{print $6$7$8}’`;
fi
#
# Download the file, but only if it is newer than the installed version.
# Then check if it downloaded correctly.
#
wget $INSTSRCDIR/$INSTFILENAME –cache=off -q -N
if [ $? -eq 0 ]; then
#
# Check if we actually got a newer version. If we did, then install it.
#
if [ ! "$FILESTAMP" = "`ls -al /tmp/$INSTFILENAME | awk '{print $6$7$8}'`" ]; then
echo “`date +%a\ %b\ %e\ %H:%M:%S\ %Y` A newer version is available!” >&4
echo “`date +%a\ %b\ %e\ %H:%M:%S\ %Y` The new version was successfully downloaded.” >&4
tar -xvzf $INSTFILENAME > /dev/null
cd sav-install
./install.sh > /dev/null
if [ ! $? -eq 0 ]; then
echo “`date +%a\ %b\ %e\ %H:%M:%S\ %Y` The installation has failed.” >&4
rm -f /tmp/$INSTFILENAME
cleanup
scriptdie
else
echo “`date +%a\ %b\ %e\ %H:%M:%S\ %Y` The installation has completed successfully.” >&4
fi
cleanup
else
echo “`date +%a\ %b\ %e\ %H:%M:%S\ %Y` The most recent version is already installed.” >&4
fi
else
echo “`date +%a\ %b\ %e\ %H:%M:%S\ %Y` The file failed to download!” >&4
scriptdie
fi
#
# Now that the installation has successfully completed,
# we download the latest set of IDE updates for Sophos.
#
wget $IDESRCDIR/$IDEFILENAME –cache=off -q
if [ $? -eq 0 ]; then
rm -f /usr/local/sav/*.ide
unzip -o web_ides.zip -d /usr/local/sav/ > /dev/null
if [ $? -eq 0 ]; then
echo “`date +%a\ %b\ %e\ %H:%M:%S\ %Y` IDE Update Successful.” >&4
else
echo “`date +%a\ %b\ %e\ %H:%M:%S\ %Y` IDE Update Failed. File did not extract.” >&4
cleanup
scriptdie
fi
cleanup
else
echo “`date +%a\ %b\ %e\ %H:%M:%S\ %Y` Couldn’t download the IDE updates.” >&4
cleanup
scriptdie
fi
#
# Log the fact that the update has finished.
#
echo “`date +%a\ %b\ %e\ %H:%M:%S\ %Y` *** Update Finished ***” >&4



