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-07, 3:59 am

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-07, 3:59 am


<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.


warezcrc@hotmail.com

2004-12-07, 3:59 am

Thank you for the tip John, I'll check out comp.lang.shell and os
related newsgroup also,
Thanx again!

warezcrc@hotmail.com

2004-12-08, 8:55 am

Hi all!
If ayone needs something similar , please feel free to use this.
########################################
###################################
#!/bin/ksh

#This script will check for a user in corporate.directory.net , if the
user doesn't exist
#the user will get a # in front of the user id in passwd, thus
disabling it,
#this is done for future purge of userid and homedirectory,
########################################
###################################
rm /tmp/sedtmp
echo "s/ justacreationofsedtmp / justacreationofsedtmp /" >
/tmp/sedtmp
for user in $( awk -F":" '{getline}{print $1}' /etc/passwd ); do #all
uid's
exception=3D$(awk -F: '{print $1}' /$HOME/uid/exception | grep -w
$user) #exception id check
if [[ $user !=3D "$exception" ]]; then #do a search
facts=3D$(ldapsearch -h corporate.directory.net -B
uid=3D$user fullName |tr "\303\205\266\251\244\245\226\204"
"\000=C5=F6=E9=E4=E5=D6=C4")
fullnAme=3D$(print "$facts" | grep fullName=3D | awk -F"=3D"
'{print $2}') #fullName variable is set
if
echo $fullnAme
[[ -z $fullnAme ]]; then #put the # in front of user if
fullname dtring empty
replaceMe=3D$(echo $user |awk '{print"#"$1}')
#adding # in front of the userid with sed script file
print "s/"$user"/"$replaceMe"/" >> /tmp/sedtmp
else
continue
fi
else
continue
fi
done
sed -f /tmp/sedtmp /etc/passwd | sort > $HOME/uid/passwddd # finally
apply the desired userid changes with sed.
########################################
##########################

warezcrc@hotmail.com

2004-12-10, 8:55 pm

Hi all!
If ayone needs something similar , please feel free to use this.
########################################
###################################
#!/bin/ksh

#This script will check for a user in corporate.directory.net , if the
user doesn't exist
#the user will get a # in front of the user id in passwd, thus
disabling it,
#this is done for future purge of userid and homedirectory,
########################################
###################################
rm /tmp/sedtmp
echo "s/ justacreationofsedtmp / justacreationofsedtmp /" >
/tmp/sedtmp
for user in $( awk -F":" '{getline}{print $1}' /etc/passwd ); do #all
uid's
exception=3D$(awk -F: '{print $1}' /$HOME/uid/exception | grep -w
$user) #exception id check
if [[ $user !=3D "$exception" ]]; then #do a search
facts=3D$(ldapsearch -h corporate.directory.net -B
uid=3D$user fullName |tr "\303\205\266\251\244\245\226\204"
"\000=C5=F6=E9=E4=E5=D6=C4")
fullnAme=3D$(print "$facts" | grep fullName=3D | awk -F"=3D"
'{print $2}') #fullName variable is set
if
echo $fullnAme
[[ -z $fullnAme ]]; then #put the # in front of user if
fullname dtring empty
replaceMe=3D$(echo $user |awk '{print"#"$1}')
#adding # in front of the userid with sed script file
print "s/"$user"/"$replaceMe"/" >> /tmp/sedtmp
else
continue
fi
else
continue
fi
done
sed -f /tmp/sedtmp /etc/passwd | sort > $HOME/uid/passwddd # finally
apply the desired userid changes with sed.
########################################
##########################

Sponsored Links







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

Copyright 2008 codecomments.com