PERL and Expect Scripting...

Discussion in 'Mac Programming' started by digital1, Nov 23, 2005.

  1. macrumors 6502

    digital1

    Joined:
    Jan 2, 2002
    Location:
    Wisconsin
    #1
    Ok, so here is the scoop. This is a continuation of what I have been working on that I asked about before. I am at stuck yet again. I found out that the authentication isn't working right in the perl script. Basically there is a call to an "expect script" that automates the user interaction to an already-working perl script and store it in an array. What this already-working perl script does is authenticates a user to a website and grabs information about their grade,classes,etc. Now the script I am working is supposed to store the result of this script and use those results for further processing. Maybe it is better that I show you all what it looks like:

    Now the PERL looks like this:
    my @paws_output = `/var/www/cgi-bin/addu/automate_paws.ept $student_id $pin_no \'$semester\'`;

    The expect script looks like this:
    #!/usr/bin/expect -f
    spawn /var/www/cgi-bin/addu/paws/Fall2004/paws.pl
    expect "Please enter your PAWS username:"
    send "[lindex $argv 0]\r"
    expect "Please enter your pin or password:"
    send "[lindex $argv 1]\r"
    expect "Which semester would you like (default Fall 2004)?"
    send "[lindex $argv 2]\r"
    expect eof
    exit


    When I try to look at what @paws_output is storing, all it shows is spawn /var/www/cgi-bin/addu/paws/Fall2004/paws.pl


    I am faced with either rewriting the authentication or getting this stupid expect script to work. You guys have any suggestions? Thank you all very much in advance! :) :D
     
  2. macrumors member

    Joined:
    Jun 11, 2004
    Location:
    UK
    #2
    Ok the last time I tried something like this I used the CPAN expect module rather than an external expect script, but that should work just as well.

    I assume the expect script prints it's output to stdout.

    So in the perl you need to do something like this:
    Code:
    open(EXPECTHANDLE, "/var/www/cgi-bin/addu/automate_paws.ept $student_id $pin_no \'$semester\' |");
    
    while(<EXPECTHANDLE>)
    {
      # The output from the expect script will be placed one line at a time into  $_
      # you can parse it here or just write it into an array or whatever you want
    }
    
    Note the | (pipe) within the quotes at the end of the open instruction. This allows your script to read what the script sends to stdout.
     
  3. thread starter macrumors 6502

    digital1

    Joined:
    Jan 2, 2002
    Location:
    Wisconsin
    #3
    Thanks for responding! When I try that, it's still blank. Its like its not grabbing the output.
     
  4. macrumors member

    Joined:
    Jun 11, 2004
    Location:
    UK
    #4
    Hmmm.
    Are you sure the expect script is sending it's output to stdout?

    First thing to try is get something simple working like:
    Code:
    open(CMD, "ls /var/www/cgi-bin/addu |");
    while(<CMD>)
    {
      print $_;
    }
    close(CMD);
    
    You should just get the output from ls displayed as if you ran it directly.
    Then replace the "ls...blahblah" with the real command and see what gets printed out.
    Then if the output looks right replace the print with the real output handling.

    Hope this helps.
     
  5. macrumors newbie

    Joined:
    Dec 10, 2008
    #5
    Add to the discussion

    I am trying to do something similar to this. I have a perl script that I need to run an expect script. When I try the following:

    open(EXPECTHANDLER, "RESET_ICND4_R1");

    while(<EXPECTHANDLER>)
    {
    print $_;
    # The output from the expect script will be placed one line at a time into $_
    # you can parse it here or just write it into an array or whatever you want
    }
    close;


    It opens and displays the file but does not execute the script. What am I doing wrong?

    Thanks for any help!

    Brandon
     

Share This Page