Shell Script Archive

6 Expect Script Examples to learn expect.

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.

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.

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.


set timeout 20

spawn "./"

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


Execute this as shown below.

$ ./user_proc.exp
spawn ./
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.


set timeout 20

spawn "./"

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


The program just prints only two lines as shown below.


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

Execute it as shown below.

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

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.


set timeout 20

set user [lindex $argv 0]

set password [lindex $argv 1]

spawn su $user

expect "Password:"

send "$password\r";


Execute the above expect program as shown below.

bala@localhost $ ./su.exp guest guest
spawn su guest
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.


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";


Execute the above expect program as shown below.

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

Originally from

Scripts for Oracle Solaris ZFS and UFS Filesystem Administration

This tech tip provides a main script and several subscripts that are useful for the administration of Oracle Solaris Zettabyte File System (ZFS) and basic administration of UFS in the Oracle Solaris 10 operating system. The scripts can be modified as needed and can be used to perform the following tasks:

  • Creating an Oracle Solaris ZFS storage pool
  • Adding a device to a storage pool
  • Creating a filesystem in a storage pool
  • Setting quota on an Oracle Solaris ZFS filesystem
  • Creating a mirrored storage pool
  • Adding devices to a mirrored storage pool
  • Creating a RAID-Z (Enhanced RAID5) storage pool
  • Adding devices to a RAID-Z storage pool
  • Destroying a storage pool
  • Determining if problems exist in a storage pool
  • Creating a UFS filesystem and mounting it

Read More:


function/script to search smit fastpath

The SMIT fast path facility allows you to go directly to a particular panel or menu from the operating system command line instead of through the panel hierarchy.

You won’t be able to remember all the fast paths in AIX SMIT menus. And it is a headache to find it menu by menu, level by level.

Here is the script/function you can add it into your profile.

function smit_fast_path {
  odmget sm_menu_opt | awk '( $1=="id" || $1=="next_id" ) && ! x[$3]++ {gsub("\"","",$3) ; print $3}' \
   | grep -i $1

Usage: On command line prompt, type smit_fast_path keyword_you_want_to_search
keyword is case insensitive.


I want to search all the SMIT fast paths that contain keyword mksysb.

$ smit_fast_path mksysb