Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Checking IP addresses against lists of IPs, partial IPs, and netmasks.

Reply
Thread Tools

Checking IP addresses against lists of IPs, partial IPs, and netmasks.

 
 
Adam Funk
Guest
Posts: n/a
 
      06-27-2005
I'm trying to write some Perl code to check an IP addresses against a list
of strings containing IP address, partial IP addresses ending in ".",
net/mask pairs and net/masklength pairs.

In other words, I'd like ip_found("192.168.2.3") to return true if the test
list contains any of the following:
192.168.2.3
192.168.
192.168.2.0/255.255.255.0
192.168.2.0/24

I'm sure someone else has done this already -- any good code to recycle?

Thanks,
Adam

 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      06-27-2005
Adam Funk <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> I'm trying to write some Perl code to check an IP addresses against a list
> of strings containing IP address, partial IP addresses ending in ".",
> net/mask pairs and net/masklength pairs.
>
> In other words, I'd like ip_found("192.168.2.3") to return true if the test
> list contains any of the following:
> 192.168.2.3
> 192.168.
> 192.168.2.0/255.255.255.0
> 192.168.2.0/24
>
> I'm sure someone else has done this already -- any good code to recycle?


So what have you tried? Which modules have you considered?

Just throwing specifications at the newsgroup is unlikely to give
useful replies.

Anno
 
Reply With Quote
 
 
 
 
Adam Funk
Guest
Posts: n/a
 
      06-27-2005
Anno Siegel wrote:

> Adam Funk <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>> I'm trying to write some Perl code to check an IP addresses against a
>> list of strings containing IP address, partial IP addresses ending in
>> ".", net/mask pairs and net/masklength pairs.
>>
>> In other words, I'd like ip_found("192.168.2.3") to return true if the
>> test list contains any of the following:
>> 192.168.2.3
>> 192.168.
>> 192.168.2.0/255.255.255.0
>> 192.168.2.0/24
>>
>> I'm sure someone else has done this already -- any good code to recycle?

>
> So what have you tried? Which modules have you considered?


I had started from scratch using regexps and some other string manipulation,
but fortunately found Net::Netmask, which does a lot of the job for me.

I can read the input file and for each netmask string, create a Net::Netmask
and test with the Net::Netmask->match($ip) method, until it returns true or
I run out of input.

But I'm wondering if there is a more efficient way to do it.

 
Reply With Quote
 
axel@white-eagle.invalid.uk
Guest
Posts: n/a
 
      06-27-2005
Adam Funk <(E-Mail Removed)> wrote:
> I'm trying to write some Perl code to check an IP addresses against a list
> of strings containing IP address, partial IP addresses ending in ".",
> net/mask pairs and net/masklength pairs.


> In other words, I'd like ip_found("192.168.2.3") to return true if the test
> list contains any of the following:
> 192.168.2.3
> 192.168.
> 192.168.2.0/255.255.255.0
> 192.168.2.0/24


Unless I am being very stupid here, what you want to do is simply
match '^192.168.' against the list. So a basic check could be:

my @list = qw(192.168.2.3
192.168.
192.168.2.0/255.255.255.0
192.168.2.0/24);
my $instr = '192.168.';
if (grep /^$instr/, @list) { print "OK"}

However I suspect you are wanting to do more than that and check
if the IP address is within a certain range... but then you would
need to supply more information about the address to be checked
(i.e. what class of address).

Axel
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      06-27-2005
Adam Funk <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
>
> > Adam Funk <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> >> I'm trying to write some Perl code to check an IP addresses against a
> >> list of strings containing IP address, partial IP addresses ending in
> >> ".", net/mask pairs and net/masklength pairs.
> >>
> >> In other words, I'd like ip_found("192.168.2.3") to return true if the
> >> test list contains any of the following:
> >> 192.168.2.3
> >> 192.168.
> >> 192.168.2.0/255.255.255.0
> >> 192.168.2.0/24
> >>
> >> I'm sure someone else has done this already -- any good code to recycle?

> >
> > So what have you tried? Which modules have you considered?

>
> I had started from scratch using regexps and some other string manipulation,
> but fortunately found Net::Netmask, which does a lot of the job for me.
>
> I can read the input file and for each netmask string, create a Net::Netmask
> and test with the Net::Netmask->match($ip) method, until it returns true or
> I run out of input.
>
> But I'm wondering if there is a more efficient way to do it.


Don't worry about efficiency at this point. Enjoy having found a
method that *works* and move on. When the program as a whole is too
slow, and if it turns out that the IP search is the culprit, then it's
time to think about a speed-up.

In any case, we have too little information about the way that search
is performed to come up with improvements. If you run many IPs against
a rarely changing set of network blocks the situation is different from
the other way 'round.

Anno
 
Reply With Quote
 
Tintin
Guest
Posts: n/a
 
      06-28-2005

"Adam Funk" <(E-Mail Removed)> wrote in message
news:d9ok0j$2gdc$(E-Mail Removed)...
> I'm trying to write some Perl code to check an IP addresses against a list
> of strings containing IP address, partial IP addresses ending in ".",
> net/mask pairs and net/masklength pairs.
>
> In other words, I'd like ip_found("192.168.2.3") to return true if the
> test
> list contains any of the following:
> 192.168.2.3
> 192.168.
> 192.168.2.0/255.255.255.0
> 192.168.2.0/24
>
> I'm sure someone else has done this already -- any good code to recycle?



Have a look at:

http://search.cpan.org/~marcel/Net-I...-0.01/Match.pm
http://search.cpan.org/~luismunoz/NetAddr-IP-3.24/IP.pm


 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      06-28-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>>192.168.2.3
>>192.168.
>>192.168.2.0/255.255.255.0
>>192.168.2.0/24

>
> However I suspect you are wanting to do more than that and check
> if the IP address is within a certain range... but then you would
> need to supply more information about the address to be checked
> (i.e. what class of address).


I've seen that syntax before. The class of address _is_ being provided.

192.168. = 192.168.*.* = 192.168.0.0:255.255.0.0 = 192.168/16
192.168.2 = 192.168.2.* = 192.168.2.0:255.255.255.0 = 192.168.2/24
192.168.2.0/24 = 192.168.2/24
192.168.2.3 = 192.168.2.3:255.255.255.255 = 192.168.2.3/32

-Joe
 
Reply With Quote
 
axel@white-eagle.invalid.uk
Guest
Posts: n/a
 
      06-30-2005
Joe Smith <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
>>>192.168.2.3
>>>192.168.
>>>192.168.2.0/255.255.255.0
>>>192.168.2.0/24


>> However I suspect you are wanting to do more than that and check
>> if the IP address is within a certain range... but then you would
>> need to supply more information about the address to be checked
>> (i.e. what class of address).


> I've seen that syntax before. The class of address _is_ being provided.


> 192.168. = 192.168.*.* = 192.168.0.0:255.255.0.0 = 192.168/16
> 192.168.2 = 192.168.2.* = 192.168.2.0:255.255.255.0 = 192.168.2/24
> 192.168.2.0/24 = 192.168.2/24
> 192.168.2.3 = 192.168.2.3:255.255.255.255 = 192.168.2.3/32


No it was not provided. Only an IP number was provided to be matched.
The class of which is unknown... except in the era of pre-classless
addresses.

We know what a 192.168 number is by simply looking at it... a
private number... but if the number had been 194... it would be a
different story.

Axel



 
Reply With Quote
 
Adam Funk
Guest
Posts: n/a
 
      06-30-2005
Anno Siegel wrote:

> Don't worry about efficiency at this point. Enjoy having found a
> method that *works* and move on. When the program as a whole is too
> slow, and if it turns out that the IP search is the culprit, then it's
> time to think about a speed-up.
>
> In any case, we have too little information about the way that search
> is performed to come up with improvements. If you run many IPs against
> a rarely changing set of network blocks the situation is different from
> the other way 'round.


The program would normally check one IP address against a list of IPs
and IP ranges parsed in from a file, so the main part would be
something like this:

$matched = 0 ;
foreach $ip_range (@ip_ranges) {
$block = new Net::Netmask($ip_range);
if ($block->match($ip_to_check)) {
$matched = 1;
last;
}
}

if that looks OK to you!

 
Reply With Quote
 
Adam Funk
Guest
Posts: n/a
 
      06-30-2005
(E-Mail Removed) wrote:

> > In other words, I'd like ip_found("192.168.2.3") to return true if the test
> > list contains any of the following:
> > 192.168.2.3
> > 192.168.
> > 192.168.2.0/255.255.255.0
> > 192.168.2.0/24

>
> Unless I am being very stupid here,


My examples were not very good.

> what you want to do is simply
> match '^192.168.' against the list. So a basic check could be:


That would work for the first two examples, but if the list of IP
ranges to check against includes "218.0.0.0/7" then "219.84.130.159"
should match.


> However I suspect you are wanting to do more than that and check
> if the IP address is within a certain range... but then you would
> need to supply more information about the address to be checked
> (i.e. what class of address).


I found the Net::Netmask module on CPAN.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
M$ against Blu-ray, M$ for Blu-ray, M$ against Blu-ray, M$ forBlu-ray, ...... Blig Merk DVD Video 66 04-27-2008 04:46 AM
Re: Partial 1.0 - Partial classes for Python Thomas Heller Python 13 02-08-2007 08:12 PM
Re: Partial 1.0 - Partial classes for Python J. Clifford Dyer Python 0 02-08-2007 05:29 PM
List of lists of lists of lists... =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?= Python 5 05-15-2006 11:47 AM
Why? Partial Class within a Partial class Billy ASP .Net 2 02-01-2006 09:10 AM



Advertisments