uzzikie has asked for the wisdom of the Perl Monks concerning the following question:

hello everyone,
i tried to capture what the system() function would return and display them on my script
something like

use IO::Scalar; tie *STDOUT, 'IO::Scalar', \$a; @args = ("/bin/ls", "-al"); print system(@args); untie *STDOUT;

however, for some reason the script dies complaining about Bad headers
but isn't what i did supposed to prevent STDOUT from printing on screen?
is there something i overlooked?

oh btw, i read read up on the previous posts regarding this topic but it didn't help :(

thanks!

Replies are listed 'Best First'.
Re: redirect STDOUT from system() function
by snax (Hermit) on Aug 10, 2001 at 13:46 UTC
    OK, so you say it's SUID. Be sure to clean up some environment variables:
    $ENV{PATH}=''; $ENV{IFS}='';
    With no PATH you have to explicitly give the entire path to system calls and so forth, but you're doing that anyway.

    Now I can suggest the way I normally play with STDOUT:

    open LS, qq(/bin/ls -al|) || die qq(cannot execute /bin/ls: $!); while (<LS>) { # Do whatever you want to do with STDOUT from # the ls call here } close LS;

    This is basically straight out of perlipc.

Re: redirect STDOUT from system() function
by ChOas (Curate) on Aug 10, 2001 at 12:24 UTC
    Hi!

    How about using backticks:
    #!/usr/bin/perl -w use strict; my @List=`ls -al`; print @List;
    Would that do it for you ?

    GreetZ!,
      ChOas

    print "profeth still\n" if /bird|devil/;
      hi

      nope i can't use backticks as the script is running setuid...
      says the $ENV{path} is insecur
      would be a bad idea anyway....
        Either way, Perl spawns a system call. You'll have to untaint a couple of things in %ENV. For example, this fails:
        #!/usr/bin/perl -wT use strict; # delete $ENV{qw( BASH_ENV PATH )}; my $output = `/bin/ls`; print "Output is ($output)\n";
        Uncommenting the appropriate line makes it work, at least on my box. Depending on what's tainted, you may have to do a little more work.
Re: redirect STDOUT from system() function
by stefan k (Curate) on Aug 10, 2001 at 12:28 UTC
    Since you're mentioning the bad headers problem I assume that you're running your script as a CGI. Then you should probably print something like "Content-type: text/plain".
    To get the STDOUT of a system call you should use the backticks ("`") or the qx/ls/-way. Using system() just gives you the return value of the call (which is zero in most cases) not their output. Actually this is quite a FAQ since it is asked many times.
    If the script dies complaining there is probably an error in it that lets it die before any reasonable output appears. Are you sure you got IO::Scalar installed? Have you tested running it on your local machine? Have you access to the webservers logfiles (where you could find more messages)?
    If I got things wrong -the webserver assume- please excuse...

    Regards... Stefan