?
Solved

change user password via scripting

Posted on 2003-03-02
17
Medium Priority
?
456 Views
Last Modified: 2013-12-16
I need to change the password of a linux user account.  Using passwd would accomplish the task.  However, I need to do it via a script(PHP, Perl, whatever).  The problem is that passwd prompts the user for information.  Is there a way I can provide all the information on one line so passwd will not ask me for additonal information?  Or does someone know how to write a script to counter act the passwd prompts?  If anyone could help me with this I would really appreciate it.  Thanks.
0
Comment
Question by:madhouse
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +5
17 Comments
 
LVL 38

Expert Comment

by:yuzh
ID: 8055216
You need to write an "expect" script to do the job.

use "expect" to handle the passwd, and make the passwd as a command line arg.

You need to have "expect" + TCL/TK  install on your system.

information about expect (including script example can be found):

http://expect.nist.gov/

Here's an example expect script for change the user passwd.
(assume that expect is installed in /usr/local/bin)

#!/usr/local/bin/expect -f
# script chpass
# useage: chpass username password

set username [lindex $argv 0]
set pwd [lindex $argv 0]

spawn /usr/bin/passwd $username
expect "New password: "
send "$pwd\n"
expect "Re-enter new password: "
send "$pwd\n"
expect eof

#==================================================

Note: you need to modify the expect strings as exactly show, in your system. just manually run :
passwd username
to verify it.

I hope this infor can help.







0
 
LVL 2

Expert Comment

by:jimbb
ID: 8055749
man newusers
man usermod (specifically, the "-p" option)

Does one of those do what you want?
0
 
LVL 8

Expert Comment

by:heskyttberg
ID: 8056617
Hi!

Otherwise this might be useful for you:
http://www.caliban.org/ruby/ruby-password.shtml

Regards
/Hans - Erik Skyttberg
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

Author Comment

by:madhouse
ID: 8058556
yuzh

I installed expect and ran the script that you gave me(with a few modifications).  And It didn't work.  I run a debian system.  When I type passwd username it prompts with :

"Enter new UNIX password: "
"Retype new UNIX password: "

So I changed the expect strings to look for those.  Anyway, when I ran the script it displayed the following:

#./autopasswd username somepass
#Enter new UNIX passwod: username
#Retype new UNIX password:
#

So what I can gather is that it's not getting the second password for some reason.  Any suggestions?  My modified file is as follows:

#!/usr/bin/expect -f
# script chpass
# useage: chpass username password

set username [lindex $argv 0]
set pwd [lindex $argv 1]

spawn /usr/bin/passwd $username
expect "Enter new UNIX password: "
send "$pwd\n"
expect "Retype new UNIX password: "
send "$pwd\n"
expect eof
0
 
LVL 8

Expert Comment

by:heskyttberg
ID: 8060937
Hi!

Well isn't your system prompting users for old password first, or do you intend to run script as suid root ?

Regards
/Hans - Erik Skyttberg
PS! I'd use the ruby/password tools instead. D.S.
0
 

Author Comment

by:madhouse
ID: 8060984
I am building a web admin.  The user will have already used their old password to login to their account.  Then they will have the option of changing it to whatever they wish.

Why should I use the ruby/password tools?  The only reason I liked yuzh's is because it seemed so easy.  Do you have a demo for ruby/password?
0
 
LVL 2

Expert Comment

by:jimbb
ID: 8061116
Not sure if this is suitable for you, but one of my favorite methods of dealing with that is to set the user's shell to /usr/bin/passwd, and then if they want to change their pass, they can log in via SSH and do it that way.  Authenticated, encrypted, etc.

Of course, they have to be able to handle an SSH client, but I think there are some Java-based ones that you could stick in a web page if normal clients are too complex.
0
 
LVL 38

Expert Comment

by:yuzh
ID: 8061767
You need you verify the screen prompt on your system:

at command try in:

passwd username

to see the exact message on your screen, and then modify your script.

If you want to let the use change their own password, then can simply use:
passwd

Cheers!
0
 

Author Comment

by:madhouse
ID: 8062625
My users are too dumb to change their own passwords.  That is why I am creating the web admin.

I already verified the screen prompt on my system.  It is :
"Enter new UNIX password: "
"Retype new UNIX password: "
like I stated above.
0
 
LVL 38

Expert Comment

by:yuzh
ID: 8062966
may be you should make the scipt to have no time out.

eg
#!/usr/bin/expect -f
# script chpass
# useage: chpass username password

set username [lindex $argv 0]
set pwd [lindex $argv 1]

spawn /usr/bin/passwd $username
expect -timeout -1 "Enter new UNIX password: "
send "$pwd\n"
expect -timeout -1 "Retype new UNIX password: "
send "$pwd\n"
expect -timeout -1 eof

#============================
0
 
LVL 38

Expert Comment

by:yuzh
ID: 8107604
how's it going?
0
 

Author Comment

by:madhouse
ID: 8107675
Sorry.  That didn't work either.  I don't know what the problem is.
0
 

Expert Comment

by:covati
ID: 8121211
madhouse, yush has you on the right track, but I think your best bet is to ditch his script and make your own, the easy way!

you can run autoexpect (I think that installs w/ expect)

from the man page:
autoexpect watches you interacting  with  another  program and creates an Expect script that reproduces your interactions. ... It is much easier to cut/paste hunks of autoexpect scripts  together than to write them from scratch.  And if you are a beginner, you may  be  able  to  get  away  with learning  nothing more about Expect than how to call auto-expect.

This should build you a script file that you modify slightly to suit your needs.

TIP: use the -p option with autoexpect to only look at the line the prompt is on. This solves the problem of dates or changing messages (in ftp for example) messing up your script.
0
 

Expert Comment

by:CleanupPing
ID: 9076659
madhouse:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 

Expert Comment

by:PARASAditya
ID: 12187731
Use
#echo "New Password" | passwd --stdin <username>
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 14302919
PAQed with no points refunded (of 80)

modulo
Community Support Moderator
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month13 days, 5 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question