Using Expect scripts to automate tasks

Slashdot it! Delicious Share on Facebook Tweet! Digg!
ximagination, 123RF

ximagination, 123RF

Great Expectations

Expect helps you develop automatic interactive scripts that respond to output from other command-line tools.

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. In Ubuntu, you would do:

$ sudo apt-get install expect

Once you have Expect installed, you can begin writing scripts.

Creating an Interactive SSH Session

As stated previously, 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 . The path on my system is: /usr/bin/expect ; therefore, enter that path on the first line of your script with a preceding "shebang" (#! ):

#!/usr/bin/expect -f

The -f switch tells Expect that it is reading commands from a file.

The spawn command spawns or launches an external command for you. In this case, ssh to a remote host (SERVER ):

spawn ssh <SERVER>

Change the host SERVER to your remote host. When you SSH to a remote system, you're prompted for a password. This password prompt is what you "expect" from the remote system; therefore, you enter that expected response:

expect "password: "

From the local side, you have to enter your password at the password prompt.

To send anything to the remote system, it must be included in double quotes and must include a hard return (\r ). Change PASSWORD to your password:

send "<PASSWORD>\r"

Again, you have to enter the expected response from the remote system, which in this case is a user prompt ($ ).

expect "$ "

Now that you're logged in to the remote system, you can begin your interactive session on that remote host. The following send command issues the ps -ef |grep apache command:

send "ps -ef |grep apache\r"

Output will appear as STDOUT. After the command has executed, you're returned to a prompt, so tell the Expect script that bit of information:

expect "$ "

Finally, send the exit command to the remote system to log out. Don't forget that hard return (\r ):

send "exit\r"

The script in its entirety looks like what you can see in Listing 1.

Listing 1

Logging onto a Remote Host

01 #!/usr/bin/expect -f
02 spawn ssh SERVER
03 expect "password: "
04 send "PASSWORD\r"
05 expect "$ "
06 send "ps -ef |grep apache\r"
07 expect "$ "
08 send "exit\r"

Change permissions on the script so that it is executable using:

$ chmod 755

and try running it (see Figure 1).

Figure 1: Log in, print some info, and log out with your Expect script.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $0.99
(incl. VAT)

Buy Ubuntu User

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content