Get all possible IPs between 2 given addresses

I was following the answers on this question, but using the code in the answer doesn’t work the way I would like.

I would like to have some sort of method for getting all possible IPs between two given IPs (as the question said) and store them in an array or some kind of object, but the OP there didn’t try to use the library suggested, as was commented.

I am using .NET Core 3.1.


static void Main(string[] args)
    if (args.Length < 4)
        throw new ApplicationException("Not enough args!");
    Console.WriteLine("Sufficient arguments.");

    IPAddress ipRangeStart = IPAddress.Parse(args[0]);
    IPAddress ipRangeEnd = IPAddress.Parse(args[1]);
    Console.WriteLine("IPs parsed.");
    int port;
        port = Convert.ToInt32(args[2]);
    catch (Exception)
        throw new FormatException("Invalid port value!");

    if (port > 65535 || port < 1) throw new InvalidOperationException("Port must be in the valid range!");
    Console.WriteLine("Port integer parsed.");

    string filePath = args[3];

    if (!(File.Exists(filePath))) File.Create(filePath);
    Console.WriteLine("Output file resolved.");

    IPAddressRange addies = new IPAddressRange(ipRangeStart, ipRangeEnd);

    Console.WriteLine("IP range created.");

    List<IPAddy> ipList = new List<IPAddy>();
    Console.WriteLine("IP list initialized.");
    Console.WriteLine("Adding IPs to list...");
    // Program hangs here
    foreach (var ip in addies)
        Console.WriteLine("Parsing: " + ip.ToString());
        ipList.Add(new IPAddy(ip.ToString()));    
    Console.WriteLine("IP range parsed.");
    Dictionary<string, bool > ipsEvaluated = Scanner.ScanRanges(ipList.ToArray(), port);

    Console.WriteLine("Evaluating keys...");
    string[] IPPortsOpen = ipsEvaluated.getTrueKeys();
    Console.WriteLine("Writing results...");
    StreamWriter sw = new StreamWriter(filePath, true);

    foreach (string i in IPPortsOpen)


is this IPv4? or IPv6? If we assume IPv4… then IP addresses are really just 32-bit big-endian unsigned integers. Let’s take, as examples, and; now think of them in terms of hex…

decimal is hex 7D.50.CF.79; 7D50CF79 is 2102447993

decimal is hex 8D.DD.84.4F; 8DDD844F is 2380104783

So the IP addresses between and are the same as the integers between 2102447993 and 2380104783, when considered as 32-bit big-endian unsigned values – then just split the octets into pieces and output each octet as an integer.

Leave a Reply

Your email address will not be published. Required fields are marked *