LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   simple htpasswd check logs script. (https://www.linuxquestions.org/questions/programming-9/simple-htpasswd-check-logs-script-4175486257/)

casperdaghost 11-29-2013 09:32 AM

simple htpasswd check logs script.
 
I have a script that runs a last of usernames and passwords though and htpasswd command - i output the stdout to a log file which is hundreds of thousands of line long.


Code:

/usr/local/apache2/bin/htpasswd -b $file $user $pass
Code:

Adding user: unknownl
Adding password for user unknown1
Adding user: unknown2
Adding password for user unknown2
Adding user: unknown3
Adding password for user unknown3
Adding user: varmand
Adding password for user varmand
Adding user: mustest
Adding password for user emustest
Adding user: dbtest1
Adding password for user dbtest1

I want to set up a check script that will look for anything out of the ordinary in the htpasswd hash creation.
like if there is an
Code:

"Adding user: $user"
line
there must be complemented with a line right after it that says:
Code:

Adding password for user $user
- like is has to be for the same user.


If the two lines do not follow that format - there must be some error. I tried using backreferences in perl, but I am drawing a blank.
Does anyone have any ideas on how i could approach this.

druuna 11-29-2013 12:54 PM

Have a look at this:
Code:

#!/bin/bash

inFile="/pat/to/infile"

userRGX='^Adding user: (.*)'
pswdRGX='^Adding password for user (.*)'

while read LINE
do
  [[ "$LINE" =~ $userRGX ]] && userAdd=("${userAdd[@]}" "${BASH_REMATCH[1]}")
  [[ "$LINE" =~ $pswdRGX ]] && userPwd=("${userPwd[@]}" "${BASH_REMATCH[1]}")
done < <( cat $inFile )

oldIFS=$IFS
IFS=$'\n\t'

echo "Users without a valid password entry : "
comm -23 <( echo "${userAdd[*]}" | sort ) <( echo "${userPwd[*]}" | sort )

echo -e "\nPassword entries without a valid user entry : "
comm -13 <( echo "${userAdd[*]}" | sort ) <( echo "${userPwd[*]}" | sort )

exit 0

Example run:
Code:

$ cat infile
Adding user: unknown1
Adding password for user unknown1
Adding user: unknown2
Adding user: unknown3
Adding password for user unknown3
Adding user: varmand
Adding password for user varmand
Adding user: mustest
Adding password for user emustest
Adding user: dbtest1
Adding password for user dbtest1

$ ./foo.sh
Users without valid password entry :
mustest
unknown2

Password entries without valid user entry :
emustest

The above code isn't perfect; If the same user name is added multiple times and one of them doesn't have a valid password entry the script will report the user name, but not if it is the first, second, third, .... entry.

casperdaghost 11-29-2013 10:03 PM

it may not be perfect - but it will lead me in the right direction.
i am the only one who can coade anything in this office.
i have no access to chat or irc.

no body to bounce things off of - this i will lead me in the right direction. all i need is a push, thanks;

druuna 11-30-2013 02:06 AM

Quote:

Originally Posted by casperdaghost (Post 5072736)
it may not be perfect - but it will lead me in the right direction.

i am the only one who can coade anything in this office.
i have no access to chat or irc.

no body to bounce things off of - this i will lead me in the right direction. all i need is a push, thanks;

You're welcome.

If you run into anything or have questions about the above script; Just ask.


All times are GMT -5. The time now is 11:21 PM.