in reply to Testing Input and Output

This is a great place to use IPC::Open3. It will open STDOUT, STDIN and STDERR as file handle in you script. The only problem is if what you call performs blocking. To be safe, you should use IO::Select to see if there is data to read on the handles.

Here is something I have been playing with, it is rough, but you should get the idea from it.

example:

#!/usr/bin/perl -w # use strict; use POSIX; use IPC::Open3; use IO qw( Select Handle ); $|++; my $host = "host"; my $login = "login"; my $pword = "password"; my $logfil = "/var/log/syslog"; my $cmd = "telnet"; my $count = 0; my ($infh,$outfh,$errfh); my $pid; my $sel = new IO::Select; open (OUTFILE, ">deneb.log"); OUTFILE->autoflush(1); eval { $pid = open3($infh,$outfh,$errfh, "$cmd $host" ) }; die $@ if $@; $sel->add($outfh,$errfh); while (my @ready = $sel->can_read){ foreach my $fh (@ready) { my $line; my $len = sysread $fh, $line, 4096; if (not defined $len) { die "Error reading from child: $!\n"; } elsif ($len == 0){ $sel->remove($fh); }else { if ($fh == $outfh){ if ($count == 0){ print "$line\n"; print $infh "$login\n"; print $infh "$pword\n"; print $infh "tail $logfil"; $count = 1; }elsif ($count == 1){ print $infh "\n"; $count = 2; }elsif ($count == 2){ print OUTFILE $line; sleep 1; $count = 3; print $infh "exit\n"; }else {exit}; }elsif ($fh == $errfh){ print "From STDERR: $errfh\n"; }else { print "Should never be here!!\n"; } } } }

Replies are listed 'Best First'.
Re: Re: Testing Input and Output
by jaldhar (Vicar) on Sep 04, 2002 at 14:35 UTC

    The only problem is if what you call performs blocking.

    Internally the brainfck , operator uses getc which does block.

    To be safe, you should use IO::Select to see if there is data to read on the handles.

    Are you by any chance an ex-C programmer? :-) In my case it's a test script so I know exactly what input and output to expect. If you are doing what I think you are doing something like Net::Telnet might save you a lot of bother.

    --
    જલધર

      Yeah, thanks for the tip. I came to that realization on friday. Some kind of brain lapse. I am using Net::Telnet now and basically threw away what I put here, but it was a nice example of open3 though.