CunnertA
CunnertA

Reputation: 185

Count last digit up from IP

I'm purchasing IPs and trying to create virtual macs with an API from the provider.

My thinking was you would enter the start IP and then the amount of IPs you have. They're always the same, only the last digit goes one up.

e.g

51.82.125.14
51.82.125.15
51.82.125.16
...
...

Then after you enter the first IP and the amount of IPs it would go through a for loop which looks something like this:

int MAX = count.Length;
for(int i = 0; i < MAX; i++)
{
                 
}

and in there somehow count the last digit of the ip up and put it into a list I initalized:

List<string> ipList = new List<string>();

and after the for loop is done, all ips should be inside the list and the process of starting the virtual mac creation should begin.

But how do I count the last digit of the IP up, and also should I determite as an string or something else?

thank you

edit

I have actually tried this solution, but it only spits out 1 increment, for example entered ip "192.168.0.1" and count "6" it prints 6x 192.168.0.2

int MAX = int.Parse(count);
for (int i = 0; i < MAX; i++)
{
   int lastIndex = ip.LastIndexOf(".");
   string lastNumber = ip.Substring(lastIndex + 1);
   string increment = (int.Parse(lastNumber) + 1).ToString();
   string result = string.Concat(ip.Substring(0, lastIndex + 1), increment);
   Notify(result);
}

Upvotes: 0

Views: 331

Answers (2)

user10216583
user10216583

Reputation:

Your code does not return the desired output because you are getting the same last byte in the loop and increasing it by 1 which creates the same IP 192.168.0.2.

string lastNumber = ip.Substring(lastIndex + 1);

To make it work, get the last byte before the loop and increase it by 1 inside it.

For example:

var MAX = 16;
var ip = "192.168.0.1";

if (!IPAddress.TryParse(ip, out IPAddress ipa))
    return;

var ipBytes = ipa.GetAddressBytes();
var lastByte = ipBytes.Last();
                
for (int i = 0; i < MAX; i++)
{
    var result = string.Join(".", ipBytes.Take(3).Append(lastByte));
    lastByte += 1; //Move this up if you want to start with 192.168.0.2
    Notify(result);
}

Alternatively, create a function that returns a range of IP addresses:

public static IEnumerable<string> CreateIPRange(string ip, int count)
{
    if (!IPAddress.TryParse(ip, out IPAddress ipa))
        return Enumerable.Empty<string>();

    var ipBytes = ipa.GetAddressBytes();

    return Enumerable.Range(0, count)
        .Select(i => string
        .Join(".", ipBytes.Take(3)
        .Append((byte)(ipBytes.Last() + i))));
}

... or returns IEnumerable<IPAddress>:

public static IEnumerable<IPAddress> CreateIPARange(string ip, int count)
{
    if (!IPAddress.TryParse(ip, out IPAddress ipa))
        return Enumerable.Empty<IPAddress>();

    var ipBytes = ipa.GetAddressBytes();

    return Enumerable.Range(0, count)
        .Select(i => new IPAddress(ipBytes.Take(3)
        .Append((byte)(ipBytes.Last() + i))
        .ToArray()));
}

... and call it as follows:

void TheCaller()
{
    var MAX = 16;
    var ip = "192.168.0.1";

    CreateIPARange(ip, MAX).ToList().ForEach(x => Console.WriteLine(x));
}

Upvotes: 0

thatguy
thatguy

Reputation: 22089

Using this answer for generation of the next IPv4 address.

private string GetIpV4Address(string ipAddress, int increment)
{
    var addressBytes = IPAddress.Parse(ipAddress).GetAddressBytes().Reverse().ToArray();
    var ipAsUint = BitConverter.ToUInt32(addressBytes, 0);
    var nextAddress = BitConverter.GetBytes(ipAsUint + increment);
    return string.Join(".", nextAddress.Reverse().Skip(4));
}

Create a list of the next count addresses.

 private IEnumerable<string> GetConsecutiveIpV4Addresses(string ipAddress, int count)
 {
     for (var i = 0; i <= count; i++)
         yield return GetIpV4Address(ipAddress, i);
 }

You could use it in your code like this.

private void DoSomething()
{
    // ...your code
    ipList.AddRange(GetConsecutiveIpV4Addresses(ipAddress, count));
}

Of course you can use any other method in the linked question or even string replacement.

Upvotes: 1

Related Questions