in reply to Quickly determine which IP range an IP belongs to.
Output:#!/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
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).$ ./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
|
---|