You can use the expect tool.

Some examples are shown at: http://stackoverflow.com/questions/4...to-ssh-command