Re: Script Required to Check a range of IP's




#!/usr/bin/perl

use strict;
use warnings;

Excellent!

use Net::Ping;

my $file_name=$ARGV[0];

What if its not given anything?

my $line;

You don't use this?

my @host_array;
open(FILE,"$file_name") || die "Not been Accessed" ;

Why is $file_name in quotes?
Why is the die string in double quotes?
What aren't you putting the reason it failed in the string?

@host_array=<FILE>;

Why my() it before and populate it here?

my @host_array = <FILE>;

close(FILE);
my $p = Net::Ping->new("icmp");

Why are you having a text string interpolated?

foreach my $host(@host_array)
{
if($p->ping($host, 2))
{
chomp($host);

Why chomp it here and below twice, why not sooner and only once?

print "$host is alive.\n";
}
else
{
chomp($host);
print "$host not reachable.\n";
}
sleep(1);
}
$p->close();
close (FILE);


Here's the same thing but "Perl Best Practice" ified a bit:

#!/usr/bin/perl

use strict;
use wanrings;
use Net::Ping;

die 'Please give me a filename as my argument!' if !defined $ARGV[0];
open(my $ipfile_fh, '<', $ARGV[0]) || die "Could not open $ARGV[0]: $!";

my $icmp = Net::Ping->new('icmp');

while(<$ipfile_fh>) {
chomp $host;

if( $icmp->ping($host, 2) ) {
print "$host is alive! weeeee\n";
}
else {
print "$host is dead. boooo\n";
}

sleep 1;
}

$icmp->close();
close $ipfile_fh;
.



Relevant Pages

  • Re: $1 $2 var confusion
    ... I have strict and warnings enabled. ... until the end of the enclosing block or until the next successful match, ... it becomes undefined as there are no capturing parentheses. ... unless ($host eq 'domain.com') { ...
    (perl.beginners)
  • Re: Script Required to Check a range of IPs
    ... Input File --- test.txt ... use strict; ... use warnings; ... print "$host not reachable.\n"; ...
    (perl.beginners)
  • Re: connecting to multiple hosts using Net::SSH2
    ... # (or to steal a routine from it) it'll be much easier to read ... use strict; ... # setup new connection in an OO sort of way: ... There's a few ways to feed in the list of hosts you want to use to hit all the boxes in a host list and execute your command. ...
    (perl.beginners)
  • Re: Virtual mail backscatter
    ... stricter spam fitering and so bouncing back emails that I have let ... The answer is to make your spam-scanning on the MX host as strict as ...
    (comp.mail.sendmail)