利用Perl Net::Ping检测网段IP是否…

基于 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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值