Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm currently writing a script to examine /var/log/secure file and report how many failed attempts there were that day. I need to output it in the following format:

Date: 03/15/10 Time: 10:30 Number of failed attempts: 8

Heres my code so far (Haven't done much)... Am I on the right track?

#!/bin/bash

classGID=5000
passfile=/var/log/secure


for i in $(grep ${classGID} ${passfile} | cut -d: -f1)
do

date=$(grep $i{passfile} | cut -d: -f2)

echo "Date: ${date}"

done
share|improve this question
    
I recommend just using logwatch. –  jordanm Mar 14 '13 at 23:00
2  
or use perl to make things much easier. –  michas Mar 14 '13 at 23:39
add comment

1 Answer

up vote 0 down vote accepted
#!/usr/bin/env bash

c=1
while read line
do
        a=( $line )
        d="${a[@]::3}"
        logdate=$( date '+%F' -d"$d" )
        if [[ $logdate == $old_logdate ]]; then
                (( c++ ))
        elif [[ $old_logdate != $logdate ]]; then
                printf "%s\t%s\t%s%s\n" "Date: $old_logdate Number of failed attempts: $c"
                old_logdate=$logdate
                c=1
                continue
        else
                printf "%s\t%s\t%s%s\n" "Date: $logdate Number of failed attempts: $c"

        fi

done < <(grep 'authentication failure' /var/log/secure)

it's skip last record , I don't know why

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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