Operating System - HP-UX
1830898 Members
2823 Online
110017 Solutions
New Discussion

Re: change password without prompting

 
Hanry Zhou
Super Advisor

change password without prompting

Hi,

I want to use a script to change a user's password, but need to feed the command passwd a default password without prompting "new password?" and enter a password.

Can I use passwd command ,and just feed it a default password?

Or how do I replace the second field in /etc/passwd file with a default password? May be using "sed"?

Thanks,
none
6 REPLIES 6
Vitek Pepas
Valued Contributor

Re: change password without prompting

I don't think you can feed passwd command with password, you will have to type it in.
Replacing the password field in /etc/passwd will work (of course you have to encode your password first). If you want to do the replacenent automatically be careful to use some kind of pattern, so you don't change other user's passwords.
Always test such utility, if you wipe /etc/passwd out - you're in trouble.
Hanry Zhou
Super Advisor

Re: change password without prompting

Thanks for the message.

Can you please provide the detailed method to replace the 2nd field, ex, using sed?
none
A. Clay Stephenson
Acclaimed Contributor

Re: change password without prompting

You really don't want to try this with sed or awk --- there is far too much danger in winding up with a corrupt and unavailable system. This should really be done with 'C' using getpwent(), crypt(), and putpwent() but it can be done with Perl. Here is one module:

http://search.cpan.org/author/SSNODGRA/Unix-ConfigFile-0.06/PasswdFile.pm

Your idea of editing the passwd field within the passwd file directly is "Mickey Mouse" and does not take into consideration either trusted systems or those running NIS or NIS+. That is why more robust solutions revolving around 'C' routines are really the preferred method.

You MIGHT be able to use the expect utility to run the passwd command. Passwd reads the passwd on file descriptor 3.
If it ain't broke, I can fix that.
Ronelle van Niekerk
Regular Advisor

Re: change password without prompting

An expect script would work well for this.

You can either have a hard coded password (default) in the script itself or pass the the new password to the script as a parameter.

rm -r /it/managers
Vitek Pepas
Valued Contributor

Re: change password without prompting

Clay is ablolutely right, but if you really want to do it, try this:
awk -F: -v NAME= -v PASS= ' BEGIN {OFS=":"}
$1 == NAME {$2=PASS; print $0; next }
{ print $0 }
' /etc/passwd > /tmp/passwd.new
Of course has to be encrypted.
Pepe Jimenez Muñoz
Frequent Advisor

Re: change password without prompting

Expect is the way, like say Ronelle.

We use this script:

#!/usr/local/bin/expect -f
# wrapper to make passwd(1) be non-interactive
# username is passed as 1st arg, passwd as 2nd, yo
# lo modifique para que el 3 argumento fuese la clave antigua
#
# Este es el dialogo de HP-UX
# Old passwd:
# New password:
# Re-enter new password:

set newpassword [lindex $argv 1]
spawn /usr/bin/passwd [lindex $argv 0]
# expect "New password:"
expect "Nueva clave:"
send "$newpassword\r"
# expect "Re-enter new password:"
expect "Vuelva a introducir la nueva clave:"
send "$newpassword\r"
expect eof


Dialog has been changed for spanish users.

To call this script:

autopasswd username newpassword


You can call this script from a loop that read the username and password from a file (don??t forget delete this file).

First you need install expect language.

I hope this help you.
ppviso