Operating System - HP-UX
1830220 Members
1991 Online
109999 Solutions
New Discussion

Script to update password field

 
SOLVED
Go to solution
Kong Kian Chay
Regular Advisor

Script to update password field

I have a file e.g idlist.txt which contains a list of login-names.

I would like to read idlist.txt and update the password field in /etc/passwd to a FIXED string e.g "abCdef".

Anybody knows how to do it ?

NOTE :
* The password field may contain "/" and "\". The script should be able to handle it. This is what stucks me.
6 REPLIES 6
Joseph C. Denman
Honored Contributor

Re: Script to update password field

I really do not know why you would want to do that????? The password field in /etc/passwd is encrypted. If you change it to abCdef, the user will not be able to log in. Why not just lock the accounts or remove the user???

But anyway, this is a way to do what you said you wanted???

for x in `cat idlist.txt'
do
var1=`cat /etc/passwd | grep ${x} | cut -f2 -d":"`
sed 's/${var1}/abCdef/' > /etc/passwd.out
mv /etc/passwd.out /etc/passwd
done

I would not do it though!!!!!!!

...jcd...
If I had only read the instructions first??
Praveen Bezawada
Respected Contributor
Solution

Re: Script to update password field

Hi
If you really want to replace the passwd field with a fixed text you can do

awk -F: '{ print $1":""text"":"$3":"$4":"$5":"$6":"$7 }' /etc/passwd > /etc/passwd.tmp

All the best

..BPK...
linuxfan
Honored Contributor

Re: Script to update password field

Hi Kong,

Before you try anthing make sure you keep a copy of you password file and have another terminal session open.

Now you could use something like this,

for id in `cat idlist.txt`
do
pass=`pwget -n id | cut -f2 -d":"`
sed "s/$pass/abCdef/" /etc/passwd > /etc/passwd.new
done

The reason i did use grep is because it won't work if you have users called test1 test12 etc.

Once you have verified the new /etc/passwd.new
move it to the /etc/directory.
make sure the permissions are ok.

-HTH
I am RU

They think they know but don't. At least I know I don't know - Socrates
Kong Kian Chay
Regular Advisor

Re: Script to update password field

Joseph & Ramesh

The "sed" command will FAIL when the password field contains special char like "/".

However, the "awk" command is perfect.
linuxfan
Honored Contributor

Re: Script to update password field

Hi Kong,

change the sed line to the following

sed "s#$pass#abCdef#" /etc/passwd /etc/passwd.new

This should work

-HTH
I am RU
They think they know but don't. At least I know I don't know - Socrates
linuxfan
Honored Contributor

Re: Script to update password field

Oops

that should be

sed "s#$pass#abCdef#" /etc/passwd > /etc/passwd.new

Obviously you do want to generate the new file right. ;)

-Regards
I am RU
They think they know but don't. At least I know I don't know - Socrates