[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Expanding subnets



Hi kj,

On Wed, Jun 03, 2009 at 09:35:15AM +0100, kj wrote:
> I need to feed subnets into a database in the format 192.168.0.% or 
> 192.168.%.%

Whenever I find myself trying to do something like this, I stop and
consider if my database design is optimal.

You can store an IPv4 address as a 32-bit unsigned integer, and you
can store an IPv4 network as one of those together with a prefix
between 0 and 32.  You can then do operations on these.

However...

> which means, for example, 192.168.0.0/23 should break down into:
> 
> 192.168.0.%
> 192.168.1.%
> 
> Does anyone know of a way to convert this?

Perl's Net::Netmask module helps:

$ perl -MNet::Netmask -e '$block = new Net::Netmask("192.168.0.0/23"); print join("\n", $block->enumerate(24)), "\n";'
192.168.0.0
192.168.1.0

After adding some validation, you could replace the trailing '.0'
with '.%'.

Cheers,
Andy

-- 
http://bitfolk.com/ -- No-nonsense VPS hosting

>> I have just recently purchased a Feathercraft Big Kahuna kayak
> does it have a heater?
Of course not.  Everyone knows you can't have your kayak and heat it.
  -- James Fidell

Attachment: signature.asc
Description: Digital signature


Reply to: