如何在IP地址范围内循环?

时间:2022-03-15 08:57:50

I want to perform a set of network tasks on an IP address range. As soon as the range is getting bigger than a class c network, I fail to enumerate all hosts in the range. I want to be able to iterate through all hosts of a network with the netmask 255.255.240.0.

我想在IP地址范围内执行一组网络任务。一旦范围变得比c类网络大,我就无法枚举该范围内的所有主机。我希望能够使用网络掩码255.255.240.0遍历网络的所有主机。

From: 192.168.0.100
To:   192.168.10.100

How would one approach this? It must be a pretty common task. I come from the green fields of Cocoa iPhone programming, so a C-stylish solution would be appreciated. :-)

怎么会接近这个?这一定是一项非常普遍的任务。我来自Cocoa iPhone编程的绿色领域,所以我们将欣赏C时尚的解决方案。 :-)

3 个解决方案

#1


Use the modulus operator %. Here is a primitive example:

使用模数运算符%。这是一个原始的例子:

#include <stdio.h>

int main(void) {
    int counter;
    unsigned int ip[] = { 192, 168, 0, 0 };

    for ( counter = 0; counter < 1000; ++counter ) {
        ip[3] = ( ++ ip[3] % 256 );
        if ( !ip[3] ) {
            ip[2] = ( ++ ip[2] % 256 );
        }
        printf("%u:%u:%u:%u\n", ip[0], ip[1], ip[2], ip[3]);
    }

    return 0;
}

#2


This is a piece of code that will quickly introduce you to the nuances involved in interpreting the IP Address and iterating through it.

这是一段代码,它将快速向您介绍解释IP地址和迭代它所涉及的细微差别。

Things get quite simple once you start looking at an IP Address as a 32-bit unsigned integer.

一旦开始将IP地址视为32位无符号整数,事情变得非常简单。

#include <stdio.h>
int
main (int argc, char *argv[])
{
    unsigned int iterator;
    int ipStart[]={192,168,0,100};
    int ipEnd[] = {192,168,10,100};

    unsigned int startIP= (
        ipStart[0] << 24 |
        ipStart[1] << 16 |
        ipStart[2] << 8 |
        ipStart[3]);
    unsigned int endIP= (
        ipEnd[0] << 24 |
        ipEnd[1] << 16 |
        ipEnd[2] << 8 |
        ipEnd[3]);

    for (iterator=startIP; iterator < endIP; iterator++)
    {
        printf (" %d.%d.%d.%d\n",
            (iterator & 0xFF000000)>>24,
            (iterator & 0x00FF0000)>>16,
            (iterator & 0x0000FF00)>>8,
            (iterator & 0x000000FF)
        );
    }

    return 0;
}

Just check that none of the elements for ipStart and ipEnd are greater than 255.
That will not be an IP Address and it will mess up the code too.

只需检查ipStart和ipEnd的元素都不大于255.这不会是一个IP地址,它也会破坏代码。

#3


Here's a PHP solution:

这是一个PHP解决方案:

<?php

$sIP1 = '192.168.0.0';
$sIP2 = '192.168.1.255';

$aIPList = array();
if ((ip2long($sIP1) !== -1) && (ip2long($sIP2) !== -1)) // As of PHP5, -1 => False
 {
 for ($lIP = ip2long($sIP1) ; $lIP <= ip2long($sIP2) ; $lIP++)
  {
  $aIPList[] = long2ip($lIP);
  }
 }
?>

There's a good summary of the (basic) maths involved here

这里涉及的(基本)数学有一个很好的总结

#1


Use the modulus operator %. Here is a primitive example:

使用模数运算符%。这是一个原始的例子:

#include <stdio.h>

int main(void) {
    int counter;
    unsigned int ip[] = { 192, 168, 0, 0 };

    for ( counter = 0; counter < 1000; ++counter ) {
        ip[3] = ( ++ ip[3] % 256 );
        if ( !ip[3] ) {
            ip[2] = ( ++ ip[2] % 256 );
        }
        printf("%u:%u:%u:%u\n", ip[0], ip[1], ip[2], ip[3]);
    }

    return 0;
}

#2


This is a piece of code that will quickly introduce you to the nuances involved in interpreting the IP Address and iterating through it.

这是一段代码,它将快速向您介绍解释IP地址和迭代它所涉及的细微差别。

Things get quite simple once you start looking at an IP Address as a 32-bit unsigned integer.

一旦开始将IP地址视为32位无符号整数,事情变得非常简单。

#include <stdio.h>
int
main (int argc, char *argv[])
{
    unsigned int iterator;
    int ipStart[]={192,168,0,100};
    int ipEnd[] = {192,168,10,100};

    unsigned int startIP= (
        ipStart[0] << 24 |
        ipStart[1] << 16 |
        ipStart[2] << 8 |
        ipStart[3]);
    unsigned int endIP= (
        ipEnd[0] << 24 |
        ipEnd[1] << 16 |
        ipEnd[2] << 8 |
        ipEnd[3]);

    for (iterator=startIP; iterator < endIP; iterator++)
    {
        printf (" %d.%d.%d.%d\n",
            (iterator & 0xFF000000)>>24,
            (iterator & 0x00FF0000)>>16,
            (iterator & 0x0000FF00)>>8,
            (iterator & 0x000000FF)
        );
    }

    return 0;
}

Just check that none of the elements for ipStart and ipEnd are greater than 255.
That will not be an IP Address and it will mess up the code too.

只需检查ipStart和ipEnd的元素都不大于255.这不会是一个IP地址,它也会破坏代码。

#3


Here's a PHP solution:

这是一个PHP解决方案:

<?php

$sIP1 = '192.168.0.0';
$sIP2 = '192.168.1.255';

$aIPList = array();
if ((ip2long($sIP1) !== -1) && (ip2long($sIP2) !== -1)) // As of PHP5, -1 => False
 {
 for ($lIP = ip2long($sIP1) ; $lIP <= ip2long($sIP2) ; $lIP++)
  {
  $aIPList[] = long2ip($lIP);
  }
 }
?>

There's a good summary of the (basic) maths involved here

这里涉及的(基本)数学有一个很好的总结