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

hello friends.. i dont know how to copy echo msg to files using perl... here is my example: when i type this command: root@ tiara root# ping host1..... the output was.. < ping: unknown host host1 > ...can u please help me on how to copy the message "ping: unknown host host1" into a file using perl.. thanks friends!

Replies are listed 'Best First'.
Re: copy echo message into file
by Roger (Parson) on Dec 02, 2003 at 05:29 UTC
    Error messages from /usr/sbin/ping are written to STDERR, so your perl should do something like -
    my $result = `ping host 2>&1`;
    to capture the error messages into a scalar (and then process it or save it to a file). Or you could do this -
    system("ping host >result.txt 2>&1");
    from perl to save the results from STDOUT and STDERR of ping into external file 'result.txt'.

      Please note that you probably want to add the "-c count" switch, in order to have ping stop at a certain point. So, for example:
      my $result = `ping -c 5 host 2>&1`;
      But in the ping case, you could also have a look at Net::Ping (which I'd prefer over the backticks).
      --
      B10m
      hmmm.. thanks mr roger..its working... :)
Re: copy echo message into file
by jweed (Chaplain) on Dec 02, 2003 at 05:30 UTC

    This looks suspciciously like a homework problem, but I'll get you started. You need to learn about pipes and perl's open function. Try to find a way in the documentation or in the linux shell to pass output from one program to another. Then it's as easy as opening a log file and printing what you find.



    Who is Kayser Söze?