Find the answer to your Linux question:
Results 1 to 4 of 4
I'm running expect script from a C++ program using the popen function. My expect script is using the command: spawn telnet $ipaddress Then it performs login and runs several commands. ...
  1. #1
    Just Joined!
    Join Date
    Dec 2007
    Posts
    2

    Waiting for the result when running expect script

    I'm running expect script from a C++ program using the popen function.
    My expect script is using the command:
    spawn telnet $ipaddress

    Then it performs login and runs several commands.

    My problem is that I don't get the results of the script. It seems like I'm getting the control right away and continue with my program before the script has performed the telnet, thus I get and empty result when running the script from my program.

    How can I wait for the script to end, and receive the results to see if there was a success or some problem has occured.

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Can you post a tiny, complete expect script which duplicates the problem?

    Don't forget to alter your user name and password on the remote machine before posting the script.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    Dec 2007
    Posts
    2
    My mistake, it appears like I got the whole interaction as the result, and I thought that the interaction was after I returned.
    The problem now is how to get only the messages which where send by send_user and not the whole output.

    Here is a short and simple script, where I would expect to recieve to my C program only the OK.

    spawn telnet [lindex $argv 0]
    expect login:
    send "admin\r"
    expect Password:
    send "\r"
    expect >
    send "enable\r"
    expect #
    send_user "OK"
    exit

    This is important since, my script is much more complex and I only want to get as a result if there was some error, or if there was a success.

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    It's been a while since I've used expect, but here's an example I'm copying straight out of the excellent O'Reilly book Exploring Expect. If there are any errors, they're not my errors; they instead belong to my stubby, clumsy fingers.
    Code:
    #!/usr/bin/expect --
    
    log_user 0
    regexp (.*)@(.*) $argv ignore user host
    spawn telenet $host smtp
    set timeout -1
    expect -re "220.*\r\n"
    send "vrfy $user\r"
    expect "250" {puts "GOOD"} \
           "550" {puts "BAD"}
    Image of sample usage of this script:
    Code:
    % vrfy jobs@next.com
    GOOD
    % vrfy jobs@apple.om
    BAD
    %
    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

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