Find the answer to your Linux question:
Results 1 to 3 of 3
Hi there, i am sitting on a very Big Expect Script and got some problems now with calling the Expect from within Bash and getting Errror and Exit Codes back ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2012
    Posts
    1

    Exit Code Handling in Expect Running from Bash


    Hi there,


    i am sitting on a very Big Expect Script and got some problems now with calling the Expect from within Bash and getting Errror and Exit Codes back to handle the errors.

    For Example my functiosn look like this

    Code:
    function execute_ssh_command () {
    
    test_log INFO "Executing SSH Command $3 on Device $2 with a $1 seconds timeout" "execute_ssh_command"
    
    expect <<- DONE
    
    set timeout $1
    
    system "touch  ~/.ssh/known_hosts"
    system "ssh-keygen -R $2"
    spawn /bin/bash
    sleep 3
    send_user "Executing command via SSH...\n"
    send "/usr/bin/ssh -l root $2\n"
    expect "(yes/no)?" { send "yes\n" }
    expect "assword:" {
    	after 1000
    	send "$DUT_PWD\r"
    	expect " #"
    	send "no_shell_timeout\n"
    }
    expect " #"
    send "$3\r"
    send_user "\n"
    expect " #";
    send_user "Done."
    exit
    expect eof
    DONE
    }
    I have to handle the cases that

    - No string is given back
    - The expected string is not given back in a certain amount of time
    - The expected string is given back in a small amount of time


    I think i have got the problems cause i really dont know how to handle the exit codes in expect itself. Are there even any error handling functions in expect??

    Thanks.

    Regards

    Sascha from Germany

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    i had a similar problem using expect to start an sftp session: i wanted to be able to trap errors in the sftp calls and let expect handle them. this may not be the same way you want to do things, but maybe it will give you some ideas.

    here's an excerpt from the code using the "cd" function in sftp to change dirs.

    Code:
    # CD mode
    if { [string equal $MODE "chdir"] == 1 } {
      send "cd $RDIR\n";
      expect "sftp>"
      set igot $expect_out(buffer)
      if { [regexp -nocase {.*file .* not found} $igot errstr] } {
        puts [string trim $errstr]
        send "quit\n";
        exit 1
      } elseif { [regexp -nocase {.*permission denied} $igot errstr] } {
        puts [string trim $errstr]
        send "quit\n";
        exit 1
      } elseif { [regexp -nocase {.*no such file or directory} $igot errstr] } {
        puts [string trim $errstr]
        send "quit\n";
        exit 1
      }
    
    }

  3. #3
    Linux Newbie
    Join Date
    Apr 2005
    Location
    Clinton Township, MI
    Posts
    104
    My thinking would also be to write a TCL/Expect script and drive all of the logic from within the Expect script. If you need to use Bash at all as a driver, just use Bash to invoke the complete Expect script, and handle the error codes from within Expect, rather than attempting to pass them back and forth between Bash and Expect. The example atreyu provides is along the lines of what I was thinking, too. Adapt that code to what you intend to do, and that should point you in the right direction.
    Brian Masinick
    masinick AT yahoo DOT com

  4. $spacer_open
    $spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •