#!/usr/bin/expect -f

# eddie@dhcp-144:~$ passwd
# Changing password for eddie.
# (current) UNIX password:
# Enter new UNIX password:
# Retype new UNIX password:
# passwd: password updated successfully

if {[llength $argv] == 0} {
	send_user "Usage: change_pw \'old_password\' \'new_password\'\n"
	exit 1
}

set oldpass [lindex $argv 0]
set newpass [lindex $argv 1]

log_user 0
spawn passwd
expect {
	timeout { send_user "timed out\n"; exit 1 }
	eof { send_user "Old password incorrect.\n"; exit 1 }
	eof { exit 1 }
	"(current) UNIX password:"
}
send "$oldpass\r"
expect {
	timeout { send_user "timed out\n"; exit 1 }
	eof { send_user "Old password incorrect.\n"; exit 1 }
	"Enter new UNIX password:"
}
send "$newpass\r"
expect {
	timeout { send_user "timed out\n"; exit 1 }
	eof { exit 1 }
	"Retype new UNIX password:*"
}
send "$newpass\n"
#exp_internal 1
expect {
	"password updated successfully" { send_user "Password updated successfully\n"; exit 0 }
	-re "...$" { send_user [string trim $expect_out(buffer)]; send_user "\n" }
	timeout { exit 2 }
}
exit 3
