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

have alist of machines I need to ping these machine names are in a file I turned into an Array. How do I test each element of the array and return the result of the ping (specifically the IP address to another file.) this is what I have so far.
#! c:\perl\bin -w my $NAMES = "c:/names.txt"; open NAMES, $NAMES or die "DOH cannot open $NAMES"; @NAMELIST=<NAMES>; chomp(@NAMELIST); foreach $NAME (@NAMELIST) { open TESTED, '>>C:\tested.txt'; system('c:\winnt\system32\ping.exe -n 1 -w 75 "$NAME"'); print TESTED "$NAME\n"; close TESTED; } close NAMES; exit 0;
Any thoughts. I know that the print TESTED "$NAME\n" is incorrect but not sure how to get the return from the ping request?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: how do I test against each element in an array
by chromatic (Archbishop) on Nov 07, 2000 at 22:44 UTC
Re: how do I test against each element in an array
by little (Curate) on Nov 07, 2000 at 22:46 UTC
    And all the answers to your problem you can find in the examples to the module:
    Net::Ping
    see, perls makes it easy :-)

    Originally posted as a Categorized Answer.

Re: how do I test against each element in an array
by kilinrax (Deacon) on Nov 07, 2000 at 22:45 UTC
    This works on my system, (linux, so you'll need to tweak the locations of things), though it's a bit of a kludge.
    #!/usr/bin/perl -w open NAMES, "names.txt" or die "Cannot open names file"; while (<NAMES>) { chomp; my $ping = `ping -c 1 $_`; if ($ping =~ /\((\d+\.\d+\.\d+\.\d+)\)/) { my $ip = $1; open TESTED, '>>tested.txt'; print TESTED "$ip\n"; close TESTED; } else { warn "Cannot deciper result of \"ping $_\""; } } close NAMES; exit 0;

    Originally posted as a Categorized Answer.

Re: how do I test against each element in an array
by davorg (Chancellor) on Nov 07, 2000 at 22:45 UTC

    To get the results from a command, you should look at the back-quotes (`cmd`) or qx() operators. Both are documented in perlop.

    Originally posted as a Categorized Answer.

Re: how do I test against each element in an array
by the_slycer (Chaplain) on Nov 07, 2000 at 22:51 UTC
    If you are trying to get the IP vs hostname, I would suggest using Socket and inet_ntoa(scalar(gethostbyname($host)));

    So, something like this (untested):
    use strict; use Socket; my $ip; open (NAMES, "<c:/names.txt") || die "Could not open c:/names.txt: $!" +; my @hosts=<NAMES>; close NAMES; chomp (@hosts); open (IPS, ">someoutputfile.txt") || die "Could not create file: $!"; foreach (@hosts){ $ip = inet_ntoa(scalar(gethostbyname($_))); print IPS "$_\n"; } close IPS;

    Originally posted as a Categorized Answer.