基于
http://hi.baidu.com/zhujian0805/blog/item/330a7be936361e37b80e2d3e .html 做了一些改动, 大多从网上找的资源,自己揉和在一起了.
ip_conf.txt IP段配置文件
# 设置IP段
# 如1=10.110.10-11.1-20
# 表示 10.110.10.1 ~ 10.110.10-11.20 以及 10.110.11.1 ~ 10.110.11.20
1=10.110.10-11.1-20
#! /usr/bin/perl -w
use strict;
use Net::Ping;
die print "usage : range_ping.pl cfgFilePath \n" if scalar @ARGV != 1 ;
my $cfgFile = shift;
print "IP Range Config File: $cfgFile\n" if (-r $cfgFile) or die "The $cfgFile is not readable. <$!>";
# 设置输出结果文件路径
my $RESULT = 'ping_result.txt';
# 设置同时运行的线程数,不要超过60
my $NumberOfThreads=60;
my %ipRanges;
my @hosts;
my $ip='';
# Read Config file - START
open(FH_CONFIG, "<$cfgFile") || die "Unable to open the ip range config file : $cfgFile <$!>";
while(<FH_CONFIG>) {
chomp;
next if /^#/;
# skip comments
next if /^\s*$/;
# skip empty lines
# Read IP Ranges.
if (/^\s*(\w+)\s*=\s*(\d+|\d+-\d+)\.(\d+|\d+-\d+)\.(\d+|\d+-\d+)\.(\d+|\d+-\d+)\s*$/){
print "IP Range:$_\n";
# Store IP Range name to value pair
if (/^\s*(\w+)\s*=\s*(.+)\s*$/){ # Format: IP_Range_Name = IP_Range
$ipRanges{$1} = $2;
}
}
}
close FH_CONFIG;
# Readn Config file - END
# 获取所有IP
sub getHosts{
my $ipRange = shift;
chomp($ipRange);
my @octets = split (/\./, $ipRange);
my @numbers=();
my $i=0;
foreach(@octets){
if (/(\d+)-(\d+)/){
$numbers[$i]=[$1,$2];
}
else {
$numbers[$i]=[$_,$_];
}
$i++;
#print "$_.\n";
}
my $ip;
foreach my $first ($numbers[0][0] .. $numbers[0][1]){
foreach my $second ($numbers[1][0] .. $numbers[1][1]){
foreach my $third ($numbers[2][0] .. $numbers[2][1]){
foreach my $fourth ($numbers[3][0] .. $numbers[3][1]){
$ip = $first.'.'.$second.'.'.$third.'.'.$fourth;
push (@hosts, $ip);
}
}
}
}
}
# Print the IP Ranges
foreach (keys %ipRanges) {
#print "The key $_ contains $ipRanges{$_}\n";
my $ipRange = $ipRanges{$_};
getHosts($ipRange);
print "\n";
}
open(FILE_OUT,">> $RESULT");
sub ping_a_host{
my $host=shift;
chomp($host);
my $a=Net::Ping->new();
print "ping $host\n";
if(!$a->ping($host)){
print FILE_OUT "$host=0\n";
}
else{
print FILE_OUT "$host=1\n";
}
sleep(1);
$a->close();
}
my %pid_to_host;
sub wait_for_a_kid {
my $pid = wait;
return 0 if $pid < 0;
my $host = delete $pid_to_host{$pid}
or warn("Why did I see $pid ($ )\n"), next;#删除HASH表中的回收进程
1;
}
open(RESULT,">$RESULT");
truncate RESULT,0;
close RESULT;
for (@hosts) {
wait_for_a_kid() if keys %pid_to_host > $NumberOfThreads;
if (my $pid = fork) {
## parent does...
$pid_to_host{$pid} = $_;
}
else { # child does
## child does...
ping_a_host($_);
exit 1;
}
}
#回收所有子进程
1 while wait_for_a_kid();
close(FILE_OUT);
print "\nPING运行结果:\n";
open(FH,"$RESULT");
while(<FH>){
print $_;
}
close(FH);
ip_conf.txt IP段配置文件
# 设置IP段
# 如1=10.110.10-11.1-20
# 表示 10.110.10.1 ~ 10.110.10-11.20 以及 10.110.11.1 ~ 10.110.11.20
1=10.110.10-11.1-20
#! /usr/bin/perl -w
use strict;
use Net::Ping;
die print "usage : range_ping.pl cfgFilePath \n" if scalar @ARGV != 1 ;
my $cfgFile = shift;
print "IP Range Config File: $cfgFile\n" if (-r $cfgFile) or die "The $cfgFile is not readable. <$!>";
# 设置输出结果文件路径
my $RESULT = 'ping_result.txt';
# 设置同时运行的线程数,不要超过60
my $NumberOfThreads=60;
my %ipRanges;
my @hosts;
my $ip='';
# Read Config file - START
open(FH_CONFIG, "<$cfgFile") || die "Unable to open the ip range config file : $cfgFile <$!>";
while(<FH_CONFIG>) {
}
close FH_CONFIG;
# Readn Config file - END
# 获取所有IP
sub getHosts{
}
# Print the IP Ranges
foreach (keys %ipRanges) {
}
open(FILE_OUT,">> $RESULT");
sub ping_a_host{
}
my %pid_to_host;
sub wait_for_a_kid {
}
open(RESULT,">$RESULT");
truncate RESULT,0;
close RESULT;
for (@hosts) {
}
#回收所有子进程
1 while wait_for_a_kid();
close(FILE_OUT);
print "\nPING运行结果:\n";
open(FH,"$RESULT");
while(<FH>){
}
close(FH);