列出子网中的所有 IP 地址

发布于 2024-09-08 18:39:38 字数 879 浏览 8 评论 0原文

我需要获取子网中包含的所有 IP 地址,并且我尝试使用 IPnetwork 来完成此操作

例如,子网 192.168.1.0/29 将具有以下输出:

        // 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

这是我的代码:

        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

如您所见,这不是所需的结果。我缺少什么?有其他工具或方法来完成这项工作吗?我设法破解了一些东西,但它并不漂亮,而且我不确定它是否正确枚举了更大的子网。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

这样的小城市 2024-09-15 18:39:38

ipnetwork 库已更新(至版本 1.3.1),包含补丁和涵盖此问题的测试单元。
可以在 IPnetwork 下载

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

究竟谁懂我的在乎 2024-09-15 18:39:38

我修复了 IPAddressCollection 类中的代码。现在它将显示子网中的所有 IP 地址,包括网络、网关、广播。例如,/29 将返回 ips .1 - .7。

这是修改后的修复。

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
    }
}

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
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文