0

I've written a bash script and would like to implement a timer that will also get the total time of the scripts execution duration and also the time that each loop iteration takes. For example, in the event that a user specifies the -f flag which will run the script on each IP address in a file instead of just 1 target with the -t flag, How can i reset the timer so that it returns the time of each IP Scan and also Returns the overall total time of all IP scans? This is My timer function currently

#!/bin/bash

NICE='\e[1;32;92m[+]\e[0m'
TEAL='\e[96m'
END='\e[0m'
SECONDS=0

timer() {

    echo -e "${TEAL}~~~~~~~~~~~~~~~~~~~~~~~ All Scans for Completed ~~~~~~~~~~~~~~~~~~~~~~~${END}"
    echo ""

    if (($SECONDS > 3600)); then
        hours=SECONDS/3600
        let "minutes=(SECONDS%3600)/60"
        let "seconds=(SECONDS%3600)%60"
        echo -e "${NICE} Scans Completed in $hours hour(s), $minutes minute(s) and $seconds second(s)"
    elif (($SECONDS > 60)); then
        let "minutes=(SECONDS%3600)/60"
        let "seconds=(SECONDS%3600)%60"
        echo -e "${NICE} Scans Completed in $minutes minute(s) and $seconds second(s)"
    else
        echo -e "${NICE} Scans Completed in $SECONDS seconds"
    fi
    echo -e ""
}

I created a resetTimer function that just resets SECONDS to 0 by doing

resetTimer() {
    SECONDS=0
}

Which almost works as it will show the individual times of each scan however, since I'm resetting SECONDS to 0, I'm not sure how to get the total time of all the scans if I use the reset function. I'm sure there's probably a really simple solution however I haven't been able to find it / figure out how to do it properly.

Here is very simplified Psuedo-code of the flow of the rest of the script when looping through IP's from a file

totalTimeFunction() {
        ## ToDo: Create Total Time function that keeps track of original Starting SECONDS
        }

for target in $target_list; do
    # do stuff
    timer
    resetTimer
done
totalTimeFunction
  • Accumulate timer's output in some variable each time you reset SECONDS. – muru Aug 12 at 6:06
  • Accumulate it: add the new value each time: ((duration += SECONDS)), or duration=$((duration + SECONDS)). – muru Aug 12 at 6:16
  • Oh i see. Awesome. Thank you. For some reason i didn't think the duration variable would hold it's value each time the timer variable got called. Going to test it now. 1 sec. Wow that worked perfectly thank you! – MrPMillz Aug 12 at 6:18
0

Thanks again. I got it to work now. Here is the working timer function.

timer() {

    echo -e "${TEAL}~~~~~~~~~~~~~~~~~~~~~~~ Scanning for $rhost Completed ~~~~~~~~~~~~~~~~~~~~~~~${END}"
    echo ""

    duration=$((duration + SECONDS))
    if (($SECONDS > 3600)) || (($duration > 3600)); then
        hours=SECONDS/3600
        totalhours=$duration/3600
        let "minutes=(SECONDS%3600)/60"
        let "seconds=(SECONDS%3600)%60"
        echo -e "${NICE} Scans Completed in $hours hour(s), $minutes minute(s) and $seconds second(s)"
        let "totalminutes=($duration%3600)/60"
        let "totalseconds=($duration%3600)%60"
        echo -e "${NICE} Scans Completed in $totalhours hour(s), $totalminutes minute(s) and $totalseconds second(s)"
    elif (($SECONDS > 60)) || (($duration > 60)); then
        let "minutes=(SECONDS%3600)/60"
        let "seconds=(SECONDS%3600)%60"
        echo -e "${NICE} This Scan Completed in $minutes minute(s) and $seconds second(s)"
        let "totalminutes=($duration%3600)/60"
        let "totalseconds=($duration%3600)%60"
        echo -e "${NICE} All Scans Completed in $totalminutes minute(s) and $totalseconds second(s)"
    else
        echo -e "${NICE} This Scan Completed in $SECONDS seconds"
        echo -e "${NICE} All Scans Completed in $duration seconds"
    fi
    echo -e ""
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.