Perl : ping for multiple IPs not working | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Perl : ping for multiple IPs not working

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-31-2013
scriptscript scriptscript is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 30 January 2014, 11:03 AM EST
Posts: 74
Thanks: 21
Thanked 0 Times in 0 Posts
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...
Sponsored Links
    #2  
Old 01-31-2013
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 23 October 2014, 9:54 PM EDT
Location: NM
Posts: 10,250
Thanks: 282
Thanked 803 Times in 747 Posts
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.
Sponsored Links
    #3  
Old 01-31-2013
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
 
Join Date: Apr 2009
Last Activity: 24 October 2014, 3:29 AM EDT
Posts: 1,841
Thanks: 7
Thanked 266 Times in 241 Posts
Quote:
Originally Posted by scriptscript View Post
...
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
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
routing multiple IPs karlochacon UNIX for Dummies Questions & Answers 2 03-05-2012 10:40 AM
How to assign multiple IPs to Aggregated interface in Solaris 10? bratan UNIX for Dummies Questions & Answers 0 08-09-2011 01:15 PM
perl return ips after successful ping borderblaster Shell Programming and Scripting 1 01-22-2010 11:12 AM
Shell script to ping a range of IPs tannu Shell Programming and Scripting 3 10-23-2009 03:10 PM
ping -s not working tjlst15 Solaris 1 09-30-2005 01:15 AM



All times are GMT -4. The time now is 10:37 AM.