1825799 Members
2247 Online
109687 Solutions
New Discussion

Expect Script Help

 
pareshan
Regular Advisor

Expect Script Help

I have a ksh script which I have written but needs to change it to expect script so that I can automate it but dont know much about expect and while I tried its not working
can anyone help me on this plz, how can i conver that into expect script

here is my script which basically checks first and copy and untar the installation files and install fixpack which is just for one server and i have to copy it to all the servers to do it and while doing it ask for password as ssh is not setup for the accounts im using and I am told its not supposed to be setup for this account so that there is no way i can set up ssh for this account.

My script runs fine and install fixpack in one server provided i enter the password manually when it prompt for.

But I am trying to do something like expect script which can scp this script to all the boxes and can install the fixpack without prompting me for password or any other way does the job i mentioned.

I have done simple expect script only and couldnt figure out how to do for this one.

Could anybody help me plz. I will appreciate any help.


--------script-----
------------------
#!/usr/bin/ksh

#set -xv

checking () {

##current_fp_version = $(/usr/local/TWS/TWSPRD84/version/version|grep FP0003)
echo "Checking The Fix Pack Version, If Alrady Installed.........."
echo "-------------------------------------------------------------"

if [[ $(/usr/local/TWS/TWSPRD84/version/version|grep FP0003) = 0 ]]; then
echo "Already have FixPack3 Installed"
exit
else
echo "Doesnt Have New Version Of FixPack, Have to install FP-3, Starting the Installation............."
fi

}

checking

copy_and_tar() {

server=$(hostname)
echo "Installing fix pack in $server"

###Making FixPack Directory First
mkdir /usr/local/TWS/TWSPRD84/FIXPACK3
chmod 755 /usr/local/TWS/TWSPRD84/FIXPACK3
cd /usr/local/TWS/TWSPRD84/FIXPACK3
echo "Copying Fix Pack Tar File From Source, It May Take A While, Please Wait..." | tee fix_pack_installation_log

scp bowie:/usr/local/TWS/TWSPRD84/DOWNLOADS/FIXPACK3/HP_TWS_FP0003.tar .

original_size = "ssh bowie ls -al /usr/local/TWS/TWSPRD84/DOWNLOADS/FIXPACK3/HP_TWS_FP0003.tar|awk '{print $5}'"
echo $original_size
new_size= "ls -al /usr/local/TWS/TWSPRD84/FIXPACK3/HP_TWS_FP0003.tar|awk '{print $5}'"
echo $new_size
echo "Checking File Size"

if [ $original_size = $new_size ]; then
echo "File Size Is Same In Source And Destination"
else
echo "Have to copy the tar file again, size varies in source and destination"
exit
fi
echo "Now Untaring The Files......Wait For It To Get Completed"

cd /usr/local/TWS/TWSPRD84/FIXPACK3
tar -xvf HP_TWS_FP0003.tar

}

copy_and_tar | tee fix_pack_copy&tar_log

installing_fix_pack () {

##Renaming Directories so that we can install as ROOT
mv /usr/local/TWS/TWSPRD84/DOWNLOADS /usr/local/TWS/TWSPRD84/DOWNLOADS_OLD
mv /usr/local/TWS/TWSPRD84/FIXPACK3 /usr/local/TWS/TWSPRD84/DOWNLOADS
cd /usr/local/TWS/TWSPRD84/DOWNLOADS/HPUX

mv SETUP.bin SETUP.bin_rename_later
cp twspatch SETUP.bin

##needs to shut down the TWS before installing
/usr/local/TWS/TWSPRD84/bin/conman "shut;wait"

sudo /usr/local/TWS/TWSPRD84/DOWNLOADS/HPUX/SETUP.bin -install -uname twsprd84

##starting the TWS again after everything is done
/usr/local/TWS/TWSPRD84/bin/conman "start"

cd /usr/local/TWS/TWSPRD84/DOWNLOADS/HPUX
mv SETUP.bin_rename_later SETUP.bin

##Renaming Directories Back To Original Names
cd /usr/local/TWS/TWSPRD84/
mv /usr/local/TWS/TWSPRD84/DOWNLOADS /usr/local/TWS/TWSPRD84/FIXPACK3
mv /usr/local/TWS/TWSPRD84/DOWNLOADS_OLD /usr/local/TWS/TWSPRD84/DOWNLOADS
mv /usr/local/TWS/TWSPRD84/FIXPACK3 /usr/local/TWS/TWSPRD84/DOWNLOADS/FIXPACK3


}

installing_fix_pack | tee fix_pack_installation_log


Thank You
2 REPLIES 2
OldSchool
Honored Contributor

Re: Expect Script Help

I'd seriously consider exchanging keys so ssh / scp can work without the password.

if not, then something similar to the following might work:


#!/usr/local/bin/expect --
spawn
expect "Password:"
send "password\r"
send "\r"

============

fix "Password:" to match the prompt you get. substitute real password for "password\r"

this can be further scripted to run thru the above (once fixed / working) with servername / password pairs.

my choice would be passwordless ssh however, as its more secure, and once setup, its dead easy to use.
Hakki Aydin Ucar
Honored Contributor

Re: Expect Script Help

As far as I see your necessity, You would better use TCL language, it seems very good especially in use this type of script, both condition check and automated telnet remote machine.. you can also use expect inside in TCL since they are compatible to work together.