Obsolete Releases > SME 8.x Contribs

bash routine to update clamd virus definitions

<< < (3/4) > >>

purvis:
clamcvdget1

--- Code: ---#!/bin/bash

# clamcvdget
# routine created on 03-21-2013 22:00:00
# this routine will update the clamav virus signature datafiles

######################################## START OF VARIABLES
# SET onlyoneinstance TO 1 TO FORCE ONLY ONE INSTANCE OF THIS
# ROUTINE TO RUN AT A TIME
    onlyoneinstance=1

# SET skipdownload TO 1 WILL SKIP DOWNLOADING OF NEW CVD FILES
# IF FILES NEED TO BE DOWNLOADED THEY WILL BE AUTOMATICALLY
# FOR TESTING PURPOSES
    skipdownload=0

# SET dovirustest TO 1 WILL CAUSE A SHORT CLAMSACN VIRUS
#   TEST TO DISPLAY SUMMARY AND KNOWN VIRUS COUNT
# USUALLY THIS IS ALWAYS SET TO 0 IN A PRODUCTON EVIRONMENT
    doclamtest=0

# SET showversion TO 1 TO DISPLAY VERSION OF CLAMD AND CLAMSCAN
    showversion=1

# SET outputmsg TO 1 TO DISPLAY MESSAGES IN ROUTINE
# IF  outputerr IS SET TO 1 THEN outputmsg WILL BE SET TO 1
    outputmsg=1

# SET outputerr TO 1 TO DISPLAY ERROR MESSAGES IN ROUTINE
# IF YOU outputerr to 1  THEN outputmsg WILL BE SET TO 1
    outputerr=1

# SET quiet TO 1 TO NOT DISPLAY ANY MESSAGES AND DO NO TEST
    quiet=0

# SET runonlyattime FOR THE ROUTINE TO WAIT FOR A SPECFIC TIME
# IF YOU SET runonlyattime TO 1 YOU MUST SET runathourminsec
#    AND SET onlyoneinstance TO 1 DURING PRODUCTION USE
    runonlyattime=0

# SET runathourminsec TO "HH:MM:SS" FOR THE TIME OF TODAY
# THE TIME OF DAY IS ONLY FOR TODAY AND NO OTHER DATES
# THE ROUTINE HAS TO START BEFORE THIS TIME OF THE DAY
# IF THE TIME HAS PASSED THEN THIS PROCESS WILL ABORT
# runathoursminsec VARIABLE WILL NOT BE CHECKED FOR CORRECTNESS
    runathourminsec="03:30:00"

# SET stopclamdsevice TO 1 WILL STOP THE CLAMAV SERVICE WHILE UPDATING
# IT IS NOT NECESSARY TO STOP THE CLAMAV SERVICE AND STOPPING
# COULD POSSIBLY CAUSE CONFLICT PROBLEMS WITH PROGRAMS NEEDING CLAMAV
# IT IS RECOMMENDED TO LEAVE THIS AT 0
    stopclamdservice=0

# THIS PROGRAM WILL TRY TO SEE IF CLAMAV IS BEING USED BY OTHER
#   ROUTINES.  SETTING THESE TO A LOWER NUMBER ON A HEAVILY USED
#   SERVER THAT MAKES CONSTAT USE OF THE CLAMAV SERVICE MAYBE NECESSARY
# SET checkclamactiveseconds TO SECONDS TO CHECK FOR ACTIVE
#    CLAMDSCAN OR CLAMD SCAN RUNNING 30 IS GOOD
    checkclamactiveseconds=30

# number of seconds between failures trying to download cvd files
    secondsbetweendownloadtries=90

#######################

####################### THESE VARIABLES MUST BE SET CORRECTLY
  downloadurl="db.us.clamav.net"
  file1="main.cvd"
  file2="daily.cvd"
  file3="bytecode.cvd"
  clamdir="/var/clamav"
  downloaddirectory=""
  downloaddirectory1='/var/clamav/download/1'
  downloaddirectory2='/var/clamav/download/2'
################################################ END OF VARIABLES



############################### begin of functions

#   function to display help
function displayhelp {
  if [ $1 != "-q" ]
     then
     echo "NAME: $routinename"
     echo "  Updates Clamav cvd signature files from the internet"
     echo "options:"
     echo "   -q   quiet"
     exit 0
   fi
}


#   FUNCTION TO DISPLAY MESSAGES WITH DATE
function displaywdate {
   if [ $outputmsg == 1 ]
      then
      TODAY=$(date +"%Y%m%d %T")
      echo "$TODAY $1"
   fi
}

function displaywdateerr {
   if [ $outputerr == 1 ]
      then
      TODAY=$(date +"%Y%m%d %T")
      echo "$TODAY $1"
   fi
}

#   FUNCTION TO DISPAY MESSAGES WITHOUT DATE
function display {
  if [ $outputmsg == 1 ];then echo "$1";fi
}

function displayerr {
  if [ $outputerr == 1 ];then echo "$1" >&2;fi
}



#   FUNCTION TO COMPARE DOWNLOADED CVD FILES
function comparecvdfiles {
result="0"
if ! diff \
   "$downloaddirectory1/$file1" \
   "$downloaddirectory2/$file1" > /dev/null
   then
   return 0
fi
if ! diff \
   "$downloaddirectory1/$file2" \
   "$downloaddirectory2/$file2" > /dev/null
   then
   return 0
fi
if ! diff \
   "$downloaddirectory1/$file2" \
   "$downloaddirectory2/$file2" > /dev/null
   then
   return 0
fi
result="1"
}

# FUNCTION TO DOWNLOAD CVD FILES
function downloadcvdfiles {
   result="0"
   cd /tmp
   currentdir=$PWD
   if [ $currentdir != "/tmp" ]
      then
      return 0
   fi
   if [ $downloaddirectory == "" ] | [ $downloaddirectory == "/" ]
      then
      return 0
   fi
   mkdir -p  $downloaddirectory
   chmod 755 $downloaddirectory
   rm -f $downloaddirectory/* > /dev/null
   cd $downloaddirectory
   currentdir=$PWD
   if [ $currentdir == "/tmp" ] | \
      [ $currentdir ==  "/" ]  |  \
      [ $currentdir  == "/root" ]
      then
      return 0
   fi
   displaywdate "download url site is $downloadurl"
   displaywdate "downloading cvd files in $downloaddirectory"
   wget -q $downloadurl/$file3
   wget -q $downloadurl/$file1
   wget -q $downloadurl/$file2
   ### displaywdate "done downloading in $downloaddirectory"
   cd /tmp
   result="1"
}

#FUNCITON TO CHECK FOR EXISTENCE OF CVD FILES
function  dodownloadfilesexist {
   result="0"
   if [ ! -f "$downloaddirectory/$file1" ] | \
      [ ! -f "$downloaddirectory/$file2" ] | \
      [ ! -f "$downloaddirectory/$file3" ]
      then
      return 0
      else
      result="1"
   fi
}
################################## end of functions

routinename=$(basename $(readlink -nf $0))

if [ $onlyoneinstance == 1 ]
   then
   processname=$(basename $(readlink -nf $0))   
   if [ $(pidof -x $processname | wc -w) -gt 2 ]
      then
      exit 0
   fi
fi

/usr/bin/renice 20 -p $$ > /dev/null
if [ ! -z $1 ]; then
   displayhelp $1
fi

if [ "$1" == "-q" ];then quiet=1;fi
if [ $outputerr == 1 ];then  outputmsg=1;fi
if [ $quiet == 1 ]
  then
  doclamtest=0
  showversion=0
  outputmsg=0
  outputerr=0
  quiet=1
fi

displaywdate "$routinename bash routine started"

if [ $runonlyattime == 1 ]
   then
   current_epoch=$(date +%s)
   target_epoch=$(date -d $runathourminsec +%s)
   sleep_seconds=$(($target_epoch - $current_epoch))
   displaywdateerr "This routine is set to run at $(date -d @$target_epoch +"%m-%d-%Y %T")"

   if [ $sleep_seconds -lt 0 ]
      then
      displaywdate "That time has already past."
      displaywdate "Aborting $routinename bash routine"
      exit 0
      else
      displaywdate "Waiting on time before continuing"
      sleep $sleep_seconds
      displaywdate "$routinename bash routine continuing now"
   fi
fi


# DOWNLOAD FIRST SET OF CVD FILES IF NOT EXIST
downloaddirectory=$downloaddirectory1
if [ $skipdownload == 0 ]
   then
   downloadcvdfiles
fi
dodownloadfilesexist
if [ $result == "0" ]
   then
   displaywdate "cvd files missing in $downloaddirectory"
   displaywdate "will try to download in $secondsbetweendownloadtries seconds"
   sleep $secondsbetweendownloadtries
   downloadcvdfiles
fi
dodownloadfilesexist
if [ $result == "0" ]
   then
   displaywdate "cvd files missing in $downloaddirectory"
   displaywdate "will try to download in $secondsbetweendownloadtries seconds"
   sleep $secondsbetweendownloadtries
   downloadcvdfiles
fi
dodownloadfilesexist
if [ $result == "0" ]
   then
   displaywdate "cvd files missing in $downloaddirectory"
   displaywdate "will try to download in $secondsbetweendownloadtries seconds"
   sleep $secondsbetweendownloadtries 
   downloadcvdfiles
fi
dodownloadfilesexist
if [ $result == "0" ]
   then
   displaywdateerr "routine $routinename"
   displaywdateerr "Update cvd files do not exist"
   displaywdateerr "in the directory of $downloaddirectory."
   displaywdateerr  "Cannot update Clamav. Aborting update routine."
   exit 0
fi

# DOWNLOAD SECOND SET OF CVD FILES IF NOT EXIST
downloaddirectory=$downloaddirectory2
dodownloadfilesexist
if [ $result == "0" ]
   then
   downloadcvdfiles
fi
dodownloadfilesexist
if [ $result == "0" ]
   then
   displaywdate "cvd files missing in $downloaddirectory"
   displaywdate "will try to download in $secondsbetweendownloadtries seconds"
   sleep $secondsbetweendownloadtries
   downloadcvdfiles
fi
dodownloadfilesexist
if [ $result == "0" ]
   then
   displaywdate "cvd files missing in $downloaddirectory"
   displaywdate "will try to download in $secondsbetweendownloadtries seconds"
   sleep $secondsbetweendownloadtries
   downloadcvdfiles
fi
if [ $result == "0" ]
   then
   if [ $outputerr == 1 ]
      then
      displaywdateerr "routine $routinename" 
      displaywdateerr "Update cvd files do not exist"
      displaywdateerr "  in the directory of $downloaddirectory."
      displaywdateerr "Cannot update Clamav. Aborting update routine."
   fi
   exit 0
fi


comparecvdfiles
if [ $result == "0" ]
   then
   downloaddirectory=$downloaddirectory2
   dodownloadfilesexist
  if [ $result == "0" ]
      then
      downloadcvdfiles
   fi
  if [ $result == "0" ]
      then
      displaywdateerr "cvd files missing in $downloaddirectory"
      displaywdateerr "will try to download in $secondsbetweendownloadtries seconds"
      sleep $secondsbetweendownloadtries
      downloadcvdfiles
   fi
   dodownloadfilesexist
   if [ $result == "0" ]
      then
      displaywdateerr "cvd files missing in $downloaddirectory"
      displaywdateerr "will try to download in $secondsbetweendownloadtries seconds"
      sleep $secondsbetweendownloadtries
      downloadcvdfiles
   fi
   dodownloadfilesexist
   if [ $result == "0" ]
      then
      if [ $outputerr == 1 ]
         then
         displaywdateerr "routine $routinename"
         displaywdateerr "Update cvd files do not exist"
         displaywdateerr "  in the directory of $downloaddirectory."
         displaywdateerr "Cannot update Clamav. Aborting update routine."
       fi
      exit 0
   fi
fi

# COMPARE THE TWO SETS OF CVD FILES
# IF THE FILES ARE NOT EQUAL THEN DOWNLOAD THE SECOND SET
comparecvdfiles
if [ $result == "0" ]
   then
   downloaddirectory=$downloaddirectory2
   downloadcvdfiles
   dodownloadfilesexist
  if [ $result == "0" ]
      then
      displaywdateerr "cvd files missing in $downloaddirectory"
      displaywdateerr "will try to download in $secondsbetweendownloadtries seconds"
      sleep $secondsbetweendownloadtries
      downloadcvdfiles
   fi
  if [ $result == "0" ]
      then
      displaywdateerr "cvd files missing in $downloaddirectory"
      displaywdateerr "will try to download in $secondsbetweendownloadtries seconds"
      sleep $secondsbetweendownloadtries
      downloadcvdfiles
   fi
   dodownloadfilesexist
   if [ $result == "0" ]
      then
      displaywdateerr "cvd files missing in $downloaddirectory"
      displaywdateerr "will try to download in $secondsbetweendownloadtries seconds"
      sleep $secondsbetweendownloadtries
      downloadcvdfiles
   fi
   dodownloadfilesexist
   if [ $result == "0" ]
      then
      if [ $outputerr == 1 ]
         then
         displaywdateerr "routine $routinename"
         displaywdateerr "Update cvd files do not exist"
         displaywdateerr "  in the directory of $downloaddirectory."
         dispalywdateerr "Cannot update Clamav. Aborting update routine."
      fi
   exit 0
   fi
fi

# COMPARE THE TWO SETS OF CVD FILES FOR A SECOND AND FINAL TRY
# IF THE TWO SETS ARE NOT EQUAL THEN THE PROGRAM ABORTS
comparecvdfiles
if [ $result == "0" ]
then
if [ $outputerr == 1 ]
      then
      displaywdateerr "Update cvd files do not exist"
      displaywdateerr "Update cvd files are not complete"
      displaywdateerr "Cannot update ClamAV"
   fi
exit 0
fi




# UPDATING THE CLAMAV SIGNATURE CVD FILES

# MAKE AN EFFORT TO CHECK IF CLAMSCAN IS RUNNING
# TEST TO MAKE SURE CLAMSCAN IS NOT RUNNING
#   FOR 15 SECONDS FIRST THEN IF CLAMSCAN IS
#   RUNNING TEST ONCE EVERY 15 SECONDS THERE AFTER
# WHEN CLAMSCAN HAS NOT BEEN RUNNING FOR 1
#   MINUTE THEN PROCEED TO UPDATE CLAMAV

displaywdate "Verifying or waiting on freed up Clamav processes"

let counter=$checkclamactiveseconds*4
until [ $counter -lt 1 ]; do
      if [ $(pidof -x "clamscan" | wc -w) -gt 0 ]  || \
         [ $(pidof -x "clamdscan" | wc -w) -gt 0 ] || \
         [ $(pidof -x "freshclam" | wc -w) -gt 1 ]
         then
         let counter=$checkclamactiveseconds*4
         sleep 2
         else
         let counter-=1
         sleep .25
      fi
done

#STOPPING THE FRESHCLAM SERVICE
displaywdate "stopping freshclam service"
service freshclam stop > /dev/null
counter=1
until [ $counter -lt 1 ]; do
      if [ $(pidof -x "freshclam" | wc -w) -gt 0 ]
         then
         sleep 2
         else
         displaywdate "freshclam service stopped"
         counter=0
      fi
done


#STOPPING THE CLAMD SERVICE
if [ $stopclamdservice == 1 ]
   then
   displaywdate "stopping clamd service"

service clamd stop > /dev/null
counter=1
until [ $counter -lt 1 ]; do
      if [ $(pidof -x "clamd" | wc -w) -gt 0 ]
         then
         sleep 2
         else
         displaywdate "clamd service stopped"
          counter=0
      fi
done
fi

#COPYING THE DOWNLOADED CLAMAV CVD FILES TO
#THE CLAMAV CVD WORKING DIRECTORY
displaywdate "updating the Clamav cvd files"
cd $clamdir
rm  -f $clamdir/*.cvd 1> /dev/null 2> /dev/null
rm  -f $clamdir/*.cld 1> /dev/null 2> /dev/null
rm  -f $clamdir/mirrors.dat 1> /dev/null 2> /dev/null
cp -p $downloaddirectory1/*.cvd $clamdir 1> /dev/null 2> /dev/null


#STARTING THE CLAMD SERVICE
if [ $stopclamdservice == 1 ]
   then
   displaywdate "starting clamd service"
   service clamd start > /dev/null
   counter=1
   until [ $counter -lt 1 ]; do
      if [ $(pidof -x "clamd" | wc -w) -gt 1 ]
         then
         sleep .5
         else
         counter=0
         displaywdate "clamd service started"
      fi
done
fi

#RELOADING THE CLAMAV VIRUS DEFINITION FILES IN THE CLAMD SERVICE

/usr/bin/clamdscan $downloaddirectory1/$file3 \
   --no-summary --infected --reload 1>/devnull 2>/dev/null


#STARTING THE FRESHCLAM SERVICE
displaywdate "starting freshclam service"
service freshclam start > /dev/null
counter=1
until [ $counter -lt 1 ]; do
      if [ $(pidof -x "freshclam" | wc -w) -gt 1 ]
         then
         sleep .5
         else
         counter=0
         displaywdate "freshclam service started"
      fi
done

displaywdate "updated Clamav"

# THE SHOW THE VERSION OF CLAMAV
if [ $showversion == 1 ]
   then
   tempstring=$(clamd -V)
   displaywdate   "ClamAV version - $tempstring"
fi

# THE FOLLOWING IS TO JUST RUN A SHORT TEST OF CLAMSCAN
if [ $doclamtest == 1 ] 
   then
   until [ $counter -lt 1 ]; do
      if [ $(pidof -x "clamscan" | wc -w) -gt 0 ]
         then
         counter=60
         sleep 2
         else
         let counter-=1
         sleep .25
      fi
    done
    echo
    echo "Running a short clamscan test on an empty directory."
    echo "No files or viruses should be found in the summary."
    mkdir -p $clamdir/temp
    clamscan $clamdir/temp
    echo
    rm -rf $clamdir/temp
fi

displaywdate "$routinename bash routine ended successfully"
exit 1


--- End code ---

purvis:
clamcvdget2

--- Code: ---#!/bin/bash

# clamcvdget
# routine created on 04-02-2013 12:10:00
# this routine will update the clamav virus signature datafiles

######################################## START OF VARIABLES
# SET onlyoneinstance TO 1 TO FORCE ONLY ONE INSTANCE OF THIS
# ROUTINE TO RUN AT A TIME
    onlyoneinstance=1

# SET skipdownload TO 1 WILL SKIP DOWNLOADING OF NEW CVD FILES
# IF FILES NEED TO BE DOWNLOADED THEY WILL BE AUTOMATICALLY
# FOR TESTING PURPOSES
    skipdownload=0

# SET dovirustest TO 1 WILL CAUSE A SHORT CLAMSACN VIRUS
#   TEST TO DISPLAY SUMMARY AND KNOWN VIRUS COUNT
# USUALLY THIS IS ALWAYS SET TO 0 IN A PRODUCTON EVIRONMENT
    doclamtest=0

# SET showversion TO 1 TO DISPLAY VERSION OF CLAMD AND CLAMSCAN
    showversion=1

# SET outputmsg TO 1 TO DISPLAY MESSAGES IN ROUTINE
# IF  outputerr IS SET TO 1 THEN outputmsg WILL BE SET TO 1
    outputmsg=1

# SET outputerr TO 1 TO DISPLAY ERROR MESSAGES IN ROUTINE
# IF YOU outputerr to 1  THEN outputmsg WILL BE SET TO 1
    outputerr=1

# SET quiet TO 1 TO NOT DISPLAY ANY MESSAGES AND DO NO TEST
    quiet=0

# SET runonlyattime FOR THE ROUTINE TO WAIT FOR A SPECFIC TIME
# IF YOU SET runonlyattime TO 1 YOU MUST SET runathourminsec
#    AND SET onlyoneinstance TO 1 DURING PRODUCTION USE
    runonlyattime=0

# SET runathourminsec TO "HH:MM:SS" FOR THE TIME OF TODAY
# THE TIME OF DAY IS ONLY FOR TODAY AND NO OTHER DATES
# THE ROUTINE HAS TO START BEFORE THIS TIME OF THE DAY
# IF THE TIME HAS PASSED THEN THIS PROCESS WILL ABORT
# runathoursminsec VARIABLE WILL NOT BE CHECKED FOR CORRECTNESS
    runathourminsec="03:30:00"

# SET stopclamdsevice TO 1 WILL STOP THE CLAMAV SERVICE WHILE UPDATING
# IT IS NOT NECESSARY TO STOP THE CLAMAV SERVICE AND STOPPING
# COULD POSSIBLY CAUSE CONFLICT PROBLEMS WITH PROGRAMS NEEDING CLAMAV
# IT IS RECOMMENDED TO LEAVE THIS AT 0
    stopclamdservice=0

# THIS PROGRAM WILL TRY TO SEE IF CLAMAV IS BEING USED BY OTHER
#   ROUTINES.  SETTING THESE TO A LOWER NUMBER ON A HEAVILY USED
#   SERVER THAT MAKES CONSTAT USE OF THE CLAMAV SERVICE MAYBE NECESSARY
# SET checkclamactiveseconds TO SECONDS TO CHECK FOR ACTIVE
#    CLAMDSCAN OR CLAMD SCAN RUNNING 30 IS GOOD
    checkclamactiveseconds=30

# number of seconds between failures trying to download cvd files
    secondsbetweendownloadtries=60

#######################

####################### THESE VARIABLES MUST BE SET CORRECTLY
### set the locations where the cvd files will be downloaded from
### local can be substutied can be any of these abbeviations found at
### this webstite http://www.clamav.net/mirrors.html
### here are the current local abbreivations as of the date at the top
### at au ba be br by ca ch cn cz de dk ee es fr gl gr hk hu id
### ie in it jp lt lv mt nl no pl pt ro ru se sg si sk th tr tw ua uk us za
### there needs to at least 1 location
### the number of locations are unlimited and will be used in reverse order
  declare -a downloadsite=(  \
     db.us.clamav.net        \
     db.local.clamav.net     \
     db.us.clamav.net        \
     db.local.clamav.net     \
     db.us.clamav.net        \
     db.local.clamav.net     \
     db.us.clamav.net        \
     db.xx.clamav.net        \
     )

  file1="main.cvd"
  file2="daily.cvd"
  file3="bytecode.cvd"
  clamdir="/var/clamav"
  downloaddirectory=""
  downloaddirectory='/var/clamav/download'
################################################ END OF VARIABLES



############################### begin of functions

#   function to display help
function displayhelp {
  if [ $1 != "-q" ]
     then
     echo "NAME: $routinename"
     echo "  Updates Clamav cvd signature files from the internet"
     echo "options:"
     echo "   -q   quiet"
     exit 0
   fi
}


#   FUNCTION TO DISPLAY MESSAGES WITH DATE
function displaywdate {
   local TODAY=""
   if [ $outputmsg == 1 ]
      then
      TODAY=$(date +"%Y%m%d %T")
      echo "$TODAY $1"
   fi
}

function displaywdateerr {
   local TODAY=""
   if [ $outputerr == 1 ]
      then
      TODAY=$(date +"%Y%m%d %T")
      echo "$TODAY $1"
   fi
}

#   FUNCTION TO DISPAY MESSAGES WITHOUT DATE
function display {
  if [ $outputmsg == 1 ];then echo "$1";fi
}

function displayerr {
  if [ $outputerr == 1 ];then echo "$1" >&2;fi
}

# function to test cvd files downloaded
function testcvdfiles {
local strikeout=0
local tempcount=""
result="0"

if [ ! -f $downloaddirectory/$file1 ];then let strikeout+=1;fi
if [ -f $downloaddirectory/$file1 ]
   then
   tempstring=""
   tempstring=$(sigtool --info=$downloaddirectory/$file1)
   sleep 2
   if [ -z "$tempstring" ]
      then
      let strikeout+=1
      else
      tempcount=""
      tempcount==$(echo $tempstring |grep -P -i -c "verification\s+ok")
      if [ "$tempcount" == "=0" ] || [ "$tempcount" == "" ]
         then
         let strikeout+=1
      fi
   fi
fi
sleep 1

if [ ! -f $downloaddirectory/$file2 ];then let strikeout+=1;fi
if [ -f $downloaddirectory/$file2 ]
   then
   tempstring=""
   tempstring=$(sigtool --info=$downloaddirectory/$file2)
   sleep 2
   if [ -z "$tempstring" ]
      then
      let strikeout+=1
      else
      tempcount=""
      tempcount==$(echo $tempstring |grep -P -i -c "verification\s+ok")
      if [ "$tempcount" == "=0" ] || [ "$tempcount" == "" ]
         then
         let strikeout+=1
      fi
   fi
fi

if [ ! -f $downloaddirectory/$file3 ];then let strikeout+=1;fi
if [ -f $downloaddirectory/$file3 ]
   then
   tempstring=""
   tempstring=$(sigtool --info=$downloaddirectory/$file3)
   sleep 2
   if [ -z "$tempstring" ]
      then
      let strikeout+=1
      else
      tempcount=""
      tempcount==$(echo $tempstring |grep -P -i -c "verification\s+ok")
      if [ "$tempcount" == "=0" ] || [ "$tempcount" == "" ]
         then
         let strikeout+=1
      fi
   fi
fi

if [ $strikeout -eq 0 ]
   then
   result="1"
fi
}



# FUNCTION TO DOWNLOAD CVD FILES
function downloadcvdfiles {
   result="0"
   cd /tmp
   currentdir=$PWD
   if [ $currentdir != "/tmp" ]
      then
      return 0
   fi
   if [ $downloaddirectory == "" ] | [ $downloaddirectory == "/" ]
      then
      return 0
   fi
   mkdir -p  $downloaddirectory
   chmod 755 $downloaddirectory
   rm -f $downloaddirectory/$file1 2> /dev/null
   rm -f $downloaddirectory/$file2 2> /dev/null
   rm -f $downloaddirectory/$file3 2> /dev/null
   rm -f $downloaddirectory/$file1* 2> /dev/null
   rm -f $downloaddirectory/$file2* 2> /dev/null
   rm -f $downloaddirectory/$file3* 2> /dev/null

 
 cd $downloaddirectory
   currentdir=$PWD
   if [ $currentdir == "/tmp" ] | \
      [ $currentdir ==  "/" ]  |  \
      [ $currentdir  == "/root" ]
      then
      return 0
   fi
   displaywdate "download url site is $downloadurl"
   displaywdate "downloading cvd files in $downloaddirectory"
   wget -q $downloadurl/$file3
   if [ ! -f "$downloaddirectory/$file3" ]
      then
      displaywdate "download failed for file $file3"
      displaywdate "aborting this download instance"
      return 0
   fi
   wget -q $downloadurl/$file1
   if [ ! -f "$downloaddirectory/$file1" ]
      then
      displaywdate "download failed for file $file1"
      displaywdate "aborting this download instance"
      return 0
   fi
   wget -q $downloadurl/$file2
     if [ ! -f "$downloaddirectory/$file2" ]
      then
      displaywdate "download failed for file $file2"
      displaywdate "aborting this download instance"
      return 0
   fi
   ### displaywdate "done downloading in $downloaddirectory"
   cd /tmp
   result="1"
}

#FUNCITON TO CHECK FOR EXISTENCE OF CVD FILES
function  dodownloadfilesexist {
   result="0"
   if [ ! -f "$downloaddirectory/$file1" ] | \
      [ ! -f "$downloaddirectory/$file2" ] | \
      [ ! -f "$downloaddirectory/$file3" ]
      then
      return 0
      else
      result="1"
   fi
}
################################## end of functions

routinename=$(basename $(readlink -nf $0))

if [ $onlyoneinstance == 1 ]
   then
   processname=$(basename $(readlink -nf $0))   
   if [ $(pidof -x $processname | wc -w) -gt 2 ]
      then
      exit 0
   fi
fi

/usr/bin/renice 20 -p $$ > /dev/null
if [ ! -z $1 ]; then
   displayhelp $1
fi

if [ "$1" == "-q" ];then quiet=1;fi
if [ $outputerr == 1 ];then  outputmsg=1;fi
if [ $quiet == 1 ]
  then
  doclamtest=0
  showversion=0
  outputmsg=0
  outputerr=0
  quiet=1
fi

displaywdate "$routinename bash routine started"

if [ $runonlyattime == 1 ]
   then
   current_epoch=$(date +%s)
   target_epoch=$(date -d $runathourminsec +%s)
   sleep_seconds=$(($target_epoch - $current_epoch))
   displaywdateerr "This routine is set to run at $(date -d @$target_epoch +"%m-%d-%Y %T")"

   if [ $sleep_seconds -lt 0 ]
      then
      displaywdate "That time has already past."
      displaywdate "Aborting $routinename bash routine"
      exit 0
      else
      displaywdate "Waiting on time before continuing"
      sleep $sleep_seconds
      displaywdate "$routinename bash routine continuing now"
   fi
fi




cvdfilesgood=0
counter=${#downloadsite[@]}
let counter-=1
until [ $counter -lt 0 ]; do
     downloadurl=${downloadsite[$counter]}
     downloadcvdfiles
     dodownloadfilesexist
     if [ $result == "0" ]
        then
        displaywdateerr "cvd files missing in $downloaddirectory"
        displaywdateerr "will retry download in $secondsbetweendownloadtries seconds"
        sleep $secondsbetweendownloadtries
        let counter-=1
     else
        testcvdfiles
        if [ $result == "1" ]
           then
           let counter=-1
           cvdfilesgood=1
        fi
     fi
done

dodownloadfilesexist
   if [ $result == "0" ]
    then
    displaywdateerr "routine $routinename"
    displaywdateerr "Update cvd files do not exist"
    displaywdateerr "in the directory of $downloaddirectory."
    displaywdateerr  "Cannot update Clamav. Aborting update routine."
    exit 0
  fi

if [ $cvdfilesgood -eq 0 ]
   then
   displaywdateerr "routine $routinename"
   displaywdateerr "Cvd files do not verify proper"
   displaywdateerr "in the directory of $downloaddirectory."
   displaywdateerr  "Cannot update Clamav. Aborting update routine."
   exit 0
fi


# UPDATING THE CLAMAV SIGNATURE CVD FILES

# MAKE AN EFFORT TO CHECK IF CLAMSCAN IS RUNNING
# TEST TO MAKE SURE CLAMSCAN IS NOT RUNNING
#   FOR 15 SECONDS FIRST THEN IF CLAMSCAN IS
#   RUNNING TEST ONCE EVERY 15 SECONDS THERE AFTER
# WHEN CLAMSCAN HAS NOT BEEN RUNNING FOR 1
#   MINUTE THEN PROCEED TO UPDATE CLAMAV

displaywdate "Verifying or waiting on freed up Clamav processes"

let counter=$checkclamactiveseconds*4
until [ $counter -lt 1 ]; do
      if [ $(pidof -x "clamscan" | wc -w) -gt 0 ]  || \
         [ $(pidof -x "clamdscan" | wc -w) -gt 0 ] || \
         [ $(pidof -x "freshclam" | wc -w) -gt 1 ]
         then
         let counter=$checkclamactiveseconds*4
         sleep 2
         else
         let counter-=1
         sleep .25
      fi
done

#STOPPING THE FRESHCLAM SERVICE
displaywdate "stopping freshclam service"
service freshclam stop > /dev/null
counter=1
until [ $counter -lt 1 ]; do
      if [ $(pidof -x "freshclam" | wc -w) -gt 0 ]
         then
         sleep 2
         else
         displaywdate "freshclam service stopped"
         counter=0
      fi
done

#STOPPING THE CLAMD SERVICE
if [ $stopclamdservice == 1 ]
   then
   displaywdate "stopping clamd service"

service clamd stop > /dev/null
counter=1
until [ $counter -lt 1 ]; do
      if [ $(pidof -x "clamd" | wc -w) -gt 0 ]
         then
         sleep 2
         else
         displaywdate "clamd service stopped"
          counter=0
      fi
done
fi

#COPYING THE DOWNLOADED CLAMAV CVD FILES TO
#THE CLAMAV CVD WORKING DIRECTORY
displaywdate "updating the Clamav cvd files"
cd $clamdir
rm  -f $clamdir/*.cvd 1> /dev/null 2> /dev/null
rm  -f $clamdir/*.cld 1> /dev/null 2> /dev/null
rm  -f $clamdir/mirrors.dat 1> /dev/null 2> /dev/null
cp -p $downloaddirectory/*.cvd $clamdir 1> /dev/null 2> /dev/null


#STARTING THE CLAMD SERVICE
if [ $stopclamdservice == 1 ]
   then
   displaywdate "starting clamd service"
   service clamd start > /dev/null
   counter=1
   until [ $counter -lt 1 ]; do
      if [ $(pidof -x "clamd" | wc -w) -gt 1 ]
         then
         sleep .5
         else
         counter=0
         displaywdate "clamd service started"
      fi
done
fi

#RELOADING THE CLAMAV VIRUS DEFINITION FILES IN THE CLAMD SERVICE

/usr/bin/clamdscan $downloaddirectory/$file3 \
   --no-summary --infected --reload 1>/devnull 2>/dev/null


#STARTING THE FRESHCLAM SERVICE
displaywdate "starting freshclam service"
service freshclam start > /dev/null
counter=1
until [ $counter -lt 1 ]; do
      if [ $(pidof -x "freshclam" | wc -w) -gt 1 ]
         then
         sleep .5
         else
         counter=0
         displaywdate "freshclam service started"
      fi
done

displaywdate "updated Clamav"

# THE SHOW THE VERSION OF CLAMAV
if [ $showversion == 1 ]
   then
   tempstring=$(clamd -V)
   displaywdate   "ClamAV version - $tempstring"
fi

# THE FOLLOWING IS TO JUST RUN A SHORT TEST OF CLAMSCAN
if [ $doclamtest == 1 ] 
   then
   until [ $counter -lt 1 ]; do
      if [ $(pidof -x "clamscan" | wc -w) -gt 0 ]
         then
         counter=60
         sleep 2
         else
         let counter-=1
         sleep .25
      fi
    done
    echo
    echo "Running a short clamscan test on an empty directory."
    echo "No files or viruses should be found in the summary."
    mkdir -p $clamdir/temp
    clamscan $clamdir/temp
    echo
    rm -rf $clamdir/temp 2> /dev/null
fi

displaywdate "$routinename bash routine ended successfully"
exit 1


--- End code ---

purvis:
Late yesterday doing some final testing.
I found out that it is possible that the current clamav datebase version number retrieved from a clamav website(host -t txt current.cvd.clamav.net) maybe be less than the version of the currently loaded database in the ClamAV service being run.
I would just never had expected that.
I figured the clamav website would of always been up to date and provided a higher version number or equal number to ClamAV service.
But never a lower version number.

The script clamcurrentck only compares the two version numbers in string format that are returned from the ClamAV service and the website.
If the two strings do not match, the clamcurrentck will return they do not match.
In the clamavupdate script, if a returned value from the clamcurrentck script indicates there is no match, then clamavdget is run to update the cvd files even if the most current cvd files had already been updated.

In order to improve the scripts and not cause unnecessary updates.
I will make a change to both clamcvdupdate and clamcurrentck scripts.
I will have clamcurrentck return a newly added value of 2 if the running ClamAV service reports a larger version number than the returned version from the ClamAV website.
Right now the returned value from clamcurrentck would be a 0(zero).

I am hoping to just update the posting of scripts above and I will make a posting they where updated.

purvis:
The changes where made to the above scripts.

While I was making those changes I posted on the previous post.
I also made some improvement to downloading that occurred to me over night to clamcvdget2.
Now clamcvdget2 can have an unlimited number of url sites to be added to try in downloading the cvd files.

With an unlimited number of url's to be added, then the variable secondsbetweendownloadtries maybe lowered if you have a long list of urls to be used.
You could have something like 1 set of urls duplicated about 4 or more times then set secondsbetweendownloadtries to equal something like 15 seconds or less.

Inside clamcvdget2 just place the urls to be used in reverse order near the top under the line

--- Code: --- declare -a downloadsite=(  \

--- End code ---

Also when the three cvd files are trying to be downloaded from a url site, any file not downloaded will break the effort to download any remaining cvd files.
This process will help speed up the retrying a download of the cvd files from the next url location.

Drifting:
Have I missed the point here ? I thought SME server did that on it's own?

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version