Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

ping with perl

by varalaxmibbnl (Acolyte)
on Sep 06, 2013 at 13:50 UTC ( #1052715=perlquestion: print w/ replies, xml ) Need Help??
varalaxmibbnl has asked for the wisdom of the Perl Monks concerning the following question:

I WANT THIS OUTPUT TO BE WRITTEN INTO A FILE BUT IT IS NOT DOING THAT PLZ HELP ME OUT TO WORK WITH THIS..

#!/usr/bin/perl open(INFILE, "<server") or die("cannot open infile:\n$!"); open(OUTFILE,">ping_output") or die("cannot open infile:\n$!"); my @ip_array = <INFILE>; chomp(@ip_array); foreach(@ip_array) { if($_ =~ /\d+.\d+.\d+.\d+/) { print OUTFILE ("\n\n\n****pinging system: $`****\n\n "); print OUTFILE system("ping -i 5 -c 4 $&"); } }

Comment on ping with perl
Download Code
Re: ping with perl
by hdb (Prior) on Sep 06, 2013 at 13:55 UTC

    If you remove OUTFILE from the print statements, do you get the desired output?



Re: ping with perl
by choroba (Abbot) on Sep 06, 2013 at 13:56 UTC
    Posting in full caps is similar to shouting in spoken dialogs. Please, avoid that.

    system does not return the output of the command, it returns its exit status. Use backquotes or qx{} to capture the output, or read perlipc for more advanced solutions.

    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: ping with perl
by Corion (Pope) on Sep 06, 2013 at 13:58 UTC

    In addition to the concrete points mentioned by the others, you may want to investigate Net::Ping. Somewhat more advanced is AnyEvent::Ping, which can ping multiple adresses in parallel, but its usage is not as straightforward for somebody not yet familiar with AnyEvent. Also, AnyEvent::FastPing, which even uses its own OS thread to send and receive the packets in the background, and which also has rate limiting.

Re: ping with perl
by Laurent_R (Parson) on Sep 06, 2013 at 17:54 UTC

    The system function is not returning the output of the called OS command, but the exit status of the forked child. If you want to retrieve the output, you need to use the backticks, although I am not sure what the result will be with a ping command.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2014-12-27 00:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls