Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bash Script - Users administration on squid

Status
Not open for further replies.

Diubidone

IS-IT--Management
Nov 3, 2009
13
IT
I posted this on the Linux section but after doing some search on the forum I find this section more appropriate, sorry!


Hey all,

We need to setup a squid-proxy for a hotel, and we need to do some automation on the users administration. For this we have created a solution based on a bash script and the NCSA authentication system.

The script that we use to insert users that will be able to navigate via proxy inserts the usernames in the /etc/squid/htpasswd file and creates a second csv file described below:

prova1.prova1,1273781005
prova2.prova2,1273781016
prova3.prova3,1273781035

Where:
the first field is username composed by firstname.lastname,
the second field number is the timestamp in which users must be deleted

What I need is one or two scripts (Your advice here would be helpful) in order to:

1. grep all user's that have a minor timestamp than now (date --date="now" +%s).
2. delete those users using the htpasswd system and command which is:

htpasswd -D /etc/squid/passwd username

Anyone could help me?

Here's a script that I tried to adapt but couldn't make it because of lack of bash knowledge:

Delete User Script

#!/bin/bash

echo "Assignment1"
if [ "$(whoami)" != "root" ]
then
echo "Error: You ARE NOT ROOT!!!!!"
exit 1
else
echo "You are Lucky! I am watching you!!"
echo "Please enter the CSV"
read CSVFILE
exec < $CSVFILE
while
read line
do
firstName=$(echo $line | awk -F, '{print $1'})
lastName=$(echo $line | awk -F, '{print $2'})
idNumber=$(echo $line | awk -F, '{print $3'})
firstInit=$(echo $line | cut -c1)
userName=$lastName$firstInit
userName=$(echo $userName | tr 'A-Z' 'a-z')
userdel $userName
rm -rf /home/$userName
echo "Successfully deleted user $userName"
done
fi

Link:
Thx!
 
I would use a short perl script:

Code:
perl -nwe '
        my ($user,$expiry)=split /,/;
        if (time > $expiry) {
                print "removing expired user $user\n";
                system "htpasswd -D /etc/squid/passwd $user\n" ;
        }
' filename.csv

Annihilannic.
 
Or here it is in pure bash if that's what you would prefer:

Bash:
#!/bin/bash
while IFS=, read user expiry
do
        if [[ "$(date +%s)" -gt "$expiry" ]]
        then
                htpasswd -D /etc/squid/passwd $user
        fi
done < filename.csv

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top