Expect

Expect is another scripting language that helps solve a different type of problem. Where Perl’s strength is in pattern matching, Expect provides a way to automate interactive applications. We usually use Expect to imitate user sessions on a router to automate command-line-based tasks.

The Expect program is able to send one or more lines of output, such as router commands, and capture the results. And it can also react to whatever the router sends it in return. This could be as simple as sending a user ID and waiting for the password prompt, or you could use this feature to check for various error conditions and react appropriately.

We often write scripts in Expect to automate boring repetitive tasks. Computers are good at these tasks, people aren’t. People make typos and get bored or blink and miss key pieces of information. Also, because Expect can react immediately to the router’s responses, the script can generally execute a series of commands very quickly. We think it’s better to spend your time doing something productive while the computer is logging into all of your routers to do show commands. Expect lets us do this.

Expect is free to download, distribute, and use for any purpose. There are both Unix and Windows versions, and there are even companies doing commercial support for Expect. We wrote and tested all of the scripts in this book using Expect Version 5.31.2 on a Unix platform.

You can download Expect from the official web page at http://expect.nist.gov/. This site ...

Get Cisco IOS Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.