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 9.1.0.1 (The install process will come in another post shortly). But Oracle Identity manager 9.1.0.1 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 test.sh
spawn telnet mfgops
Trying 130.35.5.43...
Connected to mfgops.us.oracle.com (130.35.5.43).
Escape character is '^]'.

SunOS 5.6

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

So we run the script using expect (expect test.sh) 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. Expect.pm (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 !!

Reference:

expect man pages.



About these ads

3 thoughts on “Spawn, Expect, Send and Interact

  1. Pingback: Blogroll Report 14/08/2009 – 21/08/2009 « Coskan’s Approach to Oracle

Leave a Reply

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s