Expect scripting language is used to feed input automatically to an interactive program. It is easy to learn compared to other scripting languages. Using expect script sysadmins and developers can automate redundant tasks easily. It works by expecting specific strings, and sending or responding strings accordingly.

Following three expect commands are used when automating any interactive processes.

  • send – to send the strings to the process
  • expect – wait for the specific string from the process
  • spawn – to start the command

Make sure to install expect packages on your system, as it does not get installed by default. Once installed, you’ll see the expect interpreter as “/usr/bin/expect”. Generally, expect script files has .exp as extensions.

1. Expect “Hello World” Example

The following expect script is expecting the specific string “hello”. When it finds it (after user enters it), “world” string will be send as response.

#!/usr/bin/expect
expect "hello"
send "world"

2. Timeout On Expect String

By default, the expect timeout is 10 seconds. If you don’t enter anything for the expect command, it times out in 20 seconds. You can also change the timeout as shown below.

#!/usr/bin/expect
set timeout 10
expect "hello"
send "world"

3. Automate User Processes With Expect

With the help of expect, you can automate the user processes and get the expected output. For example, you might use this to simplify the project testing process by writing scripts for the test cases.

The below example does the addition program automation.

#!/usr/bin/expect

set timeout 20

spawn "./addition.pl"

expect "Enter the number1 :" { send "12\r" }
expect "Enter the number2 :" { send "23\r" }

interact

Execute this as shown below.

$ ./user_proc.exp
spawn ./addition.pl
Enter the number1 : 12
Enter the number2 : 23
Result : 35

In case, if you have written the code without interact command, then the script would exit immediately after sending the string “23\r”. Interact command does the control, hands over the job to the addition process, and produces the expected result.

4. Match and No Match Contents in $expect_out Variables

On the successful matching of string expect returns, but before that it stores the matched string in $expect_out(0,string). The string that are received prior plus the matched string are stored in $expect_out(buffer). The below example shows you the value of these two variable on match.

#!/usr/bin/expect

set timeout 20

spawn "./hello.pl"

expect "hello"
send "no match : <$expect_out(buffer)> \n"
send "match :  <$expect_out(0,string)>\n"

interact

The hello.pl program just prints only two lines as shown below.

#!/usr/bin/perl

print "Perl program\n";
print "hello world\n";

Execute it as shown below.

$ ./match.exp
spawn ./hello.pl
Perl program
hello world
no match :  <Perl program

hello>
match :  <hello>

5. Automate SU login into Other User Accounts

Expect allows you to pass the password for the Linux login account from the program, instead of entering the password on the terminal. In the below program, su login is automated to login into desired accounts.

#!/usr/bin/expect

set timeout 20

set user [lindex $argv 0]

set password [lindex $argv 1]

spawn su $user

expect "Password:"

send "$password\r";

interact

Execute the above expect program as shown below.

bala@localhost $ ./su.exp guest guest
spawn su guest
Password:
guest@localhost $

After running the above script, it logged into the guest user account from bala user account.

6. SSH Login into Another Machine

The example expect program shown below automates the ssh login from one machine to another machine.

#!/usr/bin/expect

set timeout 20

set ip [lindex $argv 0]

set user [lindex $argv 1]

set password [lindex $argv 2]

spawn ssh "$user\@$ip"

expect "Password:"

send "$password\r";

interact

Execute the above expect program as shown below.

guest@host1 $ ./ssh.exp 192.168.1.2 root password
spawn ssh root@192.168.1.2
Password:
Last login: Sat Oct  9 04:11:35 2010 from host1.geetkstuff.com
root@host2 #

Originally from http://www.thegeekstuff.com/2010/10/expect-examples/