How to use pipeline in Terminal
The canonical unix tool for this job is expect.

Here is a short tutorial that should get you going on scripting a series of passwords to be entered when the appropriate prompt is detected by the tool.