$command = "/usr/local/bin/expect -f chgpwd.expect";$command = $command . " $username $current_password $new_password1 $new_password2";print "Executing command \'$command\'";@result = `$command`;print "@result";
# To execute this script use:## ./chgpwd.expect ### Start the passwd program, giving it the username to change#spawn /bin/passwd [lindex $argv 0]## If the passwd binary is ok, send their login passwd#expect { "not found" { send_error "Unknown passwd command\n" exit } "does not exist" { send_error "User [lindex $argv 0] does not exist\n" exit } "Enter login password: " { send "[lindex $argv 1]\r" }}## If their current password is ok, send the new password#expect { "wrong passwd" { send_error "The password you gave as your current password is wrong\n" exit } "New password: " { send "[lindex $argv 2]\r" }}## If their new password is ok, send the confirmation password#expect { "Password too short" { send_error "The new password is too short. Must be at least 6 characters\n"; exit } "Re-enter new password: " { send "[lindex $argv 3]\r" }}## If the confirmation password is ok, exit#expect { "They don't match" { send_error "The confirmation password doesn't match the new password\n"; exit } eof { send_tty "Password successfully changed\n" }}