PDA

View Full Version : Change password via bash script


tmw
May 31st, 2009, 21:39
Hello,
I have a question how can i change user password in bash script, this doest work (i run this script as root):


#!/usr/local/bin/bash
$(passwd foo) <<EOF
pass
pass
EOF


Is it possible to do this via bash script?

vivek
June 1st, 2009, 01:29
Read pw command man page and look for -h FD option. Add user called foo and set password to BAR
echo BAR | pw add user foo -h 0
To modify password for existing user called tom:
echo jerry | pw mod user tom -h 0

tmw
June 1st, 2009, 07:35
Thx for this. But i need this via passwd :/. In other words i need to type a password via script. But script must be non parametrs.

tmw
June 1st, 2009, 08:43
Ok, i got answer. I must use expect for this :).

rhyous
June 8th, 2009, 05:59
Post an example of your script when you are done so it can be available for others.

tmw
June 20th, 2009, 23:56
Hi, sorry for a big delay. This script will set password by passwd without user interaction:


#!/usr/local/bin/bash
LOG=tomek
PASS=haslo1
expect << EOF
spawn passwd $LOG
expect "New Password:"
send "${PASS}\r"
expect "Retype New Password:"
send "${PASS}\r"
expect eof;
EOF


This will set password "haslo1" for user "tomek".

VictorM
December 29th, 2009, 02:21
tnx for the script - I could use this to deploy NAS boxes unattended, smart trick...