Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I'm writing a shell script to change a user's password. I tried to use the passwd command, but that command always asks for the old and new passwords to be typed; it won't accept them from my script.

Is there a way to change the user's password in a shell script, with the old and new password coming from e.g., variables?

share|improve this question
1  
    
I've edited your question substantially in an attempt to write it in "proper" English. If I've lost your intent, please feel free to edit or revert my edit. –  derobert Jan 15 at 21:29

1 Answer 1

#!/usr/bin/expect -f
spawn passwd username
expect "New password:"
send -- "user-password\r"
expect "Retype new password:"
send -- "user-password\r"
expect eof

Testing

I saved it as script.exp and when I run the script, this is the output I get in my machine.

expect script.exp
spawn passwd ramesh
Changing password for user ramesh.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.

I did not had to type in the password using my keyboard. Of course, you could modify the function to further suit your requirements and use it the way you want.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.