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

spawn passwd
set old_pwd [lindex $argv 0]
set new_pwd [lindex $argv 1]

# the first password
expect "Old password:"
send "$old_pwd\r"
expect {
  # bad old password
  "unchanged." {
     exit 1
  }  
  "New password:" {
     send "$new_pwd\r"
     expect {
        # bad new password
	"New password:" {
	   exit 2
	}
        "Re-enter new password:" {
           send "$new_pwd\r"
        }
     }
  }
}
expect eof
