For Programmers: Free Programming Magazines  


Home > Archive > AWK > December 2004 > editing the passwd file









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author editing the passwd file
warezcrc@hotmail.com

2004-12-03, 3:56 pm

Hi all,
I'm having trouble combining a ksh and awk script for editing the
passwd file.
I want to lookup a user in our corporate directory through a
ldapsearch, and if the user doesn't exist he/she will get a # in front
of their userid in passwd, thus disabling their userid for future
purge.
If someone has a solution to this problem i would be really grateful.
The small script uses arrays in awk to place and keep order of which
line and user I'm processing. Here's the script. Thankful for any help.



########################################
###################################=
########################################
###############
# This Script will check for a user in vcd.corporation.net thru a
ldapsearch if the user doesn't exist in the Corporate Directory
the userid will get a # in front of it in passwd
#! /bin/ksh
nawk 'BEGIN {FS =3D ":"} # Setting the FS =3D Field Separator for awk
{
for (i =3D 1; i <=3D NR; ++i) #This is where I keep track of which
linenumber I'm at in the passwd file
{
NR =3D=3D i
# print $1 # unneccesary line perhaps, since awk already knows
what value $1 is
$1 =3D currentuser # setting value of $1 for each NR to
currentuser

facts=3D$(/bin/ldapsearch -h vcd.corporation.net -B
uid=3Dcurrentuser fullName l departmentNumber mail telephoneNumber
Organisa
tion ou | tr "\303\205\266\251\244\245\226\204" "\000=C5=F6=E9=E4=E5=D6=C4")
# this is where the ldapsearch is being done

fullnAme=3D$(print "$FACTS" | grep fullName=3D | awk -F"=3D" '{print
$2}') # the
fullName variable is set
if
[[ -z $fullnAme ]]; then
userreplacement=3D`echo $currentuser |awk '{print"#"$1}'`
#this is where
the # is being made in front of the userid because
it doesnt exist in the Corporate Directory
perl -pi -e 's/$currentuser/$userreplacement/' $HOME/uid/passw
# replace USER
that doesnt exist in vcd with #USER in pass
wd, observe this is a local passwd copy
fi
}
}' $HOME/uid/passw
########################################
###################################=
########################################
##############

John L

2004-12-03, 8:55 pm


<warezcrc@hotmail.com> wrote in message news:1102090807.028306.53640@z14g2000cwz.googlegroups.com...
> Hi all,
> I'm having trouble combining a ksh and awk script for editing the
> passwd file.
> I want to lookup a user in our corporate directory through a
> ldapsearch, and if the user doesn't exist he/she will get a # in front
> of their userid in passwd, thus disabling their userid for future
> purge.


Rather than directly altering the passwd file, since
if anything goes wrong you might render your server
unusable, use a command like "passwd" or "usermod"
to lock or disable the account. A newsgroup devoted
to your operating system may have more details, or
comp.lang.shell for help with scripting. Essentially,
for each user, if ldapsearch fails: passwd -l

--
John.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com