#!/bin/bash
EXPECT=$(which expect)

if [ $# -ne 3 ]; then
    echo "Usage: $0 <username> <current password> <new password>"
    exit 2
fi

USER=$1
PASS=$2
NEW=$3

$EXPECT <<EOEXPECT
spawn su -c passwd $USER
expect "Password:"
send "$PASS\r"
expect "password:"
send "$PASS\r"
expect "password:"
send "$NEW\r"
expect "password:"
send "$NEW\r"
expect eof
EOEXPECT
