Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
730 views
in Technique[技术] by (71.8m points)

c# - List IP all addresses in a subnet

I need to get all of the IP addresses contained in within a subnet and I'm trying to do it using IPnetwork

For example the subnet 192.168.1.0/29 would have the following output:

        // Output
        // 192.168.1.0
        // 192.168.1.1
        // 192.168.1.2
        // 192.168.1.3
        // 192.168.1.4
        // 192.168.1.5
        // 192.168.1.6
        // 192.168.1.7

Here is my code:

        IPNetwork ipn = IPNetwork.Parse("192.168.1.0/29");
        IPAddressCollection ips = IPNetwork.ListIPAddress(ipn);

        foreach (IPAddress ip in ips)
        {
            Console.WriteLine(ip);
        }

        // Output
        // 192.168.1.0
        // 192.168.1.0
        // 192.168.1.0
        // 192.168.1.0
        // 192.168.1.0
        // 192.168.1.0

As you can see, this is not the desired result. What am I missing? Is there another tool or method to get this job done? I have manage to hack something up, but it ain't pretty and I'm not sure if it's properly enumerating larger subnets.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

ipnetwork library has been updated (to version 1.3.1) with patch and a testunit covering this issue. It can be downloaded at IPnetwork


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...