You can use the expect tool.

Some examples are shown at: http://stackoverflow.com/questions/4780893/use-expect-in-bash-script-to-provide-password-to-ssh-command