calling an expect script from another script (sh)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting calling an expect script from another script (sh)
# 1  
Old 06-11-2011
calling an expect script from another script (sh)

Hi,
how to call the Expect script from linux shall script?
Our objective is
1. we have written a shall script - it will connect to oracle and generate the xml file
2. we have to transfer this xml file to SFTP machine using SFTP with Expect utility as SFTP will prompt the password.

Thanks in advance and apprecaite you quick help. Thanks

Dhanraj Chilla
# 2  
Old 06-11-2011
Just call it like any other script. Pass in the required information as command line arguments.

For example
Code:
#!/usr/bin/expect

if {[llength $argv] != 3} {
   puts "usage: $argv0 username password hostname"
   exit 1
}

set username [lindex $argv 0] 
set password [lindex $argv 1] 
set hostname [lindex $argv 2]

requires three command line arguments and set each in turn to 3 expect variables.
# 3  
Old 06-12-2011
Thank Q... but still it does not works for me...

Our objective is to connect the oracle database and generate the xml file and then that xml file should be tranfered to another machine using SFTP and the remote SFTP machine prompt the password.
- Here we are using the EXPECT utility

Our first script 1:
Code:
ORACLE_SID=TEST
ORACLE_HOME=/u01/oratest/db/tech_st/11.1.0
export ORACLE_SID ORACLE_HOME
LD_LIBRARY_PATH=$ORACLE_HOME/lib
PATH=$ORACLE_HOME/bin:$PATH
output=`sqlplus -s /nolog <<EOT
   set pages 0 feed off
   whenever sqlerror exit failure;
   connect xgbzprod/xgbzprod
   exec XGBZ_GL_COA_XMLTAG_PROC;
EOT
   `
cd  /u01/oratest/gebiz_processed
for fn in GEBIZ_COA_RPO000*.*; do

# EXPECT using continuing
set timeout 10
spawn $env(SHELL)
match_max 100000
send -- "sftp username@IP Address\r"
expect -exact "testing123@10.1.2.3's password:"
send -- "Password\r"
expect -exact "sftp>"
send -- "cd /<SFTP location>\r"
expect -exact "sftp>"
send -- "lcd /<Local locatoin>\r"
expect -exact "sftp>"
send -- "bin\r"
send -- "put $fn\r"
expect -exact "sftp>"
send -- "quit\r"
send -- "exit\r"
expect eof

But the script fails... even it kept the EXPECT in another location and calling the shall script. Still it does not work.
Please help me I am very much new for EXPECT utility using....
Please guide me step by step...
Appreciate your kind help and thanks in advance...

Dhanraj Chilla

Last edited by Scott; 06-13-2011 at 07:10 AM.. Reason: Code tags
# 4  
Old 06-13-2011
You cannot just bury expect code within a shell script as you have done in the example you provided. Neither can you take such a simplistic sequential approach to expect/send statements if you want robust code.

Perhaps the following script will help you understand how to combine the two. This is a simple script to automate ssh'ing to another system. (yes I know you can use keys with no passphases!)
Code:
#!/bin/bash

TMPEXPECT=expectscript.$$

cat <<EOT > $TMPEXPECT
#!/usr/bin/expect

if {[llength \$argv] != 3} {
   puts "usage: \$argv0 username password host"
   exit 1
}

#log_file -a expectscript.log
#log_user 0

set username  [lindex \$argv 0] 
set password  [lindex \$argv 1] 
set host      [lindex \$argv 2]

set timeout 60 

spawn /usr/bin/ssh \$username@\$host

expect {
    "assword: " {
        send "\$password\n"
        expect { 
            "expecting." { }
            "try again." { exit 1 }
            timeout      { exit 1 }
        } 
    }
    "(yes/no)? " { 
        send "yes\n" 
        expect {
            "assword: " {
                send "\$password\n"
                expect { 
                    "expecting." { }
                    "try again." { exit 1 }
                    timeout      { exit 1 }
                } 
            }
        }
    }
}

exit 0 
EOT

chmod 755 $TMPEXPECT
./$TMPEXPECT <username> <password> <hostname>

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Password check in bash script calling on expect

password check in bash script calling on expect Background: I have to copy a file from one server, to over 100 servers in a test environment. once the file is copied, it requires to have the permissions on the file changed/verified. These are all linux servers. most of them have the same... (1 Reply)
Discussion started by: 2legit2quit
1 Replies

2. Programming

Calling another expect script inside an expect script

I have an expect script called remote that I want to call from inside my expect script called sudoers.push, here is the code that is causing me issues: set REMOTE "/root/scripts/remote" ... log_user 1 send_user "Executing remote script as $user...\n" send_user "Command to execute is: $REMOTE... (1 Reply)
Discussion started by: brettski
1 Replies

3. Programming

Calling expect script inside another expect

Hi, Am very new to expect scripting.. Can You please suggest me how to call an expect script inside another expect script.. I tried with spawn /usr/bin/ksh send "expect main.exp\r" expect $root_prompt and spawn /usr/bin/ksh send "main.exp\r" expect $root_prompt Both... (1 Reply)
Discussion started by: Priya Amaresh
1 Replies

4. Shell Programming and Scripting

Calling a function which uses expect from a shells script

Hi all, This is the first time i am using expect. I am trying to call a function with in the shell script. The function will shh to a new server and will pass the password using expect and send. I need help in calling the fuction i am getting follaowing errors... here the script ... (8 Replies)
Discussion started by: firestar
8 Replies

5. Shell Programming and Scripting

Calling Expect Script - Telnet

Hi All, I have an Expect script which logs into Cisco switch, performs a show interface command. I want to read a file of ip addresses which will be passed to the expect script. The script to read the file works, the expect script works on it's own but when i call the 'expect' script from the... (12 Replies)
Discussion started by: trinak96
12 Replies

6. Shell Programming and Scripting

Calling Shell Script from Expect...

Hi there, I need some help regarding the execution of shell script from expect as the method I am trying is giving me error. I wrote an shell program which takes two arguments to telnet to a device and saves the output in a file. Following is the script.... (0 Replies)
Discussion started by: cyberparanoid
0 Replies

7. Shell Programming and Scripting

ssh is not working while calling through expect shell script

Hi, Please share you experience and way out on below error:--> #!/bin/bash -xv FILE=login.txt + FILE=login.txt CONNECT=sshlogin.exp + CONNECT=sshlogin.exp SERVERNAME=$1 + SERVERNAME=192.168.12.1 MyServer="" + MyServer= MyUser="" + MyUser= MyPassword="" + MyPassword= exec 3<&0 +... (6 Replies)
Discussion started by: manish_1678
6 Replies

8. Shell Programming and Scripting

calling expect script in ksh is failing via cron

I'm calling an expect script via a ksh script in cron and it is failing. The script runs fine if i run it manually. Does anyone know if it is an issue with compatibilty and if there is a way around it? (2 Replies)
Discussion started by: bhatia
2 Replies

9. Shell Programming and Scripting

Calling Expect script in Perl...

I call a EXPECT script from my perl script with machine IP and a FIle. The script logins to the machine and exports the value. The values to be exported or stored in a file. I have close to 10 machines and I have created 10 files and pass the corresponding files in command line, Now I could like... (4 Replies)
Discussion started by: ramkriz
4 Replies

10. Shell Programming and Scripting

Calling an Expect script from a Webpage using PHP

I have a webpage that is in HTML and PHP. In PHP I have tried using exec, system, shell_exec and passthru functions to call an Expect Script file (temp.exp). This Expect file spawns a telnet session that uses "expect/send" commands to retrieve information from an environmental unit (not a normal... (0 Replies)
Discussion started by: CCUSmith
0 Replies
Login or Register to Ask a Question