passwd_expect
>
#! usr/bin/expect -f
>#
# An expect script to avoid interactively
># having to supply passwords for passwd(1).
# This program MUST be run as root, as it
># expects to be able to change the password
# without being prompted for the old password.
># It will not uninteractively set root's password
# (though that is easy to change, I suggest you
># don't).
#
># Author: David Hassett
# Date: 13/06/2000
># Version: 1.0
# Script name: passwd_expect
>#
# Notes: passwd is an interactive program,
># and as such, expects a small delay in the
# user responding. This is implemented with
># sleep statements. It was written around
# GNU passwd, but should be transferable to
># other passwd's by just changing the prompt
# strings to match your version.
>#
# This program is hereby placed in the public domain.
>#
>if { [llength $argv] < 2 } {
puts stderr "usage: $argv0 username password"
> exit 1
}
>
set USER [lindex $argv 0]
>set PASSWD [lindex $argv 1]
>if { ! [string compare $USER "root"] } {
puts stderr "$argv0: Cowardly refusing to set root's password!"
> puts stderr "$argv0: Use plain 'passwd' for this."
puts stderr "$argv0: Sorry... bailing out."
> exit -1
}
>
stty -echo
>spawn -noecho passwd $USER
>expect {
"New UNIX password: " {
> sleep 0.3
send "$PASSWD\r"
> exp_continue
}
> -re "BAD PASSWORD" {
puts stderr "\r$argv0: exited due to bad password"
> exit 2
}
> "Retype new UNIX password: " {
sleep 0.3
> send "$PASSWD\r"
exp_continue
> }
-re "successfully" {
> puts stderr "\r$argv0: operation completed successfully!"
exit 0
> }
-re "Unknown user name" {
> puts stderr "\r$argv0: exited due to unknown user"
exit 3
> }
default {
> puts stderr "\r$argv0: operation timed out or eof was reached"
exit 4
> }
}
>
# end of script
>