#!/usr/bin/expect
# Set password un-interactive
#
# call this passwdex <user> <passwd>
# Written by By Eko M. Budi,
# but this is so common all over the Internet

spawn passwd [lindex $argv 0]
set password [lindex $argv 1]

# the first password
expect "New password:"
send "$password\r"
expect {
  # If bad password, new password will be asked again
  "New password:" {
     send "$password\r"
     expect "Re-enter new password:"
     send "$password\r"
  }
  # second password
  "Re-enter new password:" {
     send "$password\r"   
  }
}
expect eof
