Iterate over IP Addresses

Let's say I have two IP addresses (in .NET, the System.Net.IPAddress class). How can I iterate over all IP addresses between two addresses?

For example, let one address be 192.168.1.1 and the other 192.168.2.3. I want to somehow sort through all the addresses between them and print them on the console.

Thanks.

+4
source share
2 answers

In the end, I ended up using the approach presented in this answer .

This is a bit more complicated, but works well without using obsolete properties. It converts IP addresses to uint s, increments them, and then converts them back.

0
source

IPv4 addresses are basically Int64 with a different notation. So you can do the following:

 for (var i = fromAddress.Address; i <= toAddress.Address; i++) { Console.WriteLine(new IPAddress(i)); } 

The Address property is deprecated, but this may not bother you.

-2
source

Source: https://habr.com/ru/post/1304707/


All Articles