Using Expect scripts to automate tasks

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 script.sh

and try running it (see Figure 1).

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

Expect Caveats

If your script hangs and doesn't continue, try the command manually yourself and look for the response. If the remote system drops you to a prompt as its final act, then place that in your script (e.g., expect "$ " ).

Be sure you have entered the hard return (\r ) inside the closing quotation mark in your send line. You might also find that your system needs two backslashes on the send line for a hard return (\r ). Sometimes Expect scripts execute too fast, and you won't see your expected response. If that happens, place a sleep command and a number of seconds for the command preceding it to wait for a response, or your data might be ignored.

For example, if you connect to a remote system and there's a delay in creating that connection, your script will continue to execute and fail because it sends commands before the remote system has time to respond.

You have to think about network delays, shell responses, and system timing when scripting in Expect. Like any scripting language, Expect has its quirks, but you'll find that it's an easy way to automate those repetitious keystrokes and procedures. The time you spend debugging your scripts is well worth the effort.

Autoexpect

Of course, some system administrators take lazy to a higher level and even cheat at writing Expect scripts by invoking a shell "watcher" or recorder script named Autoexpect [2].

Once invoked, Autoexpect watches your every keystroke and records it to a file named, script.exp by default. You'll almost certainly have to edit and prune this script to achieve your desired results; however, it can save hours of script debugging to have an almost complete script from which to work. If you simply run a freshly created Autoexpect script, it will likely fail because, if you issued a command that answers your request by displaying information to the screen, the script picks up that answer, too, and copies it into the script file.

For example, if during your Autoexpect session you type ls , the result of that command appears in your script.exp file as well. After you have created a few Expect scripts by hand, you will appreciate the cleanup editing that you have to do in an Autoexpect-created script.

To install Autoexpect, issue a command like:

$ sudo apt-get install expect-dev

You'll likely require many more dependencies for this feature, so prepare yourself for a slight delay while everything installs.

Creating an Interactive SSH Session with Autoexpect

After installing Autoexpect and all of its required packages, you're ready to create Expect scripts automatically by stepping through the procedures you want to automate.

Using the above example, SSH to a remote system, run

ps -ef |grep apache

and then log out.

Invoking Autoexpect is easy:

$ Autoexpect
Autoexpect started, file is script.exp
$

Although it looks as if nothing has happened or is happening, every keystroke you type will be recorded into script.exp . Every STDOUT response you receive will also be copied into that same file.

Your entire session is recorded – but not just recorded, it is also formatted in Expect script style. To stop recording keystrokes to your script, press Ctrl+D on your keyboard to stop Autoexpect and copy the buffer to your file. The complete transcription of this simple procedure is very long and includes a lot of commentary from the author, Don Libes (Listing 2).

Listing 2

Autoexpect-Generated Script

01 #!/usr/bin/expect -f
02 #
03 # This Expect script was generated by Autoexpect on Thu Oct 11 15:53:18 2012
04 # Expect and Autoexpect were both written by Don Libes, NIST.
05 #
06 # Note that Autoexpect does not guarantee a working script.  It
07 # necessarily has to guess about certain things.  Two reasons a script
08 # might fail are:
09 #
10 # 1) timing - A surprising number of programs (rn, ksh, zsh, telnet,
11 # etc.) and devices discard or ignore keystrokes that arrive "too
12 # quickly" after prompts.  If you find your new script hanging up at
13 # one spot, try adding a short sleep just before the previous send.
14 # Setting "force_conservative" to 1 (see below) makes Expect do this
15 # automatically - pausing briefly before sending each character.  This
16 # pacifies every program I know of.  The -c flag makes the script do
17 # this in the first place.  The -C flag allows you to define a
18 # character to toggle this mode off and on.
19
20 set force_conservative 0  ;# set to 1 to force conservative mode even if
21                           ;# script wasn't run conservatively originally
22 if {$force_conservative} {
23         set send_slow {1 .1}
24         proc send {ignore arg} {
25                 sleep .1
26                 exp_send -s -- $arg
27         }
28 }
29
30 #
31 # 2) differing output - Some programs produce different output each time
32 # they run.  The "date" command is an obvious example.  Another is
33 # ftp, if it produces throughput statistics at the end of a file
34 # transfer.  If this causes a problem, delete these patterns or replace
35 # them with wildcards.  An alternative is to use the -p flag (for
36 # "prompt") which makes Expect only look for the last line of output
37 # (i.e., the prompt).  The -P flag allows you to define a character to
38 # toggle this mode off and on.
39 #
40 # Read the man page for more info.
41 #
42 # -Don
43
44 set timeout -1
45 spawn $env(SHELL)
46 match_max 100000
47 expect -exact "]0;USER@HOST: ~USER@HOST:~\$ "
48 send -- "ssh SERVER\r"
49 expect -exact "ssh SERVER\r
50 USER@HOST's password: "
51 send -- "PASSWORD\r"
52 expect -exact "\r
53 Linux SERVER 2.6.32-43-server #97-Ubuntu SMP Wed Sep 5 16:56:41 UTC 2012 x86_64 GNU/Linux\r
54 Ubuntu 10.04.4 LTS\r
55 \r
56 Welcome to the Ubuntu Server!\r
57  * Documentation:  http://www.ubuntu.com/server/doc\r
58 \r
59   System information as of Thu Oct 11 15:55:28 CDT 2012\r
60 \r
61   System load:  1.09               Temperature:         40 C\r
62   Usage of /:   1.0% of 454.22GB   Processes:           168\r
63   Memory usage: 22%                Users logged in:     1\r
64   Swap usage:   0%                 IP address for eth0: 192.168.1.250\r
65 \r
66   Graph this data and manage this system at https://landscape.canonical.com/\r
67 \r
68 7 packages can be updated.\r
69 7 updates are security updates.\r
70 \r
71 New release 'precise' available.\r
72 Run 'do-release-upgrade' to upgrade to it.\r
73 \r
74 *** System restart required ***\r
75 Last login: Thu Oct 11 15:53:41 2012 from HOST\r\r
76 ]0;USER@HOST: ~USER@HOST:~\$ "
77 send -- "ps -ef|grep apache\r"
78 expect -exact "ps -ef|grep apache\r
79 www-data   555 23171  0 Oct07 ?        00:00:00 /usr/sbin/apache2 -k start\r
80 www-data   556 23171  0 Oct07 ?        00:00:00 /usr/sbin/apache2 -k start\r
81 www-data   557 23171  0 Oct07 ?        00:00:00 /usr/sbin/apache2 -k start\r
82 www-data   558 23171  0 Oct07 ?        00:00:00 /usr/sbin/apache2 -k start\r
83 www-data   559 23171  0 Oct07 ?        00:00:00 /usr/sbin/apache2 -k start\r
84 khess    21504 21433  0 15:55 pts/1    00:00:00 grep apache\r
85 root     23171     1  0 Sep27 ?        00:00:28 /usr/sbin/apache2 -k start\r
86 ]0;USER@HOST: ~USER@HOST:~\$ "
87 send -- "exit\r"
88 expect -exact "exit\r
89 logout\r
90 Connection to SERVER closed.\r\r
91 ]0;USER@HOST: ~USER@HOST:~\$ "
92 send -- "^D"
93 expect eof
94 USER@HOST:~$

You can see from the listing that you have a lot of cleanup to do before you distill this transcript down to its essential parts. Autoexpect also changes permissions on the script.exp file so that it is executable. The parts you needed for this script to execute correctly are shown in Listing 3 in my cleaned up version.

Listing 3

Cleaned Up Autoexpect Script

01 #!/usr/bin/expect -f
02
03 set force_conservative 0  ;# set to 1 to force conservative mode even if
04                           ;# script wasn't run conservatively originally
05 if {$force_conservative} {
06         set send_slow {1 .1}
07         proc send {ignore arg} {
08                 sleep .1
09                 exp_send -s -- $arg
10         }
11 }
12
13 set timeout -1
14 spawn $env(SHELL)
15 match_max 100000
16 expect -exact "$ "
17 send -- "ssh SERVER\r"
18 expect -exact "password: "
19 send -- "PASSWORD\r"
20 expect -exact "$ "
21 send -- "ps -ef|grep apache\r"
22 expect -exact "$ "
23 send -- "exit\r"
24 expect -exact "$ "

You can see that the complex prompts, such as

expect -exact "exit\r
logout\r
Connection to <SERVER> closed.\r\r
]0;<USER>@<HOST>: ~<USER>@<HOST>:~\$ "

have been shortened significantly to:

expect -exact "$ "

The prompt still works because Expect looks for the last few characters in an expect line and not the entire string.

You could shorten the line that expects the password prompt from:

expect -exact "password: "

to

expect -exact ": "

A word of caution against shortening your Expect lines too much: It makes the script more difficult, not easier, to read and interpret in the future when you try to figure out what's going on. You might not realize that ": " is a password prompt. Unless you're great at including comments in your scripts, you might spend hours debugging this shortened version.

Conclusion

To be perfectly honest, I only use Autoexpect when building an Expect draft script. To sit down and attempt writing an Expect script line by line just isn't appealing after being seduced and ruined by the ease of removing unwanted lines from an Autoexpect-created script [3].

Autoexpect makes using Expect fun and more intuitive by letting you perform a procedure one time instead of many. After discovering and using Autoexpect, my Expect scripting creation time and debug time has been cut by at least two-thirds. I suspect you'll have much the same return on your time as well.