Search code examples
bashipipv4seq

Generate all possible ipv4 addresses using seq?


When using seq to generate an ip address, I use seq 0 255 and it generate the last octet. How can I transition this so it will generate all the other octets and their possible combinations (over 4 million combinations). Any help to start would be appreciated


Solution

  • With awk and four loops:

    awk 'BEGIN{OFS="."; for(h=0;h<256;h++){for(i=0;i<256;i++){for(j=0;j<256;j++){for(k=0;k<256;k++){print h,i,j,k}}}}}'
    

    With C and four loops:

    Put this a file with name ipgen.c:

    #include <stdio.h>
    
    int main() {
      int h, i, j, k;
    
      for (h = 0; h < 256; h++) {
        for (i = 0; i < 256; i++) {
          for (j = 0; j < 256; j++) {
            for (k = 0; k < 256; k++) {
              printf("%d.%d.%d.%d\n", h, i, j, k);
            }
          }
        }
      }
      return 0;
    }
    

    Compile it: gcc ipgen.c -o ipgen

    Start it: ./ipgen