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.


Advertisement

Automating FTP from Unix Shell Script

Recently I faced an issue about FTP. I was supposed to ask the name and location of the file as one of the input to the user and get the file at current host where I am running the script. The situation was bit complex. I mean, the inputs from the user were from a GUI tool and that was getting passed to one  of the shell script. One of the challenge that I faced was FTPing the file from some other server to the current server. Because normal FTP command ask for user ID and password and is interactive.

But with little investigation I was able to get the correct command options for FTP that can be used inside a shell script and its a totally non-interactive version. Here is the FTP code I used.

EMSUI_SOURCE_LOCATION=$1
echo "echo \"open statj11" >> $HOME/ftp.sh
echo "quote USER ora1818" >> $HOME/ftp.sh
echo "quote PASS ORA1818" >> $HOME/ftp.sh
echo "bin" >> $HOME/ftp.sh
echo "get $EMSUI_SOURCE_LOCATION $HOME/dump/GC_AGENT/`basename $EMSUI_SOURCE_LOCATION`" >> $HOME/ftp.sh
echo "close" >> $HOME/ftp.sh
echo "quite\" | ftp -n" >> $HOME/ftp.sh

sh $HOME/ftp.sh

Basically I am connecting to statj11 server as ora1818 user. The password for this user is ORA1818. EMSUI_SOURCE_LOCATION is the variable which has name and location of the file to be FTPed. I am taking the name and location (complete path) of the file to be FTPed from the user. (EMSUI_SOURCE_LOCATION=$1)

`basename $EMSUI_SOURCE_LOCATION` will give you the name of the file to be FTPed, which I am using as the name of my FTPed file.

All the above commands are echoed to a file called $HOME/ftp.sh and then I am executing this script which will FTP the required file.

Hope this helps !!

One Good Year …

First of all, I would like to thank all the visitors of this blog for there support and co-operation extended and making this blog a success !!! THANKS A TON !!!

I started this blog exactly an year a go… with an intention to keep notes of the things that I do, so that some time down the line, this might help.

I never thought it will grow this big and will serve so many people around globe…

During this year, I have learned many new things, and posted many critical and general things about Oracle technology. I never limited the information to just Oracle Database, but explored other technologies of Oracle as well. I tried to provide uncommon things which one may not be able to see on any other sites. I also tried to consolidate the things which are present at different places (giving the references) so that a visitor should get everything he/she wanted at one place.

Most of the steps and procedure mentioned in my post are executed by me and posted only after a successful try. Therefore all visitors can definitely trust the reliability of any post on this site.

Last thing, I would like to present the statistics for this blog. In 1 year, I posted 72 Posts, 18 Pages distributed among 17 categories.

Below graph shows the month wise status for the year.

Total number of hits in 1 year is close to 45000 ( 44,820 Exact)

Thanks all again for the support and I look forward for more knowledge sharing and growth in the upcoming years. I will be posting more and more information about Oracle technology. Please let me know if you have any feedback for me.

THANKS !!