Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3358842
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:48:02+00:00 2026-05-18T02:48:02+00:00

How do I iterate through a range of IP addresses provided by the user?

  • 0

How do I iterate through a range of IP addresses provided by the user?

I’m flexible on the format, provided it allows all ranges to be specified. Perhaps something like the nmap-style:

'192.0.2.1'                 #   one IP address

'192.0.2.0-31'              #   one block with 32 IP addresses.

'192.0.2-3.1-254'           #   two blocks with 254 IP addresses.

'0-255.0-255.0-255.0-255'   #   the whole IPv4 address space

For example, if the user entered 192.0.2-3.1-254, I would like to know how to generate a list of all the valid IP addresses in this range so that I could iterate through them.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:48:02+00:00Added an answer on May 18, 2026 at 2:48 am

    For example:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Text.RegularExpressions;
    
    namespace IpRanges
    {
        public class IPRange
        {
            public IPRange(string ipRange)
            {
                if (ipRange == null)
                    throw new ArgumentNullException();
    
                if (!TryParseCIDRNotation(ipRange) && !TryParseSimpleRange(ipRange))
                    throw new ArgumentException();
            }
    
            public IEnumerable<IPAddress> GetAllIP()
            {
                int capacity = 1;
                for (int i = 0; i < 4; i++)
                    capacity *= endIP[i] - beginIP[i] + 1;
    
                List<IPAddress> ips = new List<IPAddress>(capacity);
                for (int i0 = beginIP[0]; i0 <= endIP[0]; i0++)
                {
                    for (int i1 = beginIP[1]; i1 <= endIP[1]; i1++)
                    {
                        for (int i2 = beginIP[2]; i2 <= endIP[2]; i2++)
                        {
                            for (int i3 = beginIP[3]; i3 <= endIP[3]; i3++)
                            {
                                ips.Add(new IPAddress(new byte[] { (byte)i0, (byte)i1, (byte)i2, (byte)i3 }));
                            }
                        }
                    }
                }
    
                return  ips;
            }
    
            /// <summary>
            /// Parse IP-range string in CIDR notation.
            /// For example "12.15.0.0/16".
            /// </summary>
            /// <param name="ipRange"></param>
            /// <returns></returns>
            private bool TryParseCIDRNotation(string ipRange)
            {
                string[] x = ipRange.Split('/');
    
                if (x.Length != 2)
                    return false;
    
                byte bits = byte.Parse(x[1]);
                uint ip = 0;
                String[] ipParts0 = x[0].Split('.');
                for (int i = 0; i < 4; i++)
                {
                    ip = ip << 8;
                    ip += uint.Parse(ipParts0[i]);
                }
    
                byte shiftBits = (byte)(32 - bits);
                uint ip1 = (ip >> shiftBits) << shiftBits;
    
                if (ip1 != ip) // Check correct subnet address
                    return false;
    
                uint ip2 = ip1 >> shiftBits;
                for (int k = 0; k < shiftBits; k++)
                {
                    ip2 = (ip2 << 1) + 1;
                }
    
                beginIP = new byte[4];
                endIP = new byte[4];
    
                for (int i = 0; i < 4; i++)
                {
                    beginIP[i] = (byte) ((ip1 >> (3 - i) * 8) & 255);
                    endIP[i] = (byte)((ip2 >> (3 - i) * 8) & 255);
                }
    
                return true;
            }
    
            /// <summary>
            /// Parse IP-range string "12.15-16.1-30.10-255"
            /// </summary>
            /// <param name="ipRange"></param>
            /// <returns></returns>
            private bool TryParseSimpleRange(string ipRange)
            {
                String[] ipParts = ipRange.Split('.');
    
                beginIP = new byte[4];
                endIP = new byte[4];
                for (int i = 0; i < 4; i++)
                {
                    string[] rangeParts = ipParts[i].Split('-');
    
                    if (rangeParts.Length < 1 || rangeParts.Length > 2)
                        return false;
    
                    beginIP[i] = byte.Parse(rangeParts[0]);
                    endIP[i] = (rangeParts.Length == 1) ? beginIP[i] : byte.Parse(rangeParts[1]);
                }
    
                return true;
            }
    
            private byte [] beginIP;
            private byte [] endIP;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to randomly iterate through a range. Each value will be visited
I need to iterate through the fields on a table and do something if
Does map() iterate through the list like for would? Is there a value in
In my script, I need to iterate through a range of dates given the
I need an elegant way using VB.Net to iterate through a range of IP
How can I iterate through a simple range of ints using a for loop
I'm trying to figure out how to iterate through a document and pull all
I am trying to iterate through the range(750, 765) and add the non-sequential numbers
I use code like this to iterate through a list when I also need
I have a stl set of integers and I would like to iterate through

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.