如何将IP地址增加指定数量? [英] How can I increment an IP address by a specified amount?

查看:32
本文介绍了如何将IP地址增加指定数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何增加一个起始IP地址,并将其增加我指定的偏移量.我试图这样做,但是我做错了,因为我得到的IP到处都是,甚至不在同一网络范围内.

I am trying to figure out how to increment a starting ip address, and increment it by an offset that I specify. I have attempted to do this, but I am doing something wrong because I am getting IPs that are all over the place, not even in the same network range.

我目前正在做的是获取我的起始ip和结束ip,获取地址总数,然后将总ips增加一个偏移量,然后尝试实际增加IP.

What I am currently doing is taking my starting ip and ending ip, getting the total amount of addresses then incrementing the total ips by an offset then attempting to actually increment the IP.

我将总IP增加一个偏移量,因此我知道增加IP的数量.(每个偏移量我要完成不同的任务.)无论循环将"t"增加到多少,我都会增加IP数量.既然我已经给出了解决方案,我的问题似乎只在于实际增加ips,在这种情况下任何人都可以帮助我.谢谢.

I am incrementing to the total ips by an offset so I know how many to increment the ip. (I am completing different tasks per offset.) Whatever the loop has incremented "t" to that is how many I increment IPs. Now that I have given the rundown, my issue only seems to be with actually incrementing ips, can anyone help me out in this situation. Thanks.

            string from = txtStart.Text, to = txtEnd.Text;
            uint current = from.ToUInt(), last = to.ToUInt();

            ulong total = last - current;
            int offset = 3; //This is an example number, it actually could be anything.

            while (current <= last)
            {
             for (int t = 0; t < total; t += offset)
                    {
                        uint ut = Convert.ToUInt32(t);
                        current = current + ut;
                        var ip = current.ToIPAddress();
                    }  
              }

这是我正在使用的扩展类.他们工作正常.

Here is the extension class I am using. They work fine.

public static class Extensions
    {
        public static uint ToUInt(this string ipAddress)
        {
            var ip = IPAddress.Parse(ipAddress);
            var bytes = ip.GetAddressBytes();
            Array.Reverse(bytes);
            return BitConverter.ToUInt32(bytes, 0);
        }

        public static string ToString(this uint ipInt)
        {
            return ToIPAddress(ipInt).ToString();
        }

        public static IPAddress ToIPAddress(this uint ipInt)
        {
            var bytes = BitConverter.GetBytes(ipInt);
            Array.Reverse(bytes);
            return new IPAddress(bytes);
        }
    }

推荐答案

[TestFixture]
public class GetNextIpAddressTest
{
    [Test]
    public void ShouldGetNextIp()
    {
        Assert.AreEqual("0.0.0.1", GetNextIpAddress("0.0.0.0", 1));
        Assert.AreEqual("0.0.1.0", GetNextIpAddress("0.0.0.255", 1));
        Assert.AreEqual("0.0.0.11", GetNextIpAddress("0.0.0.1", 10));
        Assert.AreEqual("123.14.1.101", GetNextIpAddress("123.14.1.100", 1));
        Assert.AreEqual("0.0.0.0", GetNextIpAddress("255.255.255.255", 1));
    }

    private static string GetNextIpAddress(string ipAddress, uint increment)
    {
        byte[] addressBytes = IPAddress.Parse(ipAddress).GetAddressBytes().Reverse().ToArray();
        uint ipAsUint = BitConverter.ToUInt32(addressBytes, 0);
        var nextAddress = BitConverter.GetBytes(ipAsUint + increment);
        return String.Join(".", nextAddress.Reverse());
    }
}

这篇关于如何将IP地址增加指定数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆