List IP all addresses in a subnet

混江龙づ霸主 提交于 2019-11-27 18:47:18

问题


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.


回答1:


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




回答2:


I fixed the code in the IPAddressCollection class. It will now show all IP addresses in the subnet including network, gateway, broadcast. For example, a /29 would return ips .1 - .7.

Here's the amended fix.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections;

namespace LukeSkywalker.IPNetwork
{
    public class IPAddressCollection : IEnumerable<IPAddress>, IEnumerator<IPAddress>
    {
        private IPNetwork _ipnetwork;
        private double _enumerator;

        internal IPAddressCollection(IPNetwork ipnetwork)
        {
            this._ipnetwork = ipnetwork;
            this._enumerator = -1;
        }

        #region Count, Array, Enumerator

        public double Count
        {
            get
            {
                // return this._ipnetwork.Usable;
                return this._ipnetwork.Usable + 2;
            }
        }

        public IPAddress this[double i]
        {
            get
            {
                if (i >= this.Count)
                {
                    throw new ArgumentOutOfRangeException("i");
                }

                IPNetworkCollection ipn = IPNetwork.Subnet(this._ipnetwork, 32);

                // return ipn[0].Network;

                return ipn[i].Network;
            }
        }

        #endregion

        #region IEnumerable Members

        IEnumerator<IPAddress> IEnumerable<IPAddress>.GetEnumerator()
        {
            return this;
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this;
        }

        #region IEnumerator<IPNetwork> Members

        public IPAddress Current
        {
            get { return this[this._enumerator]; }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            // nothing to dispose
            return;
        }

        #endregion

        #region IEnumerator Members

        object IEnumerator.Current
        {
            get { return this.Current; }
        }

        public bool MoveNext()
        {
            this._enumerator++;
            if (this._enumerator >= this.Count)
            {
                return false;
            }
            return true;
        }

        public void Reset()
        {
            this._enumerator = -1;
        }

        #endregion

        #endregion
    }
}


来源:https://stackoverflow.com/questions/3219762/list-ip-all-addresses-in-a-subnet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!