#!/usr/local/bin/expect -f # Password change shell script, tested on Linux and FreeBSD # ---------------------------------- # It need expect tool. If you are using Linux use following command # to install expect # apt-get install expect # FreeBSD user can use ports or following command: # pkg_add -r -v expect # ---------------------------------- # If you are using linux change first line # From: #!/usr/local/bin/expect -f # To: #!/usr/bin/expect -f # ----------------------------------------------- # Copyright (c) 2006 nixCraft project # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------- # This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ------------------------------------------------------------------------- # display usage if {$argc!=2} { send_user "usage: $argv0 username password \n" exit } # script must be run by root user set whoami [exec id -u] if {$whoami!=0} { send_user "You must be a root user to run this script\n" exit } # set timeout -1 match_max 100000 # stopre password set password [lindex $argv 1] # username set user [lindex $argv 0] # opem shell spawn $env(SHELL) # send passwd command send -- "passwd $user\r" expect "assword:" send "$password\r" expect "assword:" send "$password\r" send "\r" expect eof
Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our email newsletter to make sure you don't miss a single tip/tricks.
- Download Script
- Email this to a friend
- Rss Feed
- Last Updated: 04/10/08
{ 4 comments… read them below or add one }
Long story short.
echo $password | /usr/bin/passwd –stdin user1
Um, yea Tamilan – that doesn’t actually work. Did you try it yourself? All it does on my machine is remove a shell from the test user and not change the password.
The problem I’m running into with the above script is that it isn’t properly returning to the cli when done. In fact it is a pain in the ass to run within another script.
chpasswd is your friend:
echo username:password | chpasswd
You can even do
cat passlist.txt | chpasswd
where passlist.txt is a newline delimited list of username:password pairs.
All of these are bad ideas. You do not want your password being processed via the shell. Most shells keep a history file of commands executed – and this will show up in them. In general, you do not want your password saved in cleartext anywhere, regardless of file system controls.