Saturday, August 11, 2012

Expect Programming

Automated process can be easily done by using crontab, but a new problem arises when you need to provide some credentials, such as user/password in order to execute some task, such as pulling git automatically, uploading files to FTP server, and many others.

With some use of expect programming, it can be easily be solved. Expect has three main commands:
  • spawn
  • expect
  • send
Spawn is used to execute a command or a file. Please note that all command that you wish to execute must be placed on a file and let spawn call this file. You should also use an absolute path or you will have problems when debugging it.

Next is expect. This command will try to compare what output does the process  gave us and compare it with some strings that you have provided in the script. When it matches, you can do something with it, for example give your users/password when there's a login prompt.

Last command is send, which is  obvious. It sends some strings to the process and it replaces our manual input. Don't forget to end the string with "\r" so that it emulates ENTER button being pressed.

See The Geek Stuff for more examples of expect programming.

1 comment: