Sponsored Content
Top Forums Shell Programming and Scripting Expect script - going in loops can't stop Post 302589302 by frappa on Wednesday 11th of January 2012 08:50:37 AM
Old 01-11-2012
Hi,

in order to intercept errors during the connection stage in expect, I generally use a "default" behaviour that makes expect to exit with a custom error code, like in the code fragment shown below:
Code:
#   handle unexpected exceptions (default bahaviour), RSA key
# + fingerprint not found and authentication password request
#
expect {

          default { 
             send_user "unable to connect to host $host\r\n"
             exit $ec_exception
          }

          -re ".*Are.*.*yes.*no.*" {
             send -s "yes\r"
             exp_continue
             #look for the password prompt
          }

          "assword:" {
             send -s "$password\r"
             #the expect command will now return
          }

}

#   handle the chance that the password is not valid or access is denied
expect { 

         default {
            send_user "unable to connect to host $host\r\n"
            exit $ec_exception
         }

         -re "denied|failed|invalid|incorrect" {
            send_user "invalid password or account for host $host\r\n"
            exit $ec_failedauth
         }

         "$shell_prompt"

}

in particular note the instruction
Code:
            exit $ec_exception

and
Code:
            exit $ec_failedauth

where $ec_exception and $ec_failedauth are variables declared and set with a value other than 0 in advance.

Note that in the previous example the "default" behaviour actually has the meaning of an exception handling (that is, I define actions for known expected results, all unforesees results are managed based on the default behaviour).

Then the calling script may check, at each iteration, the exit code returned by the expect script, like this:
Code:
for ip in `cat iplist.txt`
do
   /home/user1/script/change.sh $ip
   RETVAL=$?
   if [ $RETVAL -ne 0 ] ; then
      #perform some action based on intercepted exit code (i.e. log a message, send an email, etc.)...
      [...]
   fi
done

Note that using different exit codes for different exceptions caught in expect may also be useful to determine what action the calling script should do next.

see ya
fra
This User Gave Thanks to frappa For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

strange expect script behavior, or am i misunderstanding expect scripting?

Hello to all...this is my first post (so please go easy). :) I feel pretty solid at expect scripting, but I'm running into an issue that I'm not able to wrap my head around. I wrote a script that is a little advanced for logging into a remote Linux machine and changing text in a file using sed.... (2 Replies)
Discussion started by: v1k0d3n
2 Replies

2. Shell Programming and Scripting

Need help with Expect script for Cisco IPS Sensors, Expect sleep and quoting

This Expect script provides expect with a list of IP addresses to Cisco IPS sensors and commands to configure Cisco IPS sensors. The user, password, IP addresses, prompt regex, etc. have been anonymized. In general this script will log into the sensors and send commands successfully but there are... (1 Reply)
Discussion started by: genewolfe
1 Replies

3. Shell Programming and Scripting

Expect While Loops - Partial Automation?

I've been reading the O'Reilley expect book and I'm trying to create partial automation for common questions asked on screen in a telnet session and return to interact so the user can resume control. For example while {1} { expect { -re "What color is the sky?" {send... (0 Replies)
Discussion started by: mlarivie
0 Replies

4. Shell Programming and Scripting

multiple while loops in expect script

Hi, I am trying to incorporate multiple while loops into an expect script written in ksh shell. This is on a Solaris 10 system. Here is the code: #!/bin/ksh EXPECT=/usr/local/bin/expect exp_internal i=1 h=0 while ]; do $EXPECT << DONE set stty_init raw ... (1 Reply)
Discussion started by: cic
1 Replies

5. Shell Programming and Scripting

korn shell for loops with expect issue

Hi I have the following Korn script having multiple for loops. #!/bin/ksh EXPECT=/usr/local/bin/expect exp_internal for d in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 i22 23 24 25 26; do for i in 01 02 03 04 05 06 07 ; do for h in 00 01 02 03 04 05 06 07 08 09 10 11 12... (2 Replies)
Discussion started by: cic
2 Replies

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

7. Shell Programming and Scripting

Expect scripting telnet stop on bad username or password

I am trying to build and expect script to log into multiple aix boxes and change password. I need for the script to terminate if it cannot log into a server because the username or password is wrong. #!/usr/bin/expect set timeout 1 set host set user set password set uh "Unknown host" set... (3 Replies)
Discussion started by: leemalloy
3 Replies

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

9. Programming

Expect script returning string following a found expect.

I'm fairly new to scripting so this might not be possible. I am using Expect with Cisco switches and need to capture the string after finding the expect request. For example, when I issue "show version" on a Nexus switch, I'm looking to capture the current firmware version: #show version ... (0 Replies)
Discussion started by: IBGaryA
0 Replies

10. UNIX for Advanced & Expert Users

Howto stop loops in CentOS

Good morning, At the client location os is CentOS. In all the terminals i.e F1, F2, F3....F10 PING command is continuously running. I tried to terminate it using CTRL C or quit but unable to stop that command in all the terminals. How to stop that? Howto find batch files which are being... (3 Replies)
Discussion started by: sureshbabu.anis
3 Replies
All times are GMT -4. The time now is 06:09 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy