Librarium Whitehat
Blog Pics
"Inveniam viam aut faciam" : I will either find a way, or I shall make one

Adeptus-Mechanicus - BlogMe

Linux “expect” – making things easy

Recently I needed to do some work via pop3 that I just could not find the right tool for. I tried nail, I tried fetchmail, I tried popcheck, etc. But all of them were not quite what I wanted. So I returned to using expect to create a script. Now expect starts what you would call a session, and everything you type during that session is captured, and when the session is done, a file is created. This file will allow you to replay that action again without user interaction. So if you telnet to a router, and check something, expect can help.

Now it has it’s problems. (1) any passwords are stored in the file which is text, (2) the expect “replay” file can normally benefit from some tweaking, since by default it is exactly the original session and you may have time-related data that will cause problems. Reading through it is easy and will help you make it better.

The best way to generate this script is autoexpect. This is part of the expect package and makes life even easier. You type:

“autoexpect -f run-again.exp”

..this will start an expect session and when finished will generate it in the run-again.exp file. Simple.

Expect is a blunt tool – no doubt, but sometimes it can be a life-saver.

Published by erich, on April 12th, 2011 at 2:43 am. Filled under: linux Tags: No Comments