Operating System - HP-UX
1833863 Members
1932 Online
110063 Solutions
New Discussion

Re: changing /etc/passwd with sed & awk

 
SOLVED
Go to solution
Andy McDade
Advisor

changing /etc/passwd with sed & awk

I have been trying to replace the encrypted password in the passwd file using sed.
I have a small c program which will generate an encrypted password from a plain text string, but if the encrypted sting has " /" character in it, it upsets sed.
Can anyone point me in the direction of a bit of c or perl code which will simply replace field $2 in the /etc/passwd file with a supplied string, and which doesn't worry about any dodgy characters in the string?
Thanks
8 REPLIES 8
James R. Ferguson
Acclaimed Contributor
Solution

Re: changing /etc/passwd with sed & awk

Hi Andy:

You can simply use another character, like "%" in lieu of the "/" in your 'sed' expression.

Regards!

...JRF...
Carlos Fernandez Riera
Honored Contributor

Re: changing /etc/passwd with sed & awk

THE UNSUPPORTED !!!

/usr/sam/lbin/usermod.sam -p "string" user

unsupported
Andy McDade
Advisor

Re: changing /etc/passwd with sed & awk

Oh that was superb! You learn something new everyday!
Thanks
Rita C Workman
Honored Contributor

Re: changing /etc/passwd with sed & awk

Here's another:
I wanted to change just 'certain' users, so an HP chap (Liam) & I came up with this:

1. Take a dummy account and key in the new password to get the
encrypted password...

Here's the basic script we tested with:

newpass=""
cat /etc/passwd | while read line #read /etc/passwd line by line
do
echo $line | grep -q '^[A-Z][A-Z]*:[^:]*,3\.\.\.:' #see if it
matches the criteria
if [ $? -eq 0 ]
then #now replace old passwd for new passwd and print line
username=`echo $line | cut -d: -f1`
therest=`echo $line | cut -d: -f 3-`
echo $username:$newpass:$therest
else #print unmodified lines
echo $line
fi

You could just remove the extraneous logic or adjust it...but for me it changed only the users I wanted and left the rest in tact. And it doesn't care what characters are in the password...

Share & Enjoy,
Rita

don
Christophe MAILHE
Frequent Advisor

Re: changing /etc/passwd with sed & awk

I use myself the attached C program which is working on none trusted HPUX systems.

Hope this help!

Chris.
Christophe MAILHE
Frequent Advisor

Re: changing /etc/passwd with sed & awk

Hi Andy,

Something I forgot about chpass.c, you need to compile it using GNU C or an ANSI C compiler.

This is not working with the native C compiler included within HPUX 11.x.

You can find the GNU C compiler in the Softare Porting and Archive for HPUX at http://hpux.cs.utah.edu/

Alternatively, I attached a compiled version for HPUX 11.x 32bits.

Chris.
Andy McDade
Advisor

Re: changing /etc/passwd with sed & awk

Thanks yeah, I worked out fairly quickly that it needed an ANSI c compiler. I just installed it from the latest HP Application CD.
Thanks by the way, it works well.
Andy
Jack Werner
Frequent Advisor

Re: changing /etc/passwd with sed & awk

I certainly hope you are changing a "copy" of the /etc/passwd file, rather than /etc/passwd itself. If perchance something unanticipated happened you and your users could be up the proverbial creek sans paddle. After making changes to the copy, you can use sdiff -s to analyze the differences and make sure this is what you expected/wanted.
i'm retired