Operating System - HP-UX
1834471 Members
2368 Online
110067 Solutions
New Discussion

Re: How to change passwords in shellscript?

 
Kento Uno
Occasional Contributor

How to change passwords in shellscript?

Hi, there!

How can i change password in shellscript.
I wrote following script, and execute it by root. But it doesn't work.

useradd -u 5001 -g 800 -m -k /etc/skel user01
passwd user01 newpasswd newpasswd

Regards,
KU
12 REPLIES 12
Scott Van Kalken
Esteemed Contributor

Re: How to change passwords in shellscript?

As far as I know, passwd is an interactive thing in that it needs input.

You may be able to use expect, but I'm not sure how it would work with passwd.

Scott.
Patrick Wallek
Honored Contributor

Re: How to change passwords in shellscript?

Here is what I did:

cp /etc/passwd /etc/passwd.sed
sed -e "s/$zid:\*:/$zid:,..:/" < /etc/passwd.sed > /etc/passwd

Now what that does is matches the $zid:*: (where $zid is the user name - ours are z12345 for example) in the passwd file and replaces it with $zid:,..: The ,.. forces the user to change their passwd the the next time they login.
It takes the passwd.sed file as input and output the passwd file. It will only modify the line that matches the $zid:*: and nothing else.

This is the only thing I could come up with that would work. I researched passwd command and the useradd command to try to come up with something else and couldn't figure anything out.
A. Clay Stephenson
Acclaimed Contributor

Re: How to change passwords in shellscript?

Hi:

There is not solution in pure shell but if you know a little perl, it's very easy. The Unix::PasswdFile module which is available for www.perl.com/CPAN has everything you need.

The other method is just a few lines of C.

Clay
If it ain't broke, I can fix that.
Animesh Chakraborty
Honored Contributor

Re: How to change passwords in shellscript?

Hi,
There is a way.

See the script by Robin
http://forums.itrc.hp.com/cm/QuestionAnswer/1,11866,0xbdb6cf38d6bdd5118ff10090279cd0f9,00.html


Best of luck
Animesh
Did you take a backup?
Sridhar Bhaskarla
Honored Contributor

Re: How to change passwords in shellscript?

It's not possible to do it with shell programming.

But it is possible with a public software called "expect". YOu can download it from

http://hpux.asknet.de/hppd/hpux/Tcl/expect-5.31/

Once you get it loaded, you can use this simple script to change the password

#!/wherever/expect -f

set timeout 10

spawn passwd user
expect "*sword:"
send "some_password\r"
expect "*sword:"
send "some_password\r"
expect "$" #or whatever the prompt
send "exit\r"


You can automate any "interactive" program using expect. Check the dependencies before installing expect.

-Sri

You may be disappointed if you fail, but you are doomed if you don't try
Bernie Vande Griend
Respected Contributor

Re: How to change passwords in shellscript?

The best method is to use sed as is outlined above. The part that is missing though is to first take a plain text password and convert to encypted form.
The attached perl script should help, you'll have to edit the path to where your perl is.
Ye who thinks he has a lot to say, probably shouldn't.
Bernie Vande Griend
Respected Contributor

Re: How to change passwords in shellscript?

The best method is to use sed as is outlined above. The part that is missing though is to first take a plain text password and convert to encypted form.
The attached perl script should help, you'll have to edit the path to where your perl is.
Ye who thinks he has a lot to say, probably shouldn't.
Travis Rebok
Advisor

Re: How to change passwords in shellscript?

Clay -
I am trying to do the same thing (find a batch way to change multiple unix passwords). I created a Perl script and used Unix::PasswdFile module to change multiple passwords in /etc/passwd test file and it seems to work. But I was wondering if you knew how Perl was actually changing the password behind the scenes. I tried looking in the Perl and C source code for the module and the 5.6.1 version, but I can't seem to find what unix or C commands Perl is ultimately using to replicate the "passwd" command (we would like to find out so we don't have to use Perl and can call the unix or C functions directly). Do you know how Perl is doing this or where in the Perl source code I could find out?

Also you mention on this and another post about coding a solution in C. Do you have any examples or know what C functions to use?

Will the Perl or C solution work if I am using a trusted environment or shadow file (since Perl looks like it is updating /etc/passwd directly)? If not how can I get the shadow file updated? My guess is that there has to be some way since the "passwd" interactive command itself has a way to update the shadow file. Any help is appreciated.
Thanks -
Travis
A. Clay Stephenson
Acclaimed Contributor

Re: How to change passwords in shellscript?

Hi:

The non-trusted library functions you need to examine are getpwent() and putpwent(). Man getpwent and putpwent for details. If you use NIS there is also a yppasswd() function.

The trusted equivalents are getprpwent() and putprpwent(). Man those entries as well.

If it ain't broke, I can fix that.
Travis Rebok
Advisor

Re: How to change passwords in shellscript?

Hi.
Thanks for the quick response. I man'd the functions and they look like what I would need to write a C pgm. I will continue to look at them.
I grepped for them in the Perl source code to see if that's what Perl is using and I did find getpwent() and setpwent(), but no putpwent(). So I don't yet know how Perl is ultimately updating the file.
Would you say it is worth the effort to write a C pgm or just go with the Perl implementation for not-trusted env (it doesn't look like the Perl supports trusted so I would have to go with C pgm if I needed that)?
Thanks again.
A. Clay Stephenson
Acclaimed Contributor

Re: How to change passwords in shellscript?

Hi again:

If it were me, I would do the C version. The other piece of the puzzle is how to do the hashed passwd. Passwords are actually hashed rather than encrypted so that the process is not reversible.

To create a passwd:
Choose a random 2 character string from the set
[./A-Za-z0-9] this becomes the 'salt'. You then call the crypt() function like this:
char *hash,salt[3] = {"x9"},plaintext[16] = {"top_secret"};

hash = passwd(plaintext,salt);

hash points to a composite made up of the salt + the hashed key.

Man 3 crypt for details.


If it ain't broke, I can fix that.
Travis Rebok
Advisor

Re: How to change passwords in shellscript?

Clay -
I'm not sure if you are still following thread
http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x1b970cb17a32d5118fef0090279cd0f9,00.html
but on that thread you offered the author "C" code that you developed as the repacement code for yppasswd command. Could you send me a copy? As you suggested I created a C pgm with calls to non-trusted functions and it is working fine, but we are running NIS so I am afraid that I cannot just update the local /etc/passwd file, but I need an equivalent to yppasswd instead.
Thanks again.