Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Suppress STDOUT of a called program

by jcoaps (Initiate)
on May 16, 2007 at 21:40 UTC ( #615883=perlquestion: print w/ replies, xml ) Need Help??
jcoaps has asked for the wisdom of the Perl Monks concerning the following question:

how can I prevent the STDOUT of a program my script is running? I thought I could do this by running it as a background process, but whatever I was doing failed me. Maybe I was doing it wrong, because I am fairly new to Perl.

The script is initiated from the command line, and the program I am running is Nmap (I am port scanning and parsing the results in a way nontechnical clients can understand). I would like to replace the STDOUT results that Nmap presents the user with my own results, or a status message (i.e "test 1 complete - XX devices discovered running SMTP")

Comment on Suppress STDOUT of a called program
Re: Suppress STDOUT of a called program
by Joost (Canon) on May 16, 2007 at 21:49 UTC
    Running a program in the background doesn't suppress STDOUT.

    If you just want to suppress the output and not do anything with it you can redirect it to /dev/null:

    system("/some/program >/dev/null");
    If you want to capture its output to process it, the easiest way is to use `` quotes:
    my $output = `/some/program`;

    If your program has a lot of output that is better captured as it arrives you can use piped open:

    open(my $handle, "/some/program|") or die; while (<$handle>) { # do stuff with a line }

    See also perlopentut and perlipc.

      That was too easy...next time I'll ask a harder question. For anybody else who has the same question as me...NUL is the windows equivalent of /dev/null.

        File::Spec->devnull() is the cross-platform way to get /dev/null.

Re: Suppress STDOUT of a called program
by johngg (Abbot) on May 16, 2007 at 21:56 UTC
    You could open a filehandle to read the nmap command and read each line in a loop. That way your script receives the command output, not the user, and you can parse the results and sanitise them for consumption by your clients. Something like

    use strict; use warnings; my $nmapCmd = q{/path/to/nmap -and -any -args}; open my $nmapFH, q{-|}, $nmapCmd or die qq{fork: $nmapCmd: $!\n}; while ( <$nmapFH> ) { chomp; # Do something with nmap output here ... print qq{Sanitised stuff for user\n}; } close $nmapFH or die qq{close: $nmapCmd: $!\n};

    I hope this is of use.

    Cheers,

    JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://615883]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2014-10-02 06:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (49 votes), past polls