Operating System - HP-UX
1833356 Members
3092 Online
110051 Solutions
New Discussion

Find and replace text in a passwd file....

 
SOLVED
Go to solution
Jonathan Caplette_1
Super Advisor

Find and replace text in a passwd file....

Hi guys,

I wanna do a script to manage my users and I want to have a function that can deactivate user by replacing in the /etc/passwd file the crypted password with the * caracter...

Example:

User1:e94xsM0XKbu4E:367:230:User1 user:/home/User1:/bin/ksh

I want to replace "e94xsM0XKbu4E" by "*" to make the line in the file look like that:

User1:*:367:230:User1 user:/home/User1:/bin/ksh

I know that I can use sed to do that, but I don't clearly understand how it work?? Can somebody help me with that??

Thanks
Jonathan
10 REPLIES 10
Chris Wilshaw
Honored Contributor
Solution

Re: Find and replace text in a passwd file....

If you're on HP-UX 11.* you can simply use

passwd -l

to lock the account in this way.
Stefan Farrelly
Honored Contributor

Re: Find and replace text in a passwd file....


cat /etc/passwd | grep User1 | sed 's/e94xsM0XKbu4E/*/' >/etc/passwd.new

mv /etc/passwd.new /etc/passwd
chmod 444 /etc/passwd


Sed simply looks for text (within the '/') and replaces it with the 2nd field (the '/*/').
Im from Palmerston North, New Zealand, but somehow ended up in London...
Ian Dennison_1
Honored Contributor

Re: Find and replace text in a passwd file....

In truth, what sed is generally used for is searching a file and replacing characters, creating a new file which is then written back over the original.

In this case,...

cat /etc/passwd |awk -vuser=$USER -F":" '$1 == user {$2="*"}{print $1":"$2":"$3":"$4":"$5":"$6":"$7}' >/etc/newpasswd
cp /tmp/newpasswd /etc/passwd

This script tested and approved for human consumption. Share and Enjoy! Ian
Building a dumber user
James R. Ferguson
Acclaimed Contributor

Re: Find and replace text in a passwd file....

Hi:

Editing the 'etc/passwd' directly isn't the safest mode in which to operate. You can easily accomplish what you want with:

# passwd -r files -l User1

This locks the account. In an untrusted system this puts a "*" in teh encrypted password field.

Regards!

...JRF...
Jean-Louis Phelix
Honored Contributor

Re: Find and replace text in a passwd file....

hi,

cat /etc/passwd | sed 's;^\([^:]*\):[^:]*:\(.*\)$;\1:*:\2;'

will do it

- ^\([^:]*\): defines \1 as all chars from begining to first ':'
- [^:]*: defines password fields as all char up to next ':'
- \(.*\)$ defines \2 as all char up to end of line
- \1:*:\2 replace pattern by \1:*:\2

HTH

Jean-Louis.
It works for me (© Bill McNAMARA ...)
Jean-Louis Phelix
Honored Contributor

Re: Find and replace text in a passwd file....

Hi,

Reading other's posts I must agree that it's not really the safest way of doing it ... But it shows you what sed and awk can do anyway :-)

Regards.
It works for me (© Bill McNAMARA ...)
Jonathan Caplette_1
Super Advisor

Re: Find and replace text in a passwd file....

Haha!! Chris you're right!! I should have thought about that!! LOL!! passwd -l will do it!!!

BTW thanks all, I'll be able to use sed now!!!

ciao!
H.Merijn Brand (procura
Honored Contributor

Re: Find and replace text in a passwd file....

If you don't understand how it works, my advice would be: don't.

If you ruin your password file, you'll disable your system, and you do not want that to happen!

a5:/tmp 113 > diff passwd /etc/passwd
a5:/tmp 114 > perl -pi -e'BEGIN{($usr,@ARGV)=(@ARGV,"passwd")}s/^$usr:[^:]+:/$usr:*:/' broomer
a5:/tmp 115 > diff passwd /etc/passwd a5:/tmp 116 > perl -pi -e'BEGIN{($usr,@ARGV)=(@ARGV,"passwd")}s/^$usr:[^:]+:/$usr:*:/' gert
a5:/tmp 117 > diff passwd /etc/passwd 18c18
< gert:*:201:200:G. Hoff,,26,0220717600:/u/usr/gert:/pro/bin/tcsh
---
> gert:4k3K1WTnC3SAg:201:200:G. Hoff,,26,0220717600:/u/usr/gert:/pro/bin/tcsh
Exit 1
a5:/tmp 118 >

Enjoy, have FUN! H.Merijn [don't break your sys]
Enjoy, Have FUN! H.Merijn
Caesar_3
Esteemed Contributor

Re: Find and replace text in a passwd file....

Hello!

You wanna lock user so don't do it by replace
the encripted passwd to "*" use option of passwd -l

That's do the job well.

Caesar
Michael Kelly_5
Valued Contributor

Re: Find and replace text in a passwd file....

Jonathan,
you can try the following with awk

nullify_pw() {
rm /tmp/passwd-new >/dev/null 2>&1
cat /etc/passwd | gawk -v USER=$1 '
BEGIN {
FS = ":"
OFS = ":"
}
{
if ($1 == USER) {$2 = "*"; print
} else {
print
}
}' >>/tmp/passwd-new
cp /tmp/passwd-new /etc/passwd
}

and call nullify_pw with the username.

HTH,
Michael
The nice thing about computers is that they do exactly what you tell them. The problem with computers is that they do EXACTLY what you tell them.