Posts
22
Comments
75
Trackbacks
0
Looping to IP Address Range

As a Security/Systems Administrator, one of my task is to scan for rouge devices, services and other status and/or information regarding our network. One of the major task is to know which hosts belongs to a specified IP Range for example 192.168.0.1 to 192.168.100.255. 

One of the best solutions and probably the fastest is to convert the Ip range and iterate in its Binary form. With the help of shift operators (<< and >>), & and | operator here is my simple implementation in C# =)

-----------------------------------------------------------------
             int counter;

             int[] ipStart = {192,168,0,0};
             int[]  ipEnd = {192,168,255,255};

             int startIP= (
                ipStart[0] << 24 |
                ipStart[1] << 16 |
                ipStart[2] << 8 |
                ipStart[3]);

             int endIP= (
                ipEnd[0] << 24 |
                ipEnd[1] << 16 |
                ipEnd[2] << 8 |
                ipEnd[3]);

             for (counter = startIP; counter <= endIP; counter++)
             {
                 Console.WriteLine("{0}.{1}.{2}.{3}", 
                            (counter & 0xFF000000) >> 24, 
                            (counter & 0x00FF0000) >> 16, 
                            (counter & 0x0000FF00) >> 8, 
                            counter & 0x000000FF);
             }
-------------------------------------------------------------------------

           0xFF000000 is equal to 11111111000000000000000000000000 in Binary
            0x00FF0000 is equal to 00000000111111110000000000000000 in Binary
            0x0000FF00 is equal to 00000000000000001111111100000000 in Binary
            0x000000FF is equal to 00000000000000000000000011111111 in Binary
 

Till Next Time.......

posted on Thursday, January 20, 2011 12:21 PM Print
Comments
Gravatar
# re: Looping to IP Address Range
Dana
1/20/2011 2:09 PM
Thanks for this simple programming. It would be a very great help for me. Thanks.
Gravatar
# re: Looping to IP Address Range
Ethical Hacking Training
12/12/2011 9:14 PM
I’m speechless. It is a excellent blog and very attractive too. Nice work! That’s no longer truly a lot coming from an newbie writer like me, but it surely’s all I may just say after diving into your posts. Great grammar and vocabulary. Not like other blogs. You actually recognize what you?re talking about too. So much that you simply made me want to discover more. Your blog has grow to be a stepping stone for me, my friend.
Gravatar
# re: Looping to IP Address Range
haider
12/29/2011 7:59 PM
kindly explain 0xFF000000 is equal to 11111111000000000000000000000000 in Binary i m not getting
Gravatar
# re: Looping to IP Address Range
stocksguru
2/29/2012 6:26 PM
worthy...but seems to be a little geeky, might be coz i'm a newbie...this is not my field even though i am intreasted

Post Comment

Title *
Name *
Email
Comment *  
Verification
Tag Cloud