Expect script: obtain the exit code of remote command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Expect script: obtain the exit code of remote command
# 1  
Old 09-23-2010
Expect script: obtain the exit code of remote command

Hi all,
I'm trying to run the sipp simulator in crontab but after some attempt I came to the conclusion that for some reason this isn't possible (maybe due to sipp interactive nature).
This is confirmed by these posts.

Now I'm trying to launch sipp from an expect script that runs in crontab.

What I need is to grab the exit code of sipp to decide if the system is working (exit code == 0) or not (exit code != 0).

I searched all the afternoon trying to make this working but with no luck.
Here is the code I wrote:

Code:
#!/usr/bin/expect
set SERVER [lindex $argv 0]
set MYSCENARIO [lindex $argv 1]
set MRFP [lindex $argv 2]
set SERVICE [lindex $argv 3]
set LOCAL_IP [lindex $argv 4]
set IP_RTP [lindex $argv 5]
set media_port [lindex $argv 6]
set MAXCALLS [lindex $argv 7]
set RATE [lindex $argv 8]
set RATEPERIOD [lindex $argv 9]
set TIMEOUT [lindex $argv 10]
set TRACEMSG [lindex $argv 11]
set timeout 60
spawn ssh $SERVER
   expect "password:" {
        send "ChangeMe\r"
        expect "*]#*" {
                send "sipp -sf /scripts/MRF_Probe/$MYSCENARIO.xml $MRFP -s $SERVICE -i $LOCAL_IP -mi $IP_RTP -mp $media_port  -rtp_echo -m $MAXCALLS -r $RATE -rp $RATEPERIOD -timeout $TIMEOUT -trace_err -trace_screen  $TRACEMSG\r"
                expect "*]# "
                send "echo \$?\r"
                expect "*]# "
                set exit_status $expect_out(buffer)
                send_user "EXIT CODE: $exit_status"
        }          
   }
exit $exit_status

This seems to work fine but unfortunately the variable $exit_status gets this value (two rows):

Quote:
echo $?
0
I guess I use the wrong way: I should use the wait() function in expect, something like:

Code:
spawn true
catch wait reason
lindex $reason 3

But I can't figure out how to do this.
I didn't found much examples on the net...

Can anyone help, please?

Thanks in advance,
Evan

---------- Post updated 23-09-10 at 10:48 AM ---------- Previous update was 22-09-10 at 06:40 PM ----------

This morning I found out how to get the exit code from the expect script, here is the code:

Code:
#!/usr/bin/expect
set SERVER [lindex $argv 0]
set MYSCENARIO [lindex $argv 1]
set MRFP [lindex $argv 2]
set SERVICE [lindex $argv 3] 
set LOCAL_IP [lindex $argv 4]
set IP_RTP [lindex $argv 5]
set media_port [lindex $argv 6]
set MAXCALLS [lindex $argv 7]
set RATE [lindex $argv 8]
set RATEPERIOD [lindex $argv 9]
set TIMEOUT [lindex $argv 10]
set TRACEMSG [lindex $argv 11]
set timeout 60
spawn ssh $SERVER
   expect "password:" {
        send "ChangeMe\r"
        expect "*]#*" { 
                spawn /SIPP/sipp -sf /scripts/MRF_Probe/$MYSCENARIO.xml $MRFP -s $SERVICE -i $LOCAL_IP -mi $IP_RTP -mp $media_port  -rtp_echo -m $MAXCALLS -r $RATE -rp $RATEPERIOD -timeout $TIMEOUT -trace_err -trace_screen  $TRACEMSG
                expect "*]# "
                catch wait reason
                set exit_status [lindex $reason 3]
                send_user "EXIT CODE: $exit_status\r"
        } 
   }
exit $exit_status

Now I have another problem: it doesn't work from crontab, I get 127 exit code from the bash script in crotnab that recall the expect ... any ideas?

---------- Post updated at 11:46 AM ---------- Previous update was at 10:48 AM ----------

Got also this last one with export TERM:

Code:
#!/bin/bash

export TERM=xterm

MYSCENARIO=$1
SUTSIP=$2

#SUTSIP=10.188.1.231

SERVICE=ivr
RATE=200
RATEPERIOD=1000
MAXCALLS=1
TIMEOUT=30

MRFP=$SUTSIP:5061

LOCAL_IP=10.130.9.39
IP_RTP=10.130.9.52
media_port=5061

RETVAL=0

\rm *.log

if [ $MAXCALLS = "1" ]
then
   TRACEMSG="-trace_msg"
fi

#Eseguo il comando da script expect
/scripts/MRF_Probe/runtest.exp $SUTSIP $MYSCENARIO $MRFP $SERVICE $LOCAL_IP $IP_RTP $media_port $MAXCALLS $RATE $RATEPERIOD $TIMEOUT $TRACEMSG > /dev/null

RETVAL=$?

if [ $RETVAL == "0" ]
then
        echo RETVAL=$RETVAL";"DATE=`date +%Y%m%d%H%M%S`
else
        echo RETVAL=$RETVAL";"DATE=`date +%Y%m%d%H%M%S`";"See errors in *error.log
fi

exit $RETVAL

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script connect to remote server, not find files and exit only from remote server, but not from scrip

I have a script, which connecting to remote server and first checks, if the files are there by timestamp. If not I want the script exit without error. Below is a code TARFILE=${NAME}.tar TARGZFILE=${NAME}.tar.gz ssh ${DESTSERVNAME} 'cd /export/home/iciprod/download/let/monthly;... (3 Replies)
Discussion started by: digioleg54
3 Replies

2. Shell Programming and Scripting

Make expect exit the UNIX script in erreneous condition

Hi, I am writing a menu driven program using shell script. THe script will be collecting data by logging into the other servers and bringing back the data to home server to process it and accordingly issue commands. TO automate commands execution , I am using expect script. However I am not able... (5 Replies)
Discussion started by: ashima jain
5 Replies

3. Shell Programming and Scripting

Check/get the exit status of a remote command executed on remote host through script

Geeks, Could you please help me out in my script and identify the missing piece. I need to check/get the exit status of a remote command executed on remote host through script and send out an email when process/processes is/are not running on any/all server(s). Here's the complete... (5 Replies)
Discussion started by: lovesaikrishna
5 Replies

4. Shell Programming and Scripting

How to exit shell script if remote login unsuccessful?

#!/bin/bash for servers in `cat ~/servers` do rosh -l root -n $servers 'if then echo $HOSTNAME else exit 1 fi' done I have few servers in the for loop that is powered off, so whenever I execute my script, it works fine if all the servers are on, but when it tries to execute the script... (1 Reply)
Discussion started by: Rojan Shakya
1 Replies

5. UNIX for Advanced & Expert Users

Unable to run the script on remote machine Using Expect script

Not able to execute the file in remote host using except utility I am automating the SFTP keys setp process: So i created the expect script for controlling the output of shell below is my main code: Code: #!/usr/bin/expect set fd set password close $fd set df set app close $df... (1 Reply)
Discussion started by: Manoj Bajpai
1 Replies

6. Shell Programming and Scripting

while loop stops after first iteration - remote ssh exit command problem?

I have written the following script to update some Debian boxes. #!/bin/bash mxg_hosts_file="/etc/mxg/ssh-hosts" while read line ; do mxg_host="$(echo ${line} | awk -F":" '{print $1}')" mxg_port="$(echo ${line} | awk -F":" '{print $2}')" echo "Connecting and Upgrading... (3 Replies)
Discussion started by: jelloir
3 Replies

7. Shell Programming and Scripting

expect ssh script cannot finish and exit

All i am new to linux, and try to have a simple expect script to ssh then telnet to the network equipment, and exit itself. but dont know why i hang at the last $ #!/usr/bin/expect set timeout 10 set arg set arg1 spawn ssh -l UserA 1.1.1.1 expect "assword:"; send "PasSwOrD\r";... (1 Reply)
Discussion started by: samoptimus
1 Replies

8. Programming

Expect script to run a Shell script on remote server

Hi All, I am using a expect script to run a shell script on remote server, the code is as follows. But the problem is that it executes only first command, and hangs it doesn't run the next commands. spawn ssh $uid@$host expect "password:" send "$password\r" expect "*\r" send... (2 Replies)
Discussion started by: yashwanthsn
2 Replies

9. Shell Programming and Scripting

check exit status - Expect Script

from my main script, i am calling an expect script. there are a lot of conditions in the Expect script and it can have any exit value based on success or failure of the Expect Script. how can i check the exit status of Expect scritp in the main script. (1 Reply)
Discussion started by: iamcool
1 Replies

10. Shell Programming and Scripting

Getting the exit status of a remote command

Hi to everyone. How can I get the exit status from a remote command executed with rexec? :eek: machine A has RedHat Linux 9 and the remote machine B has SCO UNIX. Code: rexec -l user -p password host sh /u/files/scripts/seq_cal.sh 2006 08 I want the exit status returned by... (1 Reply)
Discussion started by: zoonalex
1 Replies
Login or Register to Ask a Question