1833747 Members
2923 Online
110063 Solutions
New Discussion

Re: Scripting question

 
Sivasingam Santhakumar
Frequent Advisor

Scripting question

Dear admins,

I need to clean up a passwd file ( 150 dormant accounts). I have the list of user ids to be removed. Does anyone have a sed/perl script written for this purpose? It's not a secure OS yet, so I can edit the /etc/passwd file directly.

Thanks
Kumar
10 REPLIES 10
Peter Godron
Honored Contributor

Re: Scripting question

Hi,
how about:
#!/usr/bin/sh
while read record
do
userdel -r $record
done < list_of_users.dat

for help look at man userdel

Regards
Kent Ostby
Honored Contributor

Re: Scripting question

Create a file of ids to delete in this format and call it "del":

DEL

e.g.:

DEL hpdb
DEL kmo

copy del to a directory
copy the password file to a directory
create the following file called cleanit.awk:

BEGIN {FS=":";}
/^DEL/{FS=" ";
countit++;
todel[countit]=$2;
FS=":";next}
{daflag=0;
for (idx1 in todel)
if (todel[idx1]==$1) {daflag=1;}
if (!daflag) {print $0;}
}

run the following commands:
cat del passwd > useme
awk -f cleanit.awk < useme > newfile
diff passwd newfile

Might not be the prettiest thing around, but it will work.

Best regards,

Oz


"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Gordon  Morrison
Trusted Contributor

Re: Scripting question

Hi Kumar,
First, make a safe backup copy of /etc/passwd before you do anything else!

Who needs sed or perl? Try something like this:

cp -p /etc/passwd passwd.safe
cp /etc/passwd passwd.tmp
for user in `cat deadusers.txt`
do
grep -v "^${user}:" passwd.tmp > passwd.tmp2
mv passwd.tmp2 passwd.tmp
done
cp passwd.tmp /etc/passwd

hope that helps
What does this button do?
Stefan Schulz
Honored Contributor

Re: Scripting question

Hi Kumar,

if you put all the IDs to delete to a file then you can use grep to remove them.

Just do something like:

grep -v -f yourfilewithids /etc/passwd > /tmp/newpasswd

Of course doublecheck the result befor you replace the original /etc/passwd

Hope this helps

Regards Stefan
No Mouse found. System halted. Press Mousebutton to continue.
Slawomir Gora
Honored Contributor

Re: Scripting question

Hi,

o you have only uid not user names use it:

#!/bin/sh

if [ -f $1 ]
then
INF=$1
else
exit
fi


for ID in `cat ${INF}`
do
UNAME=`cat /etc/passwd | grep ":${ID}:" | awk -F':' '{print $1}'`
CMD="userdel -r ${UNAME}"
echo ${CMD}
${CMD}
done


# $1 - file name of uid-s
ex:
106
107
108
Ravi_8
Honored Contributor

Re: Scripting question

Hi,

make a file of users to be deleted

for i in 'cat file'
do
userdel -r $i
done
never give up
Ravi_8
Honored Contributor

Re: Scripting question


one more

#cat /etc/passwd |awk -F: '{print $1}' > file

for user in 'cat file'
do
echo " Do you want $user to be deleted"
read x
if ($x==y)
userdel -r $user
fi
done
never give up
Sivasingam Santhakumar
Frequent Advisor

Re: Scripting question

Thanks Stefan for a short and quick one. Thanks to others for spending time.
Gordon  Morrison
Trusted Contributor

Re: Scripting question

An even safer way is to just lock the dead accounts, rather than deleting them:

for user in `cat deadusers.txt`
do
passwd -l $user
done

They can then be re-activated later, if necessary, and all of their files will still be there if they're ever needed.

What does this button do?
Peter Godron
Honored Contributor

Re: Scripting question

Kumar,
also attached a script to identify dormant users i.e. users who have not logged on in the 30 days. May be useful in future
Regards