This problem has been bugging me and the solution is probably very simple.
But I "Can't see the wood for the trees"
I need to catch all output of the expect.pm
Specifically :
. $exp->exp_internal(1);
. $exp->debug(1);
and send it to a file
without it printing to the screen.
Changing from expect is not an option, as it is perfect
to run and capture an unreliable program.
# Define variables used to run the unreliable program
my ($par1, $par2, $par3) = @_;
# Setup array to hold parameters to pass to runrep
my @parameterss;
# Add login parameters
push (@parameters, $login);
# Create the path name for the expect log file
my $expect_log = "/tmp/output.tmp";
# Open the expect log file
open (EXPECT, ">$expect_log")
or die ("Cannot open the expect log file: $expect_log $!\n");
# Spawn program using perl expect module
my $exp = Expect->spawn($program, @parameters)
or die "Cannot spawn $program $login: $!\n";
# Prevent the program's output from being shown on the screen as S
+TDOUT
$exp->log_stdout(0); # But I want to catch this into the $expec
+t_log file
# Populate the expect log file with any output from the expect run
+rep
$exp->log_file("$expect_log"); # This isn't catching the debug/exp
+_internal output
$exp->exp_internal(1);
$exp->debug(1);
# Wait for program to initialise for 5 minutes only
$exp->expect(300,[ qr/starting/]);
# Read in the parameters
$exp->send("read $parameters\n");
# Send appropriate command to program
$exp->send("run $program with parameters");
# Wait for program to run or generate an error
$exp->expect(18000, [ qr/Error/i, sub { my $self = shift;
$self->send("quit\n");exp_cont
+inue;}],
[ qr/success/i, sub { my $self = shift;
$self->send("quit\n"); exp_con
+tinue;}]);
my $output = $exp->exp_error();
# Clear any output and close the program gently
$exp->clear_accum();
$exp->soft_close();
# Close the except file
close(EXPECT);
<\code>
This is a simple example of expect if anyone needs to run it
<readmore>
<code>
######################################################
#!/usr/bin/perl
#
# Fri Dec 13 23:10:54 PST 2002
#
# Copyright Tom Anderson 2002, All rights reserved.
# This program may be copied under the same terms as Perl itself.
# Please send modifications to t@tomacorp.com
#
#
# unreliable.pl - Simulate a program that sometimes just hangs
#
my $VERSION=".01";
use strict;
use warnings;
use diagnostics;
if (rand(10) > 8)
{
sleep 1 while 1 > 0;
}
else
{
print "--------------------------------\n",
"It worked this time, no problems\n",
"--------------------------------\n";
}
##############################################
use warnings;
use diagnostics;
use Expect;
my $timeout=5;
foreach my $i (1..20)
{
my $spawn_ok="not OK";
my $exp = Expect->spawn("./unreliable.pl")
or die "Cannot spawn unreliable process $!\n";
$exp->expect($timeout,
[
'It worked',
sub
{
$spawn_ok = "OK";
exp_continue;
}
],
[
timeout =>
sub
{
print "Process timed out.\n";
}
]
);
print "Status: $spawn_ok\n";
}