drieux wrote:

> On Dec 24, 2003, at 12:07 AM, Tushar Gokhale wrote:
>
> > I've opened a connection to remote machine through perl script and
> > sending
> > commands and can see the output on screen but I want to put that
> > output in
> > File along with the commands that were send. how do I do that?
>
> The obvious first question is how does the
> "output" get to the command line?
>
> Is your code directly printing it? Or is the code
> that you are using to connect to the remote machine
> merely 'leaking' output to the command line?
>
> allow me to illustrate
>
>         my $foo_bar = Net::Foo::Bar->new(@net_connect_stuff);
>
>         foreach my $cmd (@remote_cmd_list)
>         {
>                 $foo_bar->do_remote($cmd);
>         }
>
> in that instance you would know which commands were be sent
> to the remote host.... and what ever that do_remote() method
> is doing is 'leaking' information to the command line rather
> than returning it to your programme.

In the case above, one posssible solution would be a localized
redirection of STDOUT:

use strict;
use warnings;

my @remote_cmd_list = ('print "Hello, world!\n";');

# my $foo_bar = Net::Foo::Bar->new(@net_connect_stuff);
my $filename = 'debug_runs/debug_' . time  . '.log';

{
   open DEBUG_LOG,  ">$filename" or die "Couldn't output to debug log:
$!";
   local *STDOUT = *DEBUG_LOG;
   foreach my $cmd (@remote_cmd_list)
     {
        do_remote($cmd);
     }
}

print "Commands have been run.  Their output is stored in $filename \n"
.
   " If you see this message, then output has also been restored to
normal\n",
   " If you do not see this message, contact your system
administrator\n";

sub do_remote {
   my $command = shift;
   eval $command;
}

Which should allow normal IO to continue as soon as output from the
commands has been captured.

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to