in reply to IP Filtering RegEx needed
This is a bit clunky but I think it should do what you want. I am going to see if I can optimise out some cruft.
#!/usr/local/bin/perl -w use strict; my %ipranges; while (<DATA>) { next if /^\s*$/; last if /END CONFIG/; chomp; my @ranges = map { if (/^\[(\d+)-(\d+)\]$/) {$1, $2} else {$_, $_}} split /\./; $ipranges{$_}=\@ranges; } while (<DATA>) { 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 (<DATA>) { next if /^\s*$/; last if /END CONFIG/; chomp; $ranges{$_}=[ map { if (/^\[(\d+)-(\d+)\]$/) {[$1, $2]} else {[$_, $_]}} split /\./]; } while (<DATA>) { 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"; } }
Cheers,
R.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: IP Filtering RegEx needed
by EdwardG (Vicar) on Sep 15, 2004 at 15:47 UTC | |
by Random_Walk (Prior) on Sep 15, 2004 at 16:20 UTC | |
by EdwardG (Vicar) on Sep 15, 2004 at 16:31 UTC | |
|
Re^2: IP Filtering RegEx needed
by Anonymous Monk on Sep 15, 2004 at 15:21 UTC |