1830892 Members
2945 Online
110017 Solutions
New Discussion

script for /etc/password

 
SOLVED
Go to solution
A Sassi
Advisor

script for /etc/password

Hi all,

I am new at scripting and need help??
I need to modify /etc/password file and compare the username to the username in a flat file if it finds it then replace the comments field in /etc/pasword and populated from a flat file with this string Ex. "C-137h 897 Abu Sassi (HP)".

Thanks for your help in Advance
19 REPLIES 19
James A. Donovan
Honored Contributor

Re: script for /etc/password

I would think something like this, using the chfn command would work:

#!/bin/sh
for name in $(cat mynamefile); do
grep $name /etc/passwd >/dev/null 2>&1
if [ $? ]; then
echo "Modifying $name"
/usr/bin/chfn $user >/dev/null 2>&1 <<-!
$name
C-137h 897 Abu Sassi (HP)
none
none
!
fi
Remember, wherever you go, there you are...
A Sassi
Advisor

Re: script for /etc/password

Hi Jim
Thanks for the script. I am still not clear on the text string. I don't want to enter the strings manually; I need the script to pull the info from the flat file. I also not sure if the $user defined in your script.

Thanks for your response
Abu
James A. Donovan
Honored Contributor

Re: script for /etc/password

Sorry, $user is a typo..meant to put $name there...

With the script I posted, you wouldn't be entering anything manually. What does your flat file look like?

e.g.

user1:some comments about user1
user2:some comments about user2

then the script would look like....

#!/bin/sh
for LINE in $(cat mynamefile); do
name=$(echo $LINE|awk -F":" '{print $1}')
comment=$(echo $LINE|awk -F":" '{print $2}')
grep $name /etc/passwd >/dev/null 2>&1
if [ $? ]; then
echo "Modifying $name"
/usr/bin/chfn $name >/dev/null 2>&1 <<-!
$comment
none
none
none
!
fi
Remember, wherever you go, there you are...
Sridhar Bhaskarla
Honored Contributor
Solution

Re: script for /etc/password

Hi,

I thought my post was missing as it happened quite a few times with this site. Looks like you have a duplicate thread here where you can see my response.

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=450853

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
James A. Donovan
Honored Contributor

Re: script for /etc/password

one more change...for loop needs to change to a while loop...

for LINE in $(cat mynamefile);do

becomes

cat mynamefile|while read LINE; do
Remember, wherever you go, there you are...
rmueller58
Valued Contributor

Re: script for /etc/password

Are you just changing one field in the file or the entire record??
rmueller58
Valued Contributor

Re: script for /etc/password

Are you appending the entire comment field?

I'd use an perl or sed and awk script to parse out the fields and redefine the text.. Make sure no one is in SAM changing a user at the time or you'll be jacked..

created a backup of your /etc/passwd file.

If you Want to read STDIN into the comment field via interaction I'd do something like this..


I do like SRI's version.. I will have to try it myself. I do something a bit different on a user by user basis..

echo "Enter User Name:"
read username
grep $username /etc/passwd
export comment `grep $username /etc/passwd |awk -F: '{print $5}'`
echo "Enter New Comment information:"
read comment2
perl -p -i -e s/$comment/$comment2/g /etc/passwd



A Sassi
Advisor

Re: script for /etc/password

I applogies, I did not mention that the script is used on Solaris system. The chfn command only in HPUX. Tried man on Solaris but did not work.

Sridhar Bhaskarla
Honored Contributor

Re: script for /etc/password

Hmmm. This is HP's forums and by default we assume that you are running it on HP-UX server.

On solaris, look at 'passmgmt' man page for more information. I believe you may be interested in "-c" option. But some solaris gurus here can confirm it.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
A Sassi
Advisor

Re: script for /etc/password

Hi Jim,

Thanks a lot for the script I am close to getting this working.
Here is the script with a little changes.

Name of the Flat file is infile and looks like that

test: â comment comment
test2: â comment comment
test3⠦⠦

Name of the script file is idmod2:
!/bin/sh
`cat /export/home/infile`|while read LINE;do
name=`echo $LINE|awk -F":"'{print $1}'`
GECOS=`echo $LINE|awk -F":"'{print $2}'`

grep $name /etc/passwd >/dev/null 2>&1
if [ $? ];then
echo "Modifying $name"
usermod -c $name >/dev/null 2>&1 <<-!
$GECOS
none
none
none
!
fi
done


When I compile it, I get this error:
./idmod2 : test : â comment : not found

It looks that the script is working ok but there is a problem I canâ t but my hand
A Sassi
Advisor

Re: script for /etc/password

Sorry the text is not clear

test: "comment comment
test2: "comment comment

also the error is

./idmod2 : test : "comment : not found

I hope this text will show up ok

Thanks
Sridhar Bhaskarla
Honored Contributor

Re: script for /etc/password

Hi,

The script may not work completely due to following reasons.

1. If the /etc/passwd has users like user11, user100 and if your flat file has user1, then "grep $user /etc/passwd" is going to fail. So, you would want to put

grep -q "^${user}:" /etc/passwd

2. You will need to test $? against 0. You can eliminate > /dev/null 2>&1 with grep -q

grep -q "^${user}:" /etc/passwd
if [ $? = 0 ]
then
..

Look at my other script and instead of chfn, try your usermod command. A side note, 'usermod' will not work if the user is active atleast on HP systems. See if passmgmt works for you.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Sridhar Bhaskarla
Honored Contributor

Re: script for /etc/password

Hi,

The end quote " is missing. You don't need to have it

user1: some comments
user2: some comments

is enough.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
rmueller58
Valued Contributor

Re: script for /etc/password

On Linux you can use usermod

the syntax is

usermod -c "NEW COMMENT" username

as is

usermod -c "Joe Blow" jblow

A Sassi
Advisor

Re: script for /etc/password

#!/usr/bin/ksh
PASSWD=/etc/passwd
MYFILE=/export/home/infile

While read LINE
do
USER=$(echo $LINE|awk '{FS=":";print $1}')
GECOS=$(echo $LINE|awk '{FS=":";print $2}')

printf "%-20s""Doing for $USER"
grep -q "^${USER}:"$PASSWD
if [ $? = 0 ]
then
usermod -c $USER < /dev/null 2>&1
none
none
none
$GECOS
EOF

Print "- Done\n"

else

printf "-N/A\n"
fi

done < $MYFILE

This is your script I ran it against the infile and I got errors. I am not sure what is the problem with those 2 lines.

# ./idmod3
./idmod3[5]: While: not found
./idmod3[6]: syntax error at line 7 : `do' unexpected
#
A Sassi
Advisor

Re: script for /etc/password

By the way SRI. I tried your suggestions on Jim's Script but still getting the error.

# ./idmod2
./idmod2: test3:comment: not found
#

Abu
James A. Donovan
Honored Contributor

Re: script for /etc/password

Unix is case-sensitive, change "While" to "while"
Remember, wherever you go, there you are...
Sridhar Bhaskarla
Honored Contributor

Re: script for /etc/password

While? With Uppercase "W"? If so, change it to lowercase.

-Sri

PS: I think I have enough points already. No more points please.
You may be disappointed if you fail, but you are doomed if you don't try
A Sassi
Advisor

Re: script for /etc/password

I appreciated all the efforts from Jim, SRI and Rex. The Script file worked with little modifications. Thanks again guys.. You Guys deserve 10 in my book. Hey SRI, I could give more points if you like.. Just kidding

Here is the script

# cat idmod
#!/usr/bin/ksh
PASSWD=/etc/passwd
MYFILE=/export/home/infile

while read LINE
do
USER=$(echo $LINE|awk '{FS=":";print $1}')
GECOS=$(echo $LINE|awk '{FS=":";print $2}')

printf "%-20s""Modifying $USER"
/usr/bin/grep -nli "^${USER}:" $PASSWD
if [ $? = 0 ]
then
usermod -c $GECOS $USER < /dev/null 2>&1
none
none
none
$GECOS
EOF

echo "- Done\n"

else
echo "- Unable to Perform Your Request\n"
fi

done< $MYFILE
#