自定義的子網範圍以下:web
- #!/usr/bin/perl
- use strict;
- use warnings;
- use Data::Dumper;
- my %hash;
- while(<DATA>){
- chomp;
- my($ip,$netmask)=split/\//;
- my $ipbit = unpack("B32",pack("C4", (split/\./,$ip)));
- my $net = substr("$ipbit",0,"$netmask");
- push @{$hash{$net}}, $ip,$netmask;
- }
- #print Dumper \%hash;
- my @testip=qw{192.168.1.5 219.111.193.1 219.111.1.2};
- foreach my $kipbit (keys %hash){
- my $netmask = $hash{$kipbit}[1];
- my $netip = $hash{$kipbit}[0];
- foreach my $testkip (@testip){
- my $testipbit = unpack("B32",pack("C4", (split/\./,$testkip))); #C4轉化爲char類型的數據類型,C4也能夠寫做CCCC,B32將IP地址轉化爲32位的bit。
- $testipbit = substr("$testipbit",0,"$netmask");
- if($kipbit == $testipbit){
- print "$netip/$netmask\n";
- }
- }
- }
- __DATA__
- 192.168.0.0/16
- 219.111.192.0/18
- 68.132.0.0/17
- 61.135.0.0/16
- 192.162.0.0/16
- 152.172.0.0/16
- 34.132.0.0/14
- 97.208.0.0/13