Perl : ping for multiple IPs not working

I have written perl ping program to ping list of IPs one by one and print the status.But each and every time it is showing the status as Pass for all IPs even though the IP is wrong.

multipleip.pl

    use Net::Ping;
    $p = Net::Ping->new();    
    $ifile="inventory.txt";
    open(IP,$ifile) or die("Not able to open the file");
    @data_count=(<IP>);
    for($i=0;$i<=$#data_count;$i++)
    {

    my $host=$data_count;
    if ($p->ping($host))
    {
        print "Pass\n";
     }
     else
     {
         print "Fail...\n";
     }
}
$p->close();

ips.txt

173.252.110.27
173.2522.110.27
8.25.218.11
8.258.218.11

But the same program is working fine for single IP.

    use Net::Ping;
    $p = Net::Ping->new();    
    my $host = "8.25.218.11";
   # print "$host is alive.\n" if $p->ping($host);
   
    if ($p->ping($host))
    {
        print "success";
    }
    else
    {
         print "Fail";
    }     
    $p->close();

Could you please let me know what went wrong with the multipleip.pl program

Regards,
John

---------- Post updated at 06:18 AM ---------- Previous update was at 05:23 AM ----------

trying my level best but not albe to solve this...Any help on this is much appreciated...

I cannot tell:

you open inventory.txt, you display ips.txt.

  1. verify your input file with
cat [inputfilename]
  1. put print statements to verify the contents of your array. Inside your loop
  2. Call p->bind() before your loop.

A few observations:

use Net::Ping;
...

I highly recommend adding the following two lines at the top of every serious Perl program that you write:

use strict;
use warnings;

The first line forces you to declare all your variables. The second one does a quick check and blurts out warnings about potential issues that it sees. It's like having an extra set of eyes for your program, and sometimes, could save many hours of frustration.

..
$ifile="inventory.txt";
...

Ensure that the file you open is the one you've specified in your post. You open "inventory.txt" here, but you've specified "ips.txt" in your post.

...
open(IP,$ifile) or die("Not able to open the file");
...

A good practice is to actually close all files you open, once you are done with the processing. Add the following line at the end of the program:

close (IP) or die "Can't close $ifile: $!";
...
@data_count=(<IP>);
...

You'll need to "chomp" all such input that you read. Perl doesn't remove the trailing EOL character (unlike awk). Do this instead:

...
chomp (@data_count = (<IP>));
...
...
for($i=0;$i<=$#data_count;$i++)
{
my $host=$data_count[i];
...

That's the iterator variable "$i" and not "i", inside the "for" loop. Perl wouldn't understand the "i" at that point. Correct code:

...
for($i=0;$i<=$#data_count;$i++)
{
my $host=$data_count[$i];
...

Note that the last issue would've been pointed out by the perl interpreter if "use warnings" was specified.

Another way of using warnings is to set it in the shebang line:

#!/path/to/perl -w

Replace the "/path/to/perl" with the actual path to perl in your system.

Hope that helps.
tyler_durden