Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Perl : ping for multiple IPs not working

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-31-2013
scriptscript scriptscript is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 16 November 2014, 9:00 AM EST
Posts: 75
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 Unix and Linux 01-31-2013
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 18 April 2015, 6:46 AM EDT
Location: NM
Posts: 10,428
Thanks: 328
Thanked 857 Times in 796 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 Unix and Linux 01-31-2013
durden_tyler's Unix or Linux Image
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
 
Join Date: Apr 2009
Last Activity: 5 April 2015, 4:04 PM EDT
Posts: 1,904
Thanks: 9
Thanked 285 Times in 259 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 07:38 AM.