Welcome!

Please read this important information about Spectre and Meltdown vulnerabilities.
Please read this important information about MX sources lists.
News
  • MX Linux on social media: here
  • Mepis support still here
Current releases
  • MX-17 Final release info here
  • MX-16.1 release info here
  • antiX-17 release info here
    New users
    • Please read this first, and don't forget to add system and hardware information to posts!
    • Read Forum Rules

Clam scripts to scan and update according to internet-time

Here you can exchange scripts that you created or have permission to share with other users.
Message
Author
User avatar
GDixon
Forum Regular
Forum Regular
Posts: 358
Joined: Fri Nov 02, 2007 4:39 pm

Clam scripts to scan and update according to internet-time

#1 Postby GDixon » Fri Nov 07, 2014 3:51 pm

Here are 3 scripts that work together to call avscan then calls a script to determine Internet connection and or elapsed time since a freshclam virus update which calls the freshclam update.

only user inter action is reading the screen and putting in the root/superuser password if it's determined that freshclam needs to be up dated.

everything goes to a log and you need to make a very few changes for your system

in avscan make the changes to what you want scanned
in the connect_time script change the time to your needs and situation (3 hours currently)
the update script should need only the log location changed.

of course rename the scripts, make them executable and place them where you place your scripts normally.
Mine are in /usr/local/bin/ and when there related scripts that call one another they go in their own file
/usr/local/bin/whatever describes them
and change a few parameters to match your system, ask questions and i will help.

I put the lines from the variables that might need changing to fit your needs at the top and made them as obvious as possible. There are very few needing changes.

5 at the top of the variable list for the avscan script
2 at the top of the variable list for the connect_time script
1 at the top of the variable list for the update script
These should be the only "must change" to match your needs


I broke them up into 3 scripts for their function, ease and also for editing. I can make it one big script if anyone wants and also make a menu for several avscan locations where you can pick what to scan.

Last thing I want to do is make a progress bar or something so that the terminal is a bit quieter and not have all the files being scanned rolling by. for now you can let them, use -i to show only infected files or i think -o for no output to screen...with nothing scrolling by it looks like everything froze when it hasn't so i left it to have the scanned files scroll by to show it's running..

Last if anyone has suggestions on how to make the script commands shorter or better let me know.


Main script for scanning

Code: Select all

#!/bin/bash

clear

# Remember to make executable and check permisions

# To scan drives for a virus using ClamAV

# You will need clamav and it's depends installed.

# If you want to make use of any success and or error sounds using ogg or mp3
# I suggest installing mplayer and using it in a script.
# line below will run mplayer in the background avoiding
# mplayers quite verbose output, play the ogg file and close.
#
#     Example : use below in a script making needed changes for your system.
#
# mplayer /usr/share/sounds/clam_ogg/Clam-Success.ogg </dev/null >/dev/null 2>&1 &


# change the 5 lines below to match your needs

CONNECT_TIME="$HOME/new_test/connect_test_time"     # location of connect script
TARGET="/media/Extra/"                                                        # Change to what you want scanned
VIRUS="$HOME/.VIRUS/"                                                      # directory you want the virus's moved to
SCAN_LOG="$HOME/ScanLog/Clam.log"                              # directory you want the scan log placed
HIDE="(hidden directory)"                                                       # uncomment if VIRUS is a hidden directory


VERSION="11-07-2014"                          # used for log
MOVED="Virus has been moved to:"
RIGHT_NOW=$(date "+ %a %x %r")
RIGHT_NOW1=$(date "+ %s")
TIME_STAMP="Updated on $RIGHT_NOW by $USER"
NO_DRIVE="Failed to scan: Not plugged in and or mounted?"
YIKES="Yikes drive not plugged in and or mounted : Nothing to scan"
SUBJECT="----------- SCAN  REPORT ------------"            # Used for log
SUBJECT1="time stamp for script use"                               # used for log
SUBJECT2="----------- SCAN  FAILED -----------"              # used for log
VIRUS0="---------- NO VIRUS FOUND ----------"                # used for log and print to screen
VIRUSL1="------------- VIRUS FOUND -------------"            # used for log
ERRORL2="----------- ERROR   FOUND -----------"           # used for log
VIRUSP1="----- VIRUS  FOUND : CHECK  LOG -----"       # Print to screen
ERRORP2="----- ERROR  FOUND : CHECK  LOG -----"   # Print to screen
exit=?                                                                                    # Exit status

# uncomment 4 lines below to remove old log
# and time stamp new log
# leave commented to append to log
# Changes will be needed in the connect_time
# script or freshclam will be unable to update
# due to the time check

# REMEMBER TO MAKE NEEDED CHANGES IN CONNECT_TIME SCRIPT IF UNCOMMENTED BELOW

#     echo
#     echo -e "Removing old log\n"; sleep 3; clear
#     rm -f "$HOME/ScanLog/Clam.log"                                           # Junk old logfile.
#     echo -e "$SUBJECT1 $RIGHT_NOW1\n" >> $SCAN_LOG    # time stamp new log

# REMEMBER TO MAKE NEEDED CHANGES IN CONNECT_TIME SCRIPT IF UNCOMMENTED ABOVE



echo
echo -e "Testing for internet connection and checking time of last update\n"

sleep 4


# use this to test for internet connection and to call freshclam if connected
# if not connected then log: not connected and do the scan anyhow
# Will also check to see how long ago virus database has been updated
# and depending on length of time either allow the update or deny the update

$CONNECT_TIME   # calls script that determines if there is a internet
                # connection and also how long it has been since the
                # virus database has been updated




if [ -d $TARGET ]; then
    echo  # just a blank line to make reading easier   
    echo -e  "Getting ready to scan $TARGET for viruses\n"
    echo -e  "If any infected files are found they will be\n"
    echo -e  "moved to $VIRUS $HIDE\n"
    echo -e  "Scan Log will be in $SCAN_LOG\n"
    echo -e  "Scan will begin shortly, be patient $USER\n"

clamscan -r --move=$VIRUS $TARGET -l $SCAN_LOG     # clamscan command and arguments/options


             

              if [ "$?" -eq "1" ]; then
                      echo -e "\n"  >> $SCAN_LOG        # inserts 2 blank lines in log (for layout and formatting)
                      echo $VIRUSL1 >> $SCAN_LOG
                      echo -e "$MOVED\n$VIRUS\n$HIDE\n" >> $SCAN_LOG
                      echo
                      echo -e "$VIRUSP1"             

                    elif [ "$?" -gt "1" ]; then
                      echo -e "\n"  >> $SCAN_LOG
                      echo $ERRORL2 >> $SCAN_LOG
                      echo
                      echo -e "$ERRORP2"

                    else
                      echo -e "\n"  >> $SCAN_LOG
                      echo  -e "$VIRUS0\n" >> $SCAN_LOG
                      echo
                      echo -e "$VIRUS0"
              fi


    echo  >> $SCAN_LOG    # inserts blank line in log (for layout and formatting)
    echo $SUBJECT >> $SCAN_LOG
    echo -e "Script: $(basename $0) v$VERSION - for user: $LOGNAME" >> $SCAN_LOG
    echo -e "Scanmode: Recursive\nFiles Scanned: $TARGET\n$TIME_STAMP\n" >> $SCAN_LOG

# above logging is used when drive/directory is present

sleep 10; clear

# below is used for log when drive is missing or not mounted

else
    echo
    echo -e "$YIKES\n"
    echo >> $SCAN_LOG    # inserts a blank line in log (for layout and formatting)
    echo $SUBJECT2  >> $SCAN_LOG
    echo -e "Script: $(basename $0) v$VERSION - for user: $LOGNAME" >> $SCAN_LOG
    echo -e "Missing target $TARGET\n$NO_DRIVE\n$TIME_STAMP\n" >> $SCAN_LOG

       sleep 4; clear


fi
        exit
   done



Script for Internet connection and or time based updating


Code: Select all

#!/bin/bash

clear

# change the 2 lines below to match your needs

VIRUS_UPDATE="/home/Greg/new_test/update_test"           # this calls freshclam from another script
SCAN_LOG="$HOME/ScanLog/Clam.log"               # directory the log is placed in

VERSION="11-07-2014"
exit=?
BLANK="-------------------------------------------------------------------------------"
RIGHT_NOW1=$(date "+ %s")
RIGHT_NOW=$(date "+ %a %x %r")
TIME_STAMP="Updated on $RIGHT_NOW by $USER"
SUBJECT1="time stamp for script use"
SUBJECT2="Something went wrong (exit code greater than 2)"
INT_TIME="Connected and Stamp found"
INT_TIME1="update more than"
INT_TIME2="update less than"
WAIT_TIME="3 Hours ago"         
SECONDS="10800"  # 3600 seconds per hour, 10,800 seconds in 3 hours
                 # this is where you can change your time interval
                 # for virus database updating, change the red number
                 # in seconds to what you need or prefer,
                 # currently it is 3 hours or 10,800 seconds
                 # Changes need to be in seconds


wget -q --tries=10 --timeout=20 -O - http://google.com > /dev/null

if [ "$?" -eq "0" ]; then

                  echo
                  echo -e "Checking for Clam.log time stamp\n"
                    sleep 4; clear

                if  grep -q "$SUBJECT1" "$SCAN_LOG"; then

                  echo
                  echo -e "Clam.Log is time stamped\n"
                    sleep 4; clear   

                else

                  echo -e "$SUBJECT1 $RIGHT_NOW1\n" >> $SCAN_LOG
                  echo
                  echo -e "Time stamp is missing : Adding current time stamp\n"
                  echo -e "Virus database will not be updated at this time\n"
                    sleep 6; clear

                fi

 
# pulls the update time from the Clam.log in seconds
# 3600 seconds per hour, 10,800 seconds in 3 hours

log_time=$(tac $SCAN_LOG | grep -m 1 "$SUBJECT1" | awk '{print $NF}')

# Pulls system time in seconds

sys_time=$(date "+%s" | awk '{print $NF}')

# determines if virus database needs updating according
# to internet connection and time stamp in Clam.log

test $(($sys_time-$log_time)) -gt "$SECONDS"


        if [ "$?" -eq "0" ]; then


          clear
          echo
          echo -e "$INT_TIME\n$INT_TIME1 $WAIT_TIME\nupdating virus database before scan\n"
          #echo -e "$INT_TIME\n"
          #echo -e "more than $WAIT_TIME updating virus database before scan\n"
          echo -e "Wait just a moment for update to start $USER\n"
          echo >> $SCAN_LOG
          echo -e "$BLANK\n\n" >> $SCAN_LOG
          echo -e "-------- Internet Connected --------\n" >> $SCAN_LOG
          echo -e "$RIGHT_NOW" >> $SCAN_LOG
          echo -e "$INT_TIME\n$INT_TIME1 $WAIT_TIME" >> $SCAN_LOG
          #echo -e "$INT_TIME\n" >> SCAN_LOG
          #echo -e "more than $WAIT_TIME" >> $SCAN_LOG
          echo -e "Virus database updated before scan" >> $SCAN_LOG
          echo -e "$SUBJECT1 $RIGHT_NOW1\n" >> $SCAN_LOG



       sleep 8; clear

        $VIRUS_UPDATE         # calls script to start freshclam
                                               # to update the virus database


        elif [ "$?" -eq "1" ]; then



          echo
          echo -e "$INT_TIME\n$INT_TIME2 $WAIT_TIME\nnot updating virus database before scan\n"
          #echo -e "$INT_TIME\n"
          #echo -e "less than $WAIT_TIME not updating virus database before scan\n"
          echo -e "Wait just a moment for scan to start $USER\n"
          echo >> $SCAN_LOG
          echo -e "$BLANK\n\n" >> $SCAN_LOG
          echo -e "------   Internet Connected --------\n" >> $SCAN_LOG
          echo -e "$RIGHT_NOW" >> $SCAN_LOG
          echo -e "$INT_TIME\n$INT_TIME2 $WAIT_TIME" >> $SCAN_LOG
          #echo -e "less than $WAIT_TIME" >> $SCAN_LOG
          echo -e "WILL NOT UPDATE VIRUS DATABASE" >> $SCAN_LOG
          echo -e "$SUBJECT1 $RIGHT_NOW1\n" >> $SCAN_LOG

        sleep 10; clear

        else

          echo -e "$SUBJECT2"
        sleep 4; clear         
 

        fi


else
        echo
        echo -e "Internet disconnected running scan without updating virus database\n"
        echo -e "Wait just a moment for scan to start $USER\n"
        echo >> $SCAN_LOG
        echo -e "$BLANK\n\n" >> $SCAN_LOG
        echo -e "------ Internet  disconnected ------\n" >> $SCAN_LOG
        echo -e "running scan without updating virus database\n" >> $SCAN_LOG

     sleep 6; clear   
       
fi

   exit



the script to call freshclam and do the actual updating



Code: Select all

#!/bin/bash


# Script has to be run as root/superuser

# This Script is to be used to update virus data base

# place script in /usr/local/bin and make executable or
# anywhere in your path where you place your scripts
# you will have to make changes in the script if other than /usr/local/bin

# This is to check if your root/superuser and if not prompts you for root's password

if [ $(id -u) != "0" ]; then
   
    echo
    echo -e "          You must be root/superuser to run this script $USER\n" >&2

# su -c ''  will give a root password prompt for this sesion and then close root when you quit the script

su -c /usr/bin/freshclam

clear


# Change to match Clam.log location

SCAN_LOG="$HOME/ScanLog/Clam.log"                         # directory the log is placed in

VERSION="11-07-2014"                                      # Used for log
BLANK="-------------------------------------------------------------------------------"
RIGHT_NOW=$(date "+ %a %x %r")
TIME_STAMP="Updated on $RIGHT_NOW by $USER"
SUBJECT="-------- VIRUS UPDATE REPORT --------"           # Used for log
SUBJECT2="Virus database has been updated and is current"
LOG_UPDATE="Log has been updated to show database is current"



    echo  >> $SCAN_LOG    # inserts blank line in log (for layout and formatting)
    echo -e "$BLANK\n\n"  >> $SCAN_LOG
    echo $SUBJECT >> $SCAN_LOG
    echo -e "Script: $(basename $0) v$VERSION - for user: $LOGNAME" >> $SCAN_LOG
    echo -e "$SUBJECT2\n$TIME_STAMP\n" >> $SCAN_LOG
    echo
    echo -e "$LOG_UPDATE\n"

sleep 6; clear

fi

    exit

User avatar
GDixon
Forum Regular
Forum Regular
Posts: 358
Joined: Fri Nov 02, 2007 4:39 pm

Re: Clam scripts to scan and update according to internet-ti

#2 Postby GDixon » Tue Nov 11, 2014 11:27 pm

Here are cleaned up scripts with a menu for choosing what and where to scan.

Still working on a progress bar of some type but it seems elusive so far.
Let me know of any place they can be improved please.

heres the menu

Code: Select all

#!/bin/bash

clear

# Remember to make executable and check permissions

# case menu for avscan

#------------------------------ START : VARIABLES -----------------------------#

VERSION="11-11-2014"

INFO_P="Will start scan on"
SCAN="/usr/local/bin/virus_scan/avscan"

SCAN_TARGET_1="/media/Storage/"                           # location to scan
SCAN_TARGET_2="/media/Storage/NewQbToTransfer/"           # location to scan
SCAN_TARGET_3="/media/Extra/"                             # location to scan
SCAN_TARGET_4="/media/TvShows/"                           # location to scan
SCAN_TARGET_5="NOT USED CURRENTLY"                        # location to scan
SCAN_TARGET_6="NOT USED CURRENTLY"                        # location to scan
                                                     
#------------------------------- END : VARIABLES ------------------------------#


#--------------------------------- MENU START ---------------------------------#
   
    until [ "$selection" = "q | Q" ]; do

    echo
    echo -e "What / Where would you like to scan for a Virus / Infection $USER?\n"

    echo -e "1 - Scan $SCAN_TARGET_1\n"
    echo -e "2 - Scan $SCAN_TARGET_2\n"
    echo -e "3 - Scan $SCAN_TARGET_3\n"
    echo -e "4 - Scan $SCAN_TARGET_4\n"
    echo -e "5 - Scan $SCAN_TARGET_5\n"
    echo -e "6 - Scan $SCAN_TARGET_6\n"

    echo -e "q | Q - quit and take no further action | exit program\n"
    echo -e "waiting on your choice $USER\n"
    echo
    read selection

    case $selection in

        1 ) clear; echo
            echo -e "$INFO_P $SCAN_TARGET_1 $USER\n"           
            sleep 3; clear
            export MAIN_TARGET=$SCAN_TARGET_1; $SCAN ;;

        2 ) clear; echo
            echo -e "$INFO_P $SCAN_TARGET_2 $USER\n"
            sleep 3; clear
            export MAIN_TARGET=$SCAN_TARGET_2; $SCAN ;;

        3 ) clear; echo
            echo -e "$INFO_P $SCAN_TARGET_3 $USER\n"
            sleep 3; clear
            export MAIN_TARGET=$SCAN_TARGET_3; $SCAN ;;
       
        4 ) clear; echo
            echo -e "$INFO_P $SCAN_TARGET_4 $USER\n"
            sleep 3; clear
            export MAIN_TARGET=$SCAN_TARGET_4; $SCAN ;;

        5 ) clear; echo
            echo -e "$INFO_P $SCAN_TARGET_5 $USER\n"
            sleep 3; clear ;;
          # export MAIN_TARGET=$SCAN_TARGET_5; $SCAN ;;

        6 ) clear; echo
            echo -e "$INFO_P $SCAN_TARGET_6 $USER\n"
            sleep 3; clear ;;
          # export MAIN_TARGET=$SCAN_TARGET_6; $SCAN ;;


    q | Q ) clear;exit ;;

        * ) clear; echo; echo -e "INVALID ENTRY : RESTARTING MENU\n"
            sleep 1; clear ;;
    esac
        done
#--------------------------------- MENU END -----------------------------------#


Heres the scan portion

Code: Select all

#!/bin/bash

clear

# Remember to make executable and check permisions

#------------------------------ START : VARIABLES -----------------------------#

VERSION_L="11-11-2014"                                      # used for log

CONNECT_TIME="/usr/local/bin/virus_scan/connect_time"     # location of connect script

#RIGHT_NOW1=$(date "+ %s")                                 # used for log
RIGHT_NOW=$(date "+ %a %x %r")                            # used for log
TIME_STAMP_L="Updated on $RIGHT_NOW"                      # used for log

SCAN_LOG="$HOME/ScanLog/Clam.log"                         # directory you want the scan log placed
MOVED_L="Virus has been moved to:"                        # used for log
VIRUS="$HOME/.VIRUS/"                                     # directory you want the virus's moved to
HIDE="(hidden directory)"                                 # uncomment if VIRUS is a hidden directory

SUBJECT_L1="----------- SCAN  REPORT ------------"        # Used for log
SUBJECT_L2="----------- SCAN  FAILED -----------"         # used for log

VIRUS_LP="---------- NO VIRUS FOUND ----------"           # used for log and print to screen
VIRUS_L="------------- VIRUS FOUND -------------"         # used for log
VIRUS_P="----- VIRUS  FOUND : CHECK  LOG -----"           # Print to screen

ERROR_L="----------- ERROR   FOUND -----------"           # used for log
ERROR_P="----- ERROR  FOUND : CHECK  LOG -----"           # Print to screen

######## MAIN_TARGET is now exported as a system variable from the menu ########

NO_DRIVE="Failed to scan: Not plugged in and or mounted?           "  # Print to screen
YIKES="Yikes drive not plugged in and or mounted : Nothing to scan"   # Print to screen

exit=?                                                                # Exit status

#------------------------------- END : VARIABLES ------------------------------#


# uncomment 4 lines below to remove old log
# and time stamp new log
# leave commented to append to log
# Changes will be needed in the connect_time
# script or freshclam will be unable to update
# due to the time check

#- REMEMBER TO MAKE NEEDED CHAGES IN CONNECT_TIME SCRIPT IF UNCOMMENTED BELOW -#

#   echo
#   echo -e "Removing old log\n"; sleep 3; clear
#   rm -f "$CLAM_LOG"                                 # Junk old logfile.
#   echo -e "$SUBJECT1 $RIGHT_NOW1\n" >> $SCAN_LOG    # time stamp new log

#- REMEMBER TO MAKE NEEDED CHANGES IN CONNECT_TIME SCRIPT IF UNCOMMENTED ABOVE -#


#-------------------------- CONNECTION : TIME START ---------------------------#

echo
echo -e "Testing for internet connection and checking time of last update\n"

sleep 4


# use this to test for internet connection and to call freshclam if connected
# if not connected then log: not connected and do the scan anyhow
# Will also check to see how long ago virus database has been updated
# and depending on length of time either allow the update or deny the update

$CONNECT_TIME   # calls script that determines if there is a internet
                # connection and also how long it has been since the
                # virus database has been updated

#-------------------------- CONNECTION : TIME END -----------------------------#


#---------------------------- MAIN PART TO DO SCAN -------------------------------#

if [ -d $MAIN_TARGET -o -f $MAIN_TARGET ]; then
    echo  # just a blank line to make reading easier   
    echo -e  "Getting ready to scan $MAIN_TARGET for viruses\n"
    echo -e  "If any infected files are found they will be\n"
    echo -e  "moved to $VIRUS $HIDE\n"
    echo -e  "Scan Log will be in $SCAN_LOG\n"
    echo -e  "Scan will begin shortly, be patient $USER\n"


    echo  >> $SCAN_LOG    # inserts blank line in log (for layout and formatting)
    echo $SUBJECT_L1 >> $SCAN_LOG
    echo -e "Script: $(basename $0) v$VERSION_L - for user: $LOGNAME" >> $SCAN_LOG
    echo -e "Scanmode: Recursive\nFiles Scanned: $MAIN_TARGET\n$TIME_STAMP_L\n" >> $SCAN_LOG


clamscan -r --move=$VIRUS $MAIN_TARGET -l $SCAN_LOG     # clamscan command and arguments/options



             

              if [ "$?" -eq "1" ]; then
                      echo -e "\n"  >> $SCAN_LOG        # inserts 2 blank lines in log (for layout and formatting)
                      echo $VIRUS_L >> $SCAN_LOG
                      echo -e "$MOVED_L\n$VIRUS\n$HIDE\n" >> $SCAN_LOG
                      echo
                      echo -e "$VIRUS_P"             

                    elif [ "$?" -gt "1" ]; then
                      echo -e "\n"  >> $SCAN_LOG
                      echo $ERROR_L >> $SCAN_LOG
                      echo
                      echo -e "$ERROR_P"

                    else
                      echo -e "\n"  >> $SCAN_LOG
                      echo  -e "$VIRUS_LP\n" >> $SCAN_LOG
                      echo
                      echo -e "$VIRUS_LP"
              fi


# above logging is used when drive/directory is present

sleep 8; clear  # this sleeps long enough to read scan
                # summary then clears and goes back to menu

# below is used for log when drive is missing or not mounted

else
    echo
    echo -e "$YIKES\n"
    echo >> $SCAN_LOG    # inserts a blank line in log (for layout and formatting)
    echo $SUBJECT_L2  >> $SCAN_LOG
    echo -e "Script: $(basename $0) v$VERSION_L - for user: $LOGNAME" >> $SCAN_LOG
    echo -e "Missing target $MAIN_TARGET\n$NO_DRIVE\n$TIME_STAMP_L\n" >> $SCAN_LOG

       sleep 4; clear

fi

    exit
        done


heres the Internet connected and Time elapsed portion

Code: Select all

#!/bin/bash

clear

# change the 2 lines below to match your needs

VIRUS_UPDATE="/usr/local/bin/virus_scan/update"   # this calls freshclam from another script
SCAN_LOG="$HOME/ScanLog/Clam.log"                 # directory the log is placed in

#------------------------------------------------------------------------------#

VERSION_L="11-11-2014"
exit=?
BLANK_L="-------------------------------------------------------------------------------"
RIGHT_NOW_L=$(date "+ %s")
RIGHT_NOW=$(date "+ %a %x %r")
TIME_STAMP_L="Updated on $RIGHT_NOW"
SUBJECT_L="time stamp for script use"
SUBJECT_P="Something went wrong (exit code greater than 2)"
INT_TIME_LP="Connected and Stamp found"
INT_TIME_LP_1="update more than"
INT_TIME_LP_2="update less than"
WAIT_TIME_LP="3 Hours ago"         
SECONDS="10800"  # 3600 seconds per hour, 10,800 seconds in 3 hours
                 # this is where you can change your time interval
                 # for virus database updating, change the red number
                 # in seconds to what you need or prefer,
                 # currently it is 3 hours or 10,800 seconds
                 # Changes need to be in seconds


wget -q --tries=10 --timeout=20 -O - http://google.com > /dev/null

if [ "$?" -eq "0" ]; then

                  echo
                  echo -e "Checking for Clam.log time stamp\n"
                    sleep 4; clear

                if  grep -q "$SUBJECT_L" "$SCAN_LOG"; then

                  echo
                  echo -e "Clam.Log is time stamped\n"
                    sleep 4; clear   

                else

                  echo -e "$SUBJECT_L $RIGHT_NOW_L\n" >> $SCAN_LOG
                  echo
                  echo -e "Time stamp is missing : Adding current time stamp\n"
                  echo -e "Virus database will not be updated at this time\n"
                    sleep 6; clear

                fi

 
# pulls the update time from the Clam.log in seconds
# 3600 seconds per hour, 10,800 seconds in 3 hours

log_time=$(tac $SCAN_LOG | grep -m 1 "$SUBJECT_L" | awk '{print $NF}')

# Pulls system time in seconds

sys_time=$(date "+%s" | awk '{print $NF}')

# determines if virus database needs updating according
# to internet connection and time stamp in Clam.log

test $(($sys_time-$log_time)) -gt "$SECONDS"


if  [ "$?" -eq "0" ]; then


          clear
          echo
          echo -e "$INT_TIME_LP\n$INT_TIME_LP_1 $WAIT_TIME_LP\nupdating virus database before scan\n"
          echo -e "Wait just a moment for update to start $USER\n"
          echo >> $SCAN_LOG
          echo -e "$BLANK_L\n\n" >> $SCAN_LOG
          echo -e "-------- Internet Connected --------\n" >> $SCAN_LOG
          echo -e "Script: $(basename $0) v$VERSION_L - for user: $LOGNAME" >> $SCAN_LOG
          echo -e "$INT_TIME_LP\n$INT_TIME_LP_1 $WAIT_TIME_LP" >> $SCAN_LOG
          echo -e "Virus database updated before scan" >> $SCAN_LOG
          echo -e "$TIME_STAMP_L" >> $SCAN_LOG
          echo -e "$SUBJECT_L $RIGHT_NOW_L\n" >> $SCAN_LOG



       sleep 8; clear

        $VIRUS_UPDATE         # calls script to start freshclam
                              # to update the virus database


        elif [ "$?" -eq "1" ]; then



          echo
          echo -e "$INT_TIME_LP\n$INT_TIME_LP_2 $WAIT_TIME_LP\nnot updating virus database before scan\n"
          echo -e "Wait just a moment for scan to start $USER\n"
          echo >> $SCAN_LOG
          echo -e "$BLANK_L\n\n" >> $SCAN_LOG
          echo -e "------   Internet Connected --------\n" >> $SCAN_LOG
          echo -e "Script: $(basename $0) v$VERSION_L - for user: $LOGNAME" >> $SCAN_LOG         
          echo -e "$INT_TIME_LP\n$INT_TIME_LP_2 $WAIT_TIME_LP" >> $SCAN_LOG
          echo -e "WILL NOT UPDATE VIRUS DATABASE" >> $SCAN_LOG
          echo -e "$TIME_STAMP_L\n" >> $SCAN_LOG

        sleep 8; clear

        else

          echo -e "$SUBJECT_P"
        sleep 4; clear         
 

        fi


else
        echo
        echo -e "Internet disconnected running scan without updating virus database\n"
        echo -e "Wait just a moment for scan to start $USER\n"
        echo >> $SCAN_LOG
        echo -e "$BLANK_L\n\n" >> $SCAN_LOG
        echo -e "------ Internet  disconnected ------\n" >> $SCAN_LOG
        echo -e "Script: $(basename $0) v$VERSION_L - for user: $LOGNAME" >> $SCAN_LOG       
        echo -e "running scan without updating virus database" >> $SCAN_LOG
        echo -e "$TIME_STAMP_L\n" >> $SCAN_LOG

     sleep 6; clear   
       
fi

   exit


and the last part is the update script using Freshclam

Code: Select all

#!/bin/bash


# Script has to be run as root/superuser

# This Script is to be used to update virus data base

# place script in /usr/local/bin and make executable or
# anywhere in your path where you place your scripts
# you will have to make changes in the script if other than /usr/local/bin

# This is to check if your root/superuser and if not prompts you for root's password

if [ $(id -u) != "0" ]; then
   
    echo
    echo -e "          You must be root/superuser to run this script $USER\n" >&2

# su -c ''  will give a root password prompt for this sesion and then close root when you quit the script

su -c /usr/bin/freshclam

clear

VERSION_L="11-11-2014"                                             # used for log
INFO_L="Script: $(basename $0) v$VERSION_L - for user: $LOGNAME"     # used for log
BLANK_L="-------------------------------------------------------------------------------"
RIGHT_NOW=$(date "+ %a %x %r")
TIME_STAMP_L="Updated on $RIGHT_NOW"                               # used for log
SCAN_LOG="$HOME/ScanLog/Clam.log"                                  # directory the log is placed in
SUBJECT_L="-------- VIRUS UPDATE REPORT --------"                  # used for log
SUBJECT__L_2="Virus database has been updated and is current"      # used for log
LOG_UPDATE_P="Log has been updated to show database is current"    # prints to screen



    echo  >> $SCAN_LOG                    # inserts blank line in log (for layout and formatting)
    echo -e "$BLANK_L\n\n"  >> $SCAN_LOG
    echo $SUBJECT_L >> $SCAN_LOG
    echo -e "$INFO_L" >> $SCAN_LOG
    echo -e "$SUBJECT__L_2\n$TIME_STAMP_L\n" >> $SCAN_LOG
    echo
    echo -e "$LOG_UPDATE_P\n"

sleep 6; clear

fi

    exit



Made some spelling corrections and further clean up to get rid of redundant variables. Good to go except for a progress bar for now.

User avatar
GDixon
Forum Regular
Forum Regular
Posts: 358
Joined: Fri Nov 02, 2007 4:39 pm

Re: Clam scripts to scan and update according to internet-ti

#3 Postby GDixon » Sat Nov 15, 2014 8:48 am

Here are all the scripts cleaned up and easier to read. I made a change on how the time stamp is logged. I have it going to it's own log so changing the Clam.log from a single appending log to multiple logs that are deleted according to the schedule you want and will not affect the time based portion of the virus database updating any longer.

MENU

Code: Select all

#!/bin/bash

clear

# Remember to make executable and check permissions

# case menu for avscan

#------------------------------ START : VARIABLES -----------------------------#

VERSION="11-15-2014"

INFO_P="Will start scan on"
SCAN="/usr/local/bin/virus_scan/avscan"
TYPE_1="SSD"
TYPE_2="USB"


SCAN_TARGET_1="/media/Storage/"             # SSD         # location to scan
SCAN_TARGET_2="/media/Extra/"               # SSD         # location to scan
SCAN_TARGET_3="/media/TvShows/"             # USB         # location to scan
SCAN_TARGET_4="NOT CURRENTLY USED"                        # location to scan
SCAN_TARGET_5="NOT CURRENTLY USED"                        # location to scan
SCAN_TARGET_6="NOT CURRENTLY USED"                        # location to scan
                                                     
#------------------------------- END : VARIABLES ------------------------------#


#--------------------------------- MENU START ---------------------------------#
   
    until [ "$selection" = "q | Q" ]; do

    echo
    echo -e "What / Where would you like to scan for a Virus / Infection $USER?\n"

    echo -e "1 - Scan $SCAN_TARGET_1\n"
    echo -e "2 - Scan $SCAN_TARGET_2\n"
    echo -e "3 - Scan $SCAN_TARGET_3\n"
    echo -e "4 - Scan $SCAN_TARGET_4\n"
    echo -e "5 - Scan $SCAN_TARGET_5\n"
    echo -e "6 - Scan $SCAN_TARGET_6\n"

    echo -e "q | Q - quit and take no further action | exit program\n"
    echo -e "waiting on your choice $USER\n"
    echo
    read selection

    case $selection in

        1 ) clear; echo
            echo -e "$INFO_P $TYPE_1 $SCAN_TARGET_1 $USER\n"           
            sleep 3; clear
            export MAIN_TARGET=$SCAN_TARGET_1; $SCAN ;;

        2 ) clear; echo
            echo -e "$INFO_P $TYPE_1 $SCAN_TARGET_2 $USER\n"
            sleep 3; clear
            export MAIN_TARGET=$SCAN_TARGET_2; $SCAN ;;

        3 ) clear; echo
            echo -e "$INFO_P $TYPE_2 $SCAN_TARGET_3 $USER\n"
            sleep 3; clear
            export MAIN_TARGET=$SCAN_TARGET_3; $SCAN ;;
       
        4 ) clear; echo
            echo -e "$INFO_P $SCAN_TARGET_4 $USER\n"
            sleep 3; clear ;;
          # export MAIN_TARGET=$SCAN_TARGET_4; $SCAN ;;

        5 ) clear; echo
            echo -e "$INFO_P $SCAN_TARGET_5 $USER\n"
            sleep 3; clear ;;
          # export MAIN_TARGET=$SCAN_TARGET_5; $SCAN ;;

        6 ) clear; echo
            echo -e "$INFO_P $SCAN_TARGET_6 $USER\n"
            sleep 3; clear ;;
          # export MAIN_TARGET=$SCAN_TARGET_6; $SCAN ;;


    q | Q ) clear;exit ;;

        * ) clear; echo; echo -e "INVALID ENTRY : RESTARTING MENU\n"
            sleep 1; clear ;;
    esac
        done
#--------------------------------- MENU END -----------------------------------#


avscan

Code: Select all

#!/bin/bash

clear

# Remember to make executable and check permissions

#------------------------------ START : VARIABLES -----------------------------#


#------------------------------- EXPORTS  START --------------------------------#

export exit=?
export RIGHT_NOW_L=$(date "+ %s")
export RIGHT_NOW=$(date "+ %a %x %r")                     # used for log
export TIME_STAMP_L="Updated on $RIGHT_NOW"               # used for log
export ERROR_P="----- ERROR  FOUND : CHECK  LOG -----"    # Print to screen
export SUBJECT_L="time stamp for script use"

######## MAIN_TARGET is now exported as a system variable from the menu ########

#-------------------------------- EXPORTS END ---------------------------------#

VERSION_L="11-15-2014"                                    # used for log

CONNECT_TIME="/usr/local/bin/virus_scan/connect_time"     # location of connect script




MOVED_L="Virus has been moved to:"                        # used for log
VIRUS="$HOME/.VIRUS_SCAN/virus_found/"                    # directory you want the virus's moved to
HIDE="(hidden directory)"                                 # uncomment if VIRUS_SCAN is a hidden directory

SUBJECT_L_1="----------- SCAN  REPORT ------------"        # Used for log
SUBJECT_L_2="----------- SCAN  FAILED -----------"         # used for log

VIRUS_LP="---------- NO VIRUS FOUND ----------"           # used for log and print to screen
VIRUS_L="------------- VIRUS FOUND -------------"         # used for log
VIRUS_P="----- VIRUS  FOUND : CHECK  LOG -----"           # Print to screen

ERROR_L="----------- ERROR   FOUND -----------"           # used for log


NO_DRIVE="Failed to scan: Not plugged in and or mounted?"             # used for log
YIKES="Yikes drive not plugged in and or mounted : Nothing to scan"   # Print to screen

#------------------------------- LOG FILES BELOW ------------------------------#

    # changed the way the time stamp is logged so switching between
    # SCAN_LOG types below no longer messes with the time based update
 
    # line below appends to log
    # comment when using separate log function below it
#export SCAN_LOG=$HOME/ScanLog/Clam.log                                    # export so other scripts can find log


    # uncomment line below to have separate logs for each scan
    # leave commented when using line above to append to log
export SCAN_LOG=$HOME/ScanLog/ClamAV-scan-for-$(date +%a-%R-%m-%d-%y).log  # export so other scripts can find log


    # uncomment this line for separate logs and
    # comment this line when appending to your log
find /$HOME/ScanLog/*.log -mtime +7 -exec rm -f {} \;                      # removes log files older than 7 days


    # leave alone unless changing location of time_stamp_log
export TIME_STAMP_LOG=$HOME/.VIRUS_SCAN/time_stamp_log/time_stamp.log      # export so other scripts can find
                                                                           # time stamp log

#------------------------------- LOG FILES ABOVE ------------------------------#


#------------------------------- END : VARIABLES ------------------------------#


#-------------------------- CONNECTION : TIME START ---------------------------#

echo
echo -e "Testing for Internet connection and checking time of last update\n"

sleep 3; clear


# use this to test for Internet connection and to call Freshclam if connected
# if not connected then log: not connected and do the scan anyhow
# Will also check to see how long ago virus database has been updated
# and depending on length of time either allow the update or deny the update

$CONNECT_TIME   # calls script that determines if there is a Internet
                # connection and also how long it has been since the
                # virus database has been updated

#-------------------------- CONNECTION : TIME END -----------------------------#


#---------------------------- MAIN PART TO DO SCAN -------------------------------#

if [ -d $MAIN_TARGET -o -f $MAIN_TARGET ]; then
    echo  # just a blank line to make reading easier   
    echo -e "Getting ready to scan $MAIN_TARGET for viruses"
    echo -e "If any infected files are found they will be"
    echo -e "moved to $VIRUS $HIDE"
    echo -e "Scan Log will be in $SCAN_LOG\n"
    echo -e "Scan will begin shortly, be patient $USER\n"


    echo >> $SCAN_LOG    # inserts blank line in log (for layout and formatting)
    echo -e "$SUBJECT_L_1" >> $SCAN_LOG
    echo -e "Script: $(basename $0) v$VERSION_L-user:$LOGNAME" >> $SCAN_LOG
    echo -e "Scanmode: Recursive\nFiles Scanned: $MAIN_TARGET\n$TIME_STAMP_L\n" >> $SCAN_LOG


 clamscan -r --move=$VIRUS $MAIN_TARGET -l $SCAN_LOG     # clamscan command and arguments/options
             

if                    [ "$?" -eq "1" ]; then
                      echo -e "\n"  >> $SCAN_LOG     # inserts 2 blank lines in log (for layout and formatting)
                      echo -e "$VIRUS_L" >> $SCAN_LOG
                      echo -e "$MOVED_L\n$VIRUS\n$HIDE\n" >> $SCAN_LOG
                      echo
                      echo -e "$VIRUS_P"             

                    elif [ "$?" -gt "1" ]; then
                      echo -e "\n"  >> $SCAN_LOG
                      echo -e "$ERROR_L" >> $SCAN_LOG
                      echo
                      echo -e "$ERROR_P"

                    else
                      echo -e "\n" >> $SCAN_LOG
                      echo -e "$VIRUS_LP\n" >> $SCAN_LOG
                      echo
                      echo -e "$VIRUS_LP"

                    fi


# above logging is used when drive/directory is present

sleep 6; clear  # this sleeps long enough to read scan
                       # summary then clears and goes back to menu

# below is used for log when drive is missing or not mounted

else
    echo
    echo -e "$YIKES\n"
    echo >> $SCAN_LOG    # inserts a blank line in log (for layout and formatting)
    echo -e "$SUBJECT_L_2"  >> $SCAN_LOG
    echo -e "Script: $(basename $0) v$VERSION_L-user:$LOGNAME" >> $SCAN_LOG
    echo -e "Missing target $MAIN_TARGET\n$NO_DRIVE\n$TIME_STAMP_L\n" >> $SCAN_LOG

       sleep 3; clear

fi

    exit
        done


connect_time

Code: Select all

#!/bin/bash

clear

# change the line below to match your needs


# this calls Freshclam from another script
VIRUS_UPDATE="/usr/local/bin/virus_scan/update"

#------------------------------------------------------------------------------#

VERSION_L="11-15-2014"
BLANK_L="-------------------------------------------------------------------------------"
INT_TIME_LP="Connected and Stamp found"
INT_TIME_LP_1="update more than"
INT_TIME_LP_2="update less than"
WAIT_TIME_LP="3 Hours ago"         
SECONDS="10800"  # 3600 seconds per hour, 10,800 seconds in 3 hours
                 # this is where you can change your time interval
                 # for virus database updating, change the red number
                 # in seconds to what you need or prefer,
                 # currently it is 3 hours or 10,800 seconds
                 # Changes need to be in seconds


wget -q --tries=10 --timeout=20 -O - http://google.com > /dev/null


if [ "$?" -eq "0" ]; then

                  echo
                  echo -e "Checking for log time stamp\n"
                    sleep 2; clear

                if  grep -q "$SUBJECT_L" "$TIME_STAMP_LOG"; then

                  echo
                  echo -e "Log is time stamped\n"
                    sleep 2; clear   

                else

                  echo -e "$SUBJECT_L 1415393781" >> $TIME_STAMP_LOG
                  echo
                  echo -e "Time stamp is missing : Adding time stamp"
                  echo -e "Adding Time stamp will force Virus database update\n"
                    sleep 4; clear

                fi

 
# pulls the update time from the time_stamp.log in seconds
# 3600 seconds per hour, 10,800 seconds in 3 hours

log_time=$(tac $TIME_STAMP_LOG | grep -m 1 "$SUBJECT_L" | awk '{print $NF}')

# Pulls system time in seconds

sys_time=$(date "+%s" | awk '{print $NF}')

# determines if virus database needs updating according
# to internet connection and time stamp in Clam.log

test $(($sys_time-$log_time)) -gt "$SECONDS"


if  [ "$?" -eq "0" ]; then


          clear
          echo
          echo -e "$INT_TIME_LP\n$INT_TIME_LP_1 $WAIT_TIME_LP\nupdating virus database before scan\n"
          echo -e "Wait just a moment for update to start $USER\n"
          echo >> $SCAN_LOG
          echo -e "$BLANK_L\n\n" >> $SCAN_LOG
          echo -e "-------- Internet Connected --------" >> $SCAN_LOG
          echo -e "Script: $(basename $0) v$VERSION_L-user:$LOGNAME" >> $SCAN_LOG
          echo -e "$INT_TIME_LP\n$INT_TIME_LP_1 $WAIT_TIME_LP" >> $SCAN_LOG
          echo -e "Virus database will be updated before scan" >> $SCAN_LOG
          echo -e "$TIME_STAMP_L\n" >> $SCAN_LOG

       sleep 5; clear

        $VIRUS_UPDATE         # calls script to start Freshclam
                              # to update the virus database


        elif [ "$?" -eq "1" ]; then



          echo
          echo -e "$INT_TIME_LP\n$INT_TIME_LP_2 $WAIT_TIME_LP\nnot updating virus database before scan\n"
          echo -e "Wait just a moment for scan to start $USER\n"
          echo >> $SCAN_LOG
          echo -e "$BLANK_L\n\n" >> $SCAN_LOG
          echo -e "-------- Internet Connected --------" >> $SCAN_LOG
          echo -e "Script: $(basename $0) v$VERSION_L-user:$LOGNAME" >> $SCAN_LOG         
          echo -e "$INT_TIME_LP\n$INT_TIME_LP_2 $WAIT_TIME_LP" >> $SCAN_LOG
          echo -e "WILL NOT UPDATE VIRUS DATABASE" >> $SCAN_LOG
          echo -e "$TIME_STAMP_L\n" >> $SCAN_LOG

        sleep 5; clear

        else
          echo
          echo -e "$ERROR_P"
        sleep 2; clear         
 

        fi


else
        echo
        echo -e "Internet disconnected running scan without updating virus database\n"
        echo -e "Wait just a moment for scan to start $USER\n"
        echo >> $SCAN_LOG
        echo -e "$BLANK_L\n\n" >> $SCAN_LOG
        echo -e "------ Internet  disconnected ------" >> $SCAN_LOG
        echo -e "Script: $(basename $0) v$VERSION_L-user:$LOGNAME" >> $SCAN_LOG       
        echo -e "running scan without updating virus database" >> $SCAN_LOG
        echo -e "$TIME_STAMP_L\n" >> $SCAN_LOG

     sleep 5; clear   
       
fi

   exit


update

Code: Select all

#!/bin/bash


# Script has to be run as root/superuser

# This Script is to be used to update virus data base

# place script in /usr/local/bin and make executable or
# anywhere in your path where you place your scripts
# you will have to make changes in the script if other than /usr/local/bin

# This is to check if your root/superuser and if not prompts you for root's password

if [ $(id -u) != "0" ]; then
   
    echo
    echo -e "You must be root/superuser to update the virus database $USER\n" >&2

# su -c ''  will give a root password prompt for this sesion and then close root when you quit the script

su -c /usr/bin/freshclam

clear

VERSION_L="11-15-2014"                                                 # used for Clam log
INFO_L="Script: $(basename $0) v$VERSION_L-user:$LOGNAME"              # used for Clam log

#log the time is taken from to determine time based update
#TIME_STAMP_LOG="$HOME/.VIRUS_SCAN/time_stamp_log/time_stamp.log"

SUBJECT_L_3="-------- VIRUS UPDATE REPORT --------"                    # used for Clam log
SUBJECT__L_4="Virus database has been updated and is current"          # used for Clam log
LOG_UPDATE_P="Time Stamp has been updated to show database is current" # prints to screen



    echo >> $SCAN_LOG                    # inserts blank line in log (for layout and formatting)
    echo -e "$SUBJECT_L_3" >> $SCAN_LOG
    echo -e "$INFO_L" >> $SCAN_LOG
    echo -e "$SUBJECT__L_4\n$TIME_STAMP_L\n" >> $SCAN_LOG
    echo >> $TIME_STAMP_LOG
    echo -e "------------- TIME STAMP -------------" >> $TIME_STAMP_LOG
    echo -e "Script: $(basename $0) v$VERSION_L-user:$LOGNAME" >> $TIME_STAMP_LOG       
    echo -e "$TIME_STAMP_L" >> $TIME_STAMP_LOG
    echo -e "$SUBJECT_L $RIGHT_NOW_L" >> $TIME_STAMP_LOG
    echo
    echo -e "$LOG_UPDATE_P\n"

sleep 4; clear

fi

    exit


desktop
e-mail me for a icon

Code: Select all

[Desktop Entry]
Categories=Applications;Utilities;
Comment[en_US]=use clamav to scan for a virus
Comment=use clamav to scan for a virus
Exec=/usr/local/bin/virus_scan/avscan_menu
GenericName[en_US]=
GenericName=
Icon=/usr/share/icons/Mine/Clam_icon/Clam.png
MimeType=
Name[en_US]=Virus Scan
Name=Virus Scan
Path=
StartupNotify=true
Terminal=true
TerminalOptions=
Type=Application
Version=0.0.1
X-DBUS-ServiceName=
X-DBUS-StartupType=none
X-KDE-SubstituteUID=false
X-KDE-Username=

You do not have the required permissions to view the files attached to this post.


Return to “Scripts”

Who is online

Users browsing this forum: No registered users and 1 guest