[SOLVED :P delayed actually] Help with generating ips
Posted: Sat Jun 14, 2003 9:41 am
Hi
I have to generate some ips knowing the network and mask: eg 1.1.1.1/16
basicaly, the algorith is: in the network 1.1.1.1 you have 2^(32-16) ips, in general, in the network x.x.x.x you have 2^(32-mask) ips. including the network and broadkast.
So, what i do:
explode the input by "/" to get the network and the mask.
then get the nr of hosts/subnets (ips) with the formula:
no_ips = 2^(32-mask).
then add to the network 1,2,3... until i have no_ips ips.
I do that by transforming the network from a dottend notation to an integer with the formula: aaa*(256^3) + bbb*(256^2) + ccc*256 + ddd, where the network is: aaa.bbb.ccc.ddd.
After getting every ip, i must translate it back to doted notation to make some chekings.
That's all.
But, a biiiiiiig but:
for masks like /31,/30, /16 it works great (a little slower, but it works). EG: for a mask like /16 it takes 20 sec to generate all the ips.
From mask below /10, my script trow the error with maximum 30 sec execution time bla, so is not working.
I need a way to speed up this algorith or a better one.
10x in advance
coditza
I have to generate some ips knowing the network and mask: eg 1.1.1.1/16
basicaly, the algorith is: in the network 1.1.1.1 you have 2^(32-16) ips, in general, in the network x.x.x.x you have 2^(32-mask) ips. including the network and broadkast.
So, what i do:
explode the input by "/" to get the network and the mask.
then get the nr of hosts/subnets (ips) with the formula:
no_ips = 2^(32-mask).
then add to the network 1,2,3... until i have no_ips ips.
I do that by transforming the network from a dottend notation to an integer with the formula: aaa*(256^3) + bbb*(256^2) + ccc*256 + ddd, where the network is: aaa.bbb.ccc.ddd.
After getting every ip, i must translate it back to doted notation to make some chekings.
That's all.
But, a biiiiiiig but:
for masks like /31,/30, /16 it works great (a little slower, but it works). EG: for a mask like /16 it takes 20 sec to generate all the ips.
From mask below /10, my script trow the error with maximum 30 sec execution time bla, so is not working.
I need a way to speed up this algorith or a better one.
10x in advance
coditza