#!/usr/local/bin/perl -w use strict; my %ipranges; while () { next if /^\s*$/; last if /END CONFIG/; chomp; my @ranges = map { if (/^\[(\d+)-(\d+)\]$/) {$1, $2} else {$_, $_}} split /\./; $ipranges{$_}=\@ranges; } while () { next if /^\s*$/; chomp; my $ip=$_; RANGES: foreach my $range (keys %ipranges) { my @eatme=@{$ipranges{$range}}; foreach (split /\./, $ip) { my $min = shift @eatme; my $max = shift @eatme; next RANGES unless ($_ >= $min and $_ <= $max) } print "A match of $ip in range $range\n"; } } __DATA__ 123.145.141.2 123.145.[146-149].2 135.168.[10-115].[0-125] END CONFIG 1.2.3.4 123.145.147.2 123.145.147.4 135.168.102.102 __END__ results A match of 123.145.147.2 in range 123.145.[146-149].2 A match of 135.168.102.102 in range 135.168.[10-115].[0-125] #### /usr/local/bin/perl -w use strict; my %ranges; while () { next if /^\s*$/; last if /END CONFIG/; chomp; $ranges{$_}=[ map { if (/^\[(\d+)-(\d+)\]$/) {[$1, $2]} else {[$_, $_]}} split /\./]; } while () { next if /^\s*$/; last if /END/; chomp (my $ip=$_); RANGE: foreach my $range (keys %ranges) { my $i=0; foreach (split /\./, $ip) { next RANGE unless ($_ >= $ranges{$range}->[$i]->[0] and $_ <= $ranges{$range}->[$i++]->[1]); } print "$ip\tmatches range $range\n"; } }