As long as you don't mean blindingly fast and huge. You want Net::Netmask.
#!/usr/bin/env perl
use strict;
use warnings;
use Net::Netmask;
# our owners and their ranges
my @blocks = (
[ 'bob', '192.168.1.0/24' ],
[ 'nancy', '192.168.1.12' ],
[ 'ralph', '192.168.2.0/255.255.255.0' ],
[ 'bob', '192.168.3.0/24' ],
[ 'NOBODY', '0.0.0.0/0' ],
);
# populate the table with tagged blocks
for my $block (@blocks) {
my ($owner, $range) = @$block;
my $nb = Net::Netmask->new2($range);
$nb->tag('owner', $owner);
$nb->storeNetblock();
};
# do the lookup.
while (my $ip = <DATA>) {
chomp $ip;
my $block = findNetblock($ip);
print "$ip belongs to ", $block->tag('owner'), "\n";
}
__DATA__
192.168.1.45
192.168.1.12
192.168.2.4
192.168.3.33
192.168.4.12
Output:
$ ./net_netmask.pl
192.168.1.45 belongs to bob
192.168.1.12 belongs to nancy
192.168.2.4 belongs to ralph
192.168.3.33 belongs to bob
192.168.4.12 belongs to NOBODY
This works rather well unless you're trying to grok the full internet route tables or such. (Somewhere there's a XS module that implements the routing table from BSD if you need really fast/big).
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.