Perl : ping for multiple IPs not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl : ping for multiple IPs not working
# 1  
Old 01-31-2013
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
Code:
    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[i];
    if ($p->ping($host))
    {
        print "Pass\n";
     }
     else
     {
         print "Fail...\n";
     }
}
$p->close();

ips.txt
Code:
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.

Code:
    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...
# 2  
Old 01-31-2013
I cannot tell:

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

1. verify your input file with
Code:
cat [inputfilename]

2. put print statements to verify the contents of your array. Inside your loop
4. Call p->bind() before your loop.
# 3  
Old 01-31-2013
Quote:
Originally Posted by scriptscript
...
multipleip.pl
Code:
    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[i];
    if ($p->ping($host))
    {
        print "Pass\n";
     }
     else
     {
         print "Fail...\n";
     }
}
$p->close();

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

Code:
use Net::Ping;
...

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

Code:
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.


Code:
..
$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.

Code:
...
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:

Code:
close (IP) or die "Can't close $ifile: $!";

Code:
...
@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:

Code:
...
chomp (@data_count = (<IP>));
...


Code:
...
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:

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:

Code:
#!/path/to/perl -w

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

Hope that helps.
tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Loop to curl multiple ips to ipinfo.io

I am rather new to shell scripting and currently taking a linux course. Im having some troubles writing a loop to curl multiple ips in mutiple access logs to the site ipinfo.io and push the output to a text file for easy viewing and removing duplicates. So far i have these simple lines cat... (1 Reply)
Discussion started by: kjcraig77
1 Replies

2. UNIX for Dummies Questions & Answers

How to ping multiple ip addresses?

Hi, I have ip addresses from 192.168.0.1 to 192.168.0.10. I have to ping those series of IP address in single command? Which command i can use? (2 Replies)
Discussion started by: thomasraj87
2 Replies

3. Red Hat

Nslookup working but ping not working at windows client

Hi Team we have created a DNS server at RHEL6.2 environment in 10.20.203.x/24 network. Everything is going well on linux client as nslookup, ping by host etc in entire subnet. We are getting problem in windows client as nslookup working as well but not ping. all the firewall is disabled and... (5 Replies)
Discussion started by: boby.kumar
5 Replies

4. UNIX for Dummies Questions & Answers

routing multiple IPs

hi guys This is suse 11 sp1 I have a Server that has 4 NICs, I've created 2 bonds bond0(eth0-eth1) - 10.10.10.2 bond1(eth2-eth3) - 10.10.10.3 Each bound goes to a Storage Device which is directly connected so bond0 goes to Storage_Controller_1 and 2 like this Server_bond0 <-------->... (2 Replies)
Discussion started by: karlochacon
2 Replies

5. UNIX for Advanced & Expert Users

ping by hostname not working

anyone ever seen this problem: I can ping the server by IP address but I can't by hostname. nslookup is working and dns query is ok. # nslookup mwxnsb24 Server: 10.11.49.206 Address: 10.11.49.206#53 Name: mwxnsb24 Address: 10.10.58.175 # ping... (8 Replies)
Discussion started by: linuxgeek
8 Replies

6. UNIX for Dummies Questions & Answers

How to assign multiple IPs to Aggregated interface in Solaris 10?

I have 2 physical interfaces (bnx0 and bnx1) aggregated into aggr1. I need to assign second IP, and normally I know how to do it to physical interface (i.e. bnx0:1) however same trick (aggr1:1) is not working. Is there any way to do it? (0 Replies)
Discussion started by: bratan
0 Replies

7. Shell Programming and Scripting

perl return ips after successful ping

Hi, I have this script in ksh, what it does is loop every ip in the nodes_nso and produced another variable up_nodes_nso of only ip's that are up. nodes_nso=$(cat /var/tmp/nodes.txt) echo "ICMP Tests:" up_nodes_nso="" for ip in ${nodes_nso} ; do ping ${ip} 3 > /dev/null if ; then ... (1 Reply)
Discussion started by: borderblaster
1 Replies

8. Shell Programming and Scripting

Shell script to ping a range of IPs

Hi Can someone give me a shell script that can ping a range of IPs and return IPs which are not pingable. Range for example say 192.168.0.1 to 192.168.0.50 and whichever are not pingable then return the IP. Thanks for your help (3 Replies)
Discussion started by: tannu
3 Replies

9. IP Networking

multiple ping replies

Helo ! I set up a new server using FreeBSD 6.0-RELEASE. Everything is ok, until I try to connect it to the internet. After I set up the connection I try to ping yahoo.com and I don't get any reply. When I try to ping the gateway I get a lot of replyes for the same packet. It looks similar to this:... (2 Replies)
Discussion started by: Sergiu-IT
2 Replies

10. Solaris

ping -s not working

I have a multipath system with 2 physicals, a virtual and a failsafe. All network connectivity is working fine to and from all of the interfaces and the virtual. The one thing that is not working is 'ping -s'. From this machine, I cannot send and receive packets using ping -s. ping without the... (1 Reply)
Discussion started by: tjlst15
1 Replies
Login or Register to Ask a Question