'Expect script to change password using ssh

Can anyone help what is the problem with below script? It says password changed successfully, but password remains old password. I am running this script to change checkpoint SPLAT firewall expert password, which directly logins to expert mode using ssh.

#!/usr/bin/expect
set username admin
set oldpass sam$$$
set newpass abc.123

spawn ssh -l $username 192.168.1.10

expect "assword:"
send "$oldpass\r"
expect "# "
sleep 1
send "passwd\r"
expect "Enter new expert password:"
send "$newpass\r"
expect "Enter new expert password (again):"
send "$newpass\r"
expect eof"
expect "# "
send "exit\n"


[admin@localhost ~]$ ./test.sh
spawn ssh -l admin 192.168.1.10
[email protected]'s password:
Last login: Thu Oct 30 18:41:52 2014 from 192.168.1.5
[Expert@cpmodule]# passwd
Enter new expert password:
Enter new expert password (again):
Expert password has been changed


Solution 1:[1]

Check this, This worked for me.

#!/usr/bin/expect
set timeout 20
set user user2
set password abc123
set new bac234qwe.1
set ip localhost
spawn -noecho ssh -q -o StrictHostKeychecking=no "$user\@$ip" "passwd"
expect "assword:"
send "$password\r"
expect "*UNIX password:"
send "$password\r"
expect "New password: "
send "$new\r"
expect "Retype new password:"
send "$new\r"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sriharsha Kalluru