Spawn, Expect, Send and Interact

Imagine a kind of automation you want to do where you want to telnet to a server within your shell script and carry out the activity on the remote server. There no RSH setup done on the remote server and you have to enter the password non-interactively. Under these kind of circumstances we have a tool called EXPECT.

expect is a unix command present under /usr/local/bin. In one of the scenario, I wanted to automate the installation Oracle Identity manager (The install process will come in another post shortly). But Oracle Identity manager is nither clonable nor it can be silently installed. Trust me, only way to install Oracle identity Manager is to do interactive installation using console mode of GUI mode.

But using expect commad we can simulate the complete installation non-interactively. Here we will see a small example of expect command.

As I said before expect is a binary present in /usr/local/bin directory and expect understand the command like spawn, expect, send and interact. These are the once that I used. Lets take a example where we want to telnet to a server and get the hostname and date (Just to verify that we connected to right server). I will provide the code and will explain the significance later.

#! /usr/bin/expect
spawn telnet mfgops;
expect "login:*";
send "cmsops\r";
expect "Password:*";
send "welcome\r";
expect "(cmsops) cmsops- ";
send "date;hostname;\r";
expect "(cmsops) cmsops- ";
send "exit\r";
expect eof;

Here is how you run this script and the expected output of the same.

-bash-3.00$ expect
spawn telnet mfgops
Connected to (
Escape character is '^]'.

SunOS 5.6

login: cmsops
Last login: Fri Aug 14 11:05:42 from
Sun Microsystems Inc.   SunOS 5.6       Generic August 1997
Built on Mon Sep 11 18:02:19 PDT 2000
You have new mail.
The cmsops user should no longer be used for osn development. Please
use the pomops user instead.
(cmsops) cmsops- date;hostname;
Fri Aug 14 11:06:56 PDT 2009
(cmsops) cmsops- exit
Connection closed by foreign host.

So we run the script using expect (expect instead of using sh. You can even remove the expect and use ./ to run the script, but do not use sh, the script wont work.

spawn is used to spawn a command. A command which will expect some prompts.

expect us used to tell which prompts are expected. Here * is used as wild character, in case you are not sure about complete prompt.

send is used to send the inputs to those prompts. Example for login and password we send the appropriate inputs

interact is not used in this program, but in case you want the control back to the user, you can use interact.

We also have a expect module in perl which does almost similar kind of activities, but its more advanced than expect unix command. (perl module) is not installed by default, but you have to install it yourself.

I found this utility wonderful in case of automating something like telnet or any interactive console based installation. A very handy tool.

Hope this helps !!


expect man pages.


3 thoughts on “Spawn, Expect, Send and Interact

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s