First of all, you need to install Expect. Expect is just like perl, but I don't believe it's installed by default in OS X. Anyway, you'll need tcl libraries for Expect too.
So once you have the software installed. O'Reiley has a very good book on how to write expect scripts. The thing...