© Kheng Ho Toh, 123RF.com
Using Expect scripts to automate tasks
Automatic
Expect is a natural and intuitive automation scripting language that operates in much the same way humans do when interacting with a system. You type in commands and expect a certain response to your command. When you receive the expected response, you enter another command and so on. Expect works in the same way, except you have to provide the script with commands and expected responses to those commands. Basically, you have to script out the entire two-way "conversation."
You can think of an Expect script as a dialog script written for two actors: a sender and a receiver. One of the more popular activities to automate is an SSH session between two hosts, in which one host is the sender (local host) and the other is the receiver (remote host). Being able to emulate every keystroke and create a true interactive session between two systems via a script is an exciting proposition.
Expect Setup
Most Linux distributions include Expect [1] as part of the available and installable software packages. In other words, you won't have to download and install from source code. Use your system's package manager to download and install Expect and any required dependencies or associated packages. For example:
$ sudo yum install expect
or
$ sudo apt-get install expect
Once you have Expect installed, you can begin writing scripts.
Creating an Interactive SSH Session
You must provide both sides of the conversation in your script because you're setting up an interactive system. Look at a few essential items before diving right into a script.
To make an Expect script executable as a standalone program, you must do two things: Make the script executable, and supply the path to the script for expect.
Buy this article as PDF
(incl. VAT)
Buy ADMIN Magazine
Subscribe to our ADMIN Newsletters
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Most Popular
Support Our Work
ADMIN content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.

