Sponsored Content
Top Forums Shell Programming and Scripting Make expect exit the UNIX script in erreneous condition Post 302963311 by ashima jain on Monday 28th of December 2015 06:41:59 AM
Old 12-28-2015
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 to understand , how to do error handling with expect script
I tried using the code tags . Somehw there is some issue . Hence pasting the code

Code:
#!/bin/awk -f
#set user [lindex $argv 0]
#set pswd [lindex $argv 1]

INTSRVUSR=$1;export INTSRVUSR
CGCUSERID=$2;export CGCUSERID
CGCPASS=$3;export CGCPASS
LABIP=$4;export LABIP
LABUSERID=$5;export LABUSERID
LABPASS=$6;export LABPASS
CURRENTDIR=`pwd`;export CURRENTDIR
FNAME="command";export FNAME

scptocgc(){
    ###---------------- scping the command file to the cgcuser
    expect -c '
    ##set timeout 10
   spawn scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no  $env(CURRENTDIR)/AUTOBR/other_scripts/$env(FNAME) $env(CGCUSERID)@$env(INTSRVUSR):.

   expect "assword:"
   send "$env(CGCPASS)\r"
   expect {
        "directory" {
                    return "No such directory"
                  }
         "ecamolx1820" {
                     return "error: file sent      ## I am not able to display the                  
                                                          ##error on prompt and also stop           
                                                    ## the function from further execution
                   } 
        }
        expect sleep 1
        spawn ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $env(CGCUSERID)@$env(INTSRVUSR)
        expect -nocase "assword:"
        send "$env(CGCPASS)\r"
        '
          }

    logintocgc(){
        expect -c '
        set timeout 60
        ###---------------- login in to the cgcuser
        spawn ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $env(CGCUSERID)@$env(INTSRVUSR)
        expect -nocase "assword:"
        send "$env(CGCPASS)\r"
        expect {
             "Access denied" {send "echo access denied;exit 1" }
             "] " {send "scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no /home/vse/$env(CURRENTUSER)/$env(FNAME) mtc@10.1.77.148:/opt/swd/incoming/$env(FNAME)\r" 
                   expect "assword:"
                   send "CDMA@123\r" } 
                   expect {
                         "directory" {send "echo The file is not found\r";exit 1}
                              "] " {send "echo. The file is present\r"}
                            }
             }
    ##It should not proceed if there are errors and stop the script here
     expect "] "
    send "ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no mtc@10.1.77.148\r"
     expect -nocase "assword:" {send "CDMA@123\r"}
     expect "cli>" {send "exit\r" } # CLI
     expect "] "
     send "exit\r"
     expect eof
    '
    sleep 1;
}
scptocgc
logintocgc


Last edited by Don Cragun; 12-28-2015 at 03:29 PM.. Reason: Add CODE tags.
 

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Exit for loop in a shell script if a condition is successfull

Hi All, I am stuch in a script where a for loop is running to execute some commands for some values. Now my problem is i have to have an if condition that if the first iteration is successful then it has to exit the for loop otherwise it has to continue normally. my code is this: for... (5 Replies)
Discussion started by: usha rao
5 Replies

3. Shell Programming and Scripting

expect wait --More-- string and exit

Hi I am programing a expect script on debian, I connected to a firewall to get configuration copy via telnet or ssh but Because of firewall show configuration console wait and print --More-- need press space key at least 100.Help me please. firewall output like this :( This output isn't... (3 Replies)
Discussion started by: ayucelen
3 Replies

4. Shell Programming and Scripting

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. ... (0 Replies)
Discussion started by: Evan
0 Replies

5. Shell Programming and Scripting

do nothing if condition is not met but not exit

Hello all, I created the below script....and it seemed to be working fine. My problem is i want the script to ignore rest of the things if my condition is not met but do not exit.... #!/bin/ksh ########################### ########################### # Set name of the listener, this... (2 Replies)
Discussion started by: abdul.irfan2
2 Replies

6. 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

7. Shell Programming and Scripting

Handle occasional condition in expect script

Hi, I am using Solaris OS, I want to handle an occasional expression in expect script while logging into a remote server with ssh. In normal scenario the expected expression is as below, spawn ssh $user@$ip expect "assword:" send "$password\r" but in a condition when the remote server... (2 Replies)
Discussion started by: varunksharma87
2 Replies

8. UNIX for Dummies Questions & Answers

Problem in Unix script to exit from Putty

We have a requirement where in the user needs to select a option 4 from the menu and the putty window should be closed.I tried giving exit 0 ;; and this is only exiting from the script menu and showing back the prompt.Is there a way for this. (2 Replies)
Discussion started by: gopalt
2 Replies

9. Shell Programming and Scripting

Time condition exit loop

Hi All, Requirement: The below script should automatically exit at 6pm everyday without manually killing the script Tried running with the below shell script but found the script was still running when the time was 6:15pm. The script did not exit the while loop at 6pm The script runs... (6 Replies)
Discussion started by: a1_win
6 Replies

10. Shell Programming and Scripting

How to exit from shell script if above condition fails?

HI cd ${back_home} if above back_home does not exist, then script shoul exit. Please let us know how to do that (7 Replies)
Discussion started by: buzzme
7 Replies
tcp-env(1)                                                    General Commands Manual                                                   tcp-env(1)

NAME
tcp-env - set up TCP-related environment variables SYNOPSIS
tcp-env [ -rR ] [ -ttimeout ] program [ arg ... ] DESCRIPTION
The input for tcp-env must be a TCP connection. tcp-env finds out information about that connection, puts the information into several environment variables as described in tcp-environ(5), and runs program with the given arguments. Usually tcp-env is run from inetd. It might instead be run from another server that already sets up the right environment variables; if PROTO is set to TCP when tcp-env is invoked, tcp-env assumes that all the other variables are set up properly, and it does not check whether the input is a TCP connection. OPTIONS
-r (Default.) Attempt to obtain TCPREMOTEINFO from the remote host. -R Do not attempt to obtain TCPREMOTEINFO from the remote host. -ttimeout Give up on the TCPREMOTEINFO connection attempt after timeout seconds. Default: 30. SEE ALSO
tcp-environ(5), inetd(8) tcp-env(1)
All times are GMT -4. The time now is 02:33 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy