PDA

View Full Version : PERL and Expect Scripting...




digital1
Nov 23, 2005, 06:36 PM
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



techgeek
Nov 28, 2005, 10:17 AM
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:

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.

digital1
Nov 29, 2005, 10:16 AM
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:

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.

Thanks for responding! When I try that, it's still blank. Its like its not grabbing the output.

techgeek
Nov 29, 2005, 10:57 AM
Hmmm.
Are you sure the expect script is sending it's output to stdout?

First thing to try is get something simple working like:

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.

brandoncarroll
Dec 10, 2008, 10:02 PM
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