Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Iterating through all IP addresses in a CIDR

by l2kashe (Deacon)
on Nov 04, 2003 at 17:47 UTC ( #304489=note: print w/replies, xml ) Need Help??


in reply to Iterating through all IP addresses in a CIDR

Heres a nifty line to compute number of addresses based on the CIDR.

my $cidr = 24; my $ips = 1 << (32 - $cidr); # alternately if your block is a /24 or larger (more IPS) # then you could do my $networks = (1 << (32 - $cidr)) / 256; # /24 == 1 # /23 == 2 etc..

Just remember that the first snippet is the total number of IPs in the block, not actual usable. There are all sorts of fun ways to play with this. The logic gets a little tricky based on the smaller blocks, and determining if a network address for the block is on a valid boundary, but its certainly doable. Also module use for this kind of work is a GoodThing(TM). I ended up going through the pains of figuring it all out recently, as I thought the code was going to be semi-throwaway.

use perl;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://304489]
help
Chatterbox?
[Corion]: Oh yay. $project has faffed around for 2 years and now pushed a hasty 2 weeks solution into production without telling me. Of course, the new data also needs changes on my side to be processed correctly. Now they try to raise incidents against my ...
[Corion]: ... input systems to make the change outside of the organized project because I don't have ressources for the project to make the change.

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2017-08-17 09:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Who is your favorite scientist and why?



























    Results (285 votes). Check out past polls.

    Notices?